編輯:關於Android編程
經過一個星期的折騰,終於做完了這個Android 聊天表情輸入、表情翻頁帶效果、下拉刷新聊天記錄。這只是一個單獨聊天表情的輸入,以及聊天的效果實現。因為我沒有寫服務器,所以沒有雙方聊天的效果。主要是聊天中表情的選擇,發送。表情翻頁帶有不同的效果。我在主要代碼中都寫了注釋。下面看代碼實現。附上本文源碼,代碼較多。
一、先看實現的效果圖
二、調用接口以及設置MainActivity
package com.example.activity; import java.util.ArrayList; import java.util.List; import java.util.Set; import com.org.adapter.FaceAdapter; import com.org.adapter.FacePageAdeapter; import com.org.adapter.MessageAdapter; import com.org.util.MyApplication; import com.org.util.SharePreferenceUtil; import com.org.view.CirclePageIndicator; import com.org.view.JazzyViewPager; import com.org.view.JazzyViewPager.TransitionEffect; import com.org.xlistview.MsgListView; import com.org.xlistview.MsgListView.IXListViewListener; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.drawable.ColorDrawable; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.text.Editable; import android.text.Spannable; import android.text.SpannableString; import android.text.TextWatcher; import android.text.style.ImageSpan; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.GridView; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity implements OnClickListener, OnTouchListener, IXListViewListener{ private Button sendBtn; private ImageButton faceBtn; private LinearLayout faceLinearLayout; private EditText msgEt; private InputMethodManager mInputMethodManager; private MessageAdapter mMessageAdapter; private JazzyViewPager faceViewPager; private MsgListView mMsgListView; private MyApplication mApplication; private SharePreferenceUtil mSpUtil; private WindowManager.LayoutParams mLayoutParams; private ListmListFaceKeys; private int currentPage = 0; private boolean isFaceShow = false; private static int MsgPagerNum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chat_main); initData(); initUI(); initFacePage(); } private void initData() { mApplication = MyApplication.getInstance(); //SharePreference存儲類 mSpUtil = new SharePreferenceUtil(this, message_save); //初始化消息列表適配器 mMessageAdapter = new MessageAdapter(this, initMsgData()); //加載表情的列表 Set keySet = MyApplication.getInstance().getFaceMap().keySet(); mListFaceKeys = new ArrayList (); mListFaceKeys.addAll(keySet); MsgPagerNum = 0; } private void initUI() { mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); //獲取窗口觸摸操作 mLayoutParams = getWindow().getAttributes(); mMsgListView = (MsgListView) findViewById(R.id.msg_listView); // 觸摸ListView隱藏表情和輸入法 mMsgListView.setOnTouchListener(this); mMsgListView.setPullLoadEnable(false); mMsgListView.setXListViewListener(this); mMsgListView.setAdapter(mMessageAdapter); mMsgListView.setSelection(mMessageAdapter.getCount() - 1); sendBtn = (Button) findViewById(R.id.send_btn); faceBtn = (ImageButton) findViewById(R.id.face_btn); faceLinearLayout = (LinearLayout) findViewById(R.id.face_ll); msgEt = (EditText) findViewById(R.id.msg_et); faceLinearLayout = (LinearLayout) findViewById(R.id.face_ll); faceViewPager = (JazzyViewPager) findViewById(R.id.face_pager); //標題欄控件 TextView mTitle = (TextView) findViewById(R.id.ivTitleName); mTitle.setText(默默笙箫); TextView mTitleLeftBtn = (TextView) findViewById(R.id.ivTitleBtnLeft); mTitleLeftBtn.setVisibility(View.VISIBLE); mTitleLeftBtn.setOnClickListener(this); //輸入框的觸摸監聽的綁定 msgEt.setOnTouchListener(this); msgEt.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (mLayoutParams.softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE || isFaceShow) { faceLinearLayout.setVisibility(View.GONE); isFaceShow = false; // imm.showSoftInput(msgEt, 0); return true; } } return false; } }); //輸入框的實時輸入長度的監聽 msgEt.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { if (s.length() > 0) { sendBtn.setEnabled(true); } else { sendBtn.setEnabled(false); } } }); faceBtn.setOnClickListener(this); sendBtn.setOnClickListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { switch (v.getId()) { case R.id.msg_listView: //ListView觸摸實現 mInputMethodManager.hideSoftInputFromWindow(msgEt.getWindowToken(), 0); faceLinearLayout.setVisibility(View.GONE); isFaceShow = false; break; case R.id.msg_et: //輸入框觸摸實現 mInputMethodManager.showSoftInput(msgEt, 0); faceLinearLayout.setVisibility(View.GONE); isFaceShow = false; break; default: break; } return false; } //歷史數據,在開始時顯示 private List initMsgData() { List msgList = new ArrayList ();// 消息對象數組 MessageItem item = new MessageItem(MessageItem.MESSAGE_TYPE_TEXT, mSpUtil.getNick(), System.currentTimeMillis(), 歷史消息, mSpUtil.getHeadIcon(), false, 0); msgList.add(item); return msgList; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.face_btn: //彈出表情 if (!isFaceShow) { mInputMethodManager.hideSoftInputFromWindow(msgEt.getWindowToken(), 0); try { Thread.sleep(80);// 解決此時會黑一下屏幕的問題 } catch (InterruptedException e) { e.printStackTrace(); } faceLinearLayout.setVisibility(View.VISIBLE); isFaceShow = true; } else { faceLinearLayout.setVisibility(View.GONE); isFaceShow = false; } break; case R.id.send_btn:// 發送消息 String msg = msgEt.getText().toString(); MessageItem item = new MessageItem(MessageItem.MESSAGE_TYPE_TEXT, mSpUtil.getNick(), System.currentTimeMillis(), msg, mSpUtil.getHeadIcon(), false, 0); mMessageAdapter.upDateMsg(item); mMsgListView.setSelection(mMessageAdapter.getCount() - 1); msgEt.setText(); break; case R.id.ivTitleBtnLeft: finish(); break; // case R.id.ivTitleBtnRigh: // break; default: break; } } //加載表情,以及設置翻頁效果 private void initFacePage() { List lv = new ArrayList (); for (int i = 0; i < MyApplication.NUM_PAGE; ++i) lv.add(getGridView(i)); FacePageAdeapter adapter = new FacePageAdeapter(lv, faceViewPager); faceViewPager.setAdapter(adapter); faceViewPager.setCurrentItem(currentPage); faceViewPager.setTransitionEffect(mEffects[mSpUtil.getFaceEffect()]); CirclePageIndicator indicator = (CirclePageIndicator) findViewById(R.id.indicator); indicator.setViewPager(faceViewPager); adapter.notifyDataSetChanged(); faceLinearLayout.setVisibility(View.GONE); indicator.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { currentPage = arg0; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // do nothing } @Override public void onPageScrollStateChanged(int arg0) { // do nothing } }); } //表情表格控件放置,設置背景 private GridView getGridView(int i) { GridView gv = new GridView(this); gv.setNumColumns(7); //一行顯示7個表情 gv.setSelector(new ColorDrawable(Color.TRANSPARENT));// 屏蔽GridView默認點擊效果 gv.setBackgroundColor(Color.TRANSPARENT); gv.setCacheColorHint(Color.TRANSPARENT); gv.setHorizontalSpacing(1); gv.setVerticalSpacing(1); gv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); gv.setGravity(Gravity.CENTER); gv.setAdapter(new FaceAdapter(this, i)); gv.setOnTouchListener(forbidenScroll()); // 防止亂pageview亂滾動 gv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { if (arg2 == MyApplication.NUM) { // 刪除表情鍵的位置 int selection = msgEt.getSelectionStart(); String text = msgEt.getText().toString(); if (selection > 0) { String text2 = text.substring(selection - 1); if (].equals(text2)) { int start = text.lastIndexOf([); int end = selection; msgEt.getText().delete(start, end); return; } msgEt.getText().delete(selection - 1, selection); } } else { int count = currentPage * MyApplication.NUM + arg2; // 注釋的部分,在EditText中顯示字符串 // String ori = msgEt.getText().toString(); // int index = msgEt.getSelectionStart(); // StringBuilder stringBuilder = new StringBuilder(ori); // stringBuilder.insert(index, keys.get(count)); // msgEt.setText(stringBuilder.toString()); // msgEt.setSelection(index + keys.get(count).length()); // 下面這部分,在EditText中顯示表情 Bitmap bitmap = BitmapFactory.decodeResource( getResources(), (Integer) MyApplication .getInstance().getFaceMap().values() .toArray()[count]); if (bitmap != null) { int rawHeigh = bitmap.getHeight(); int rawWidth = bitmap.getHeight(); int newHeight = 40; int newWidth = 40; // 計算縮放因子 float heightScale = ((float) newHeight) / rawHeigh; float widthScale = ((float) newWidth) / rawWidth; // 新建立矩陣 Matrix matrix = new Matrix(); matrix.postScale(heightScale, widthScale); // 設置圖片的旋轉角度 // matrix.postRotate(-30); // 設置圖片的傾斜 // matrix.postSkew(0.1f, 0.1f); // 將圖片大小壓縮 // 壓縮後圖片的寬和高以及kB大小均會變化 Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, rawWidth, rawHeigh, matrix, true); ImageSpan imageSpan = new ImageSpan(MainActivity.this, newBitmap); String emojiStr = mListFaceKeys.get(count); SpannableString spannableString = new SpannableString( emojiStr); spannableString.setSpan(imageSpan, emojiStr.indexOf('['), emojiStr.indexOf(']') + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); msgEt.append(spannableString); } else { String ori = msgEt.getText().toString(); int index = msgEt.getSelectionStart(); StringBuilder stringBuilder = new StringBuilder(ori); stringBuilder.insert(index, mListFaceKeys.get(count)); msgEt.setText(stringBuilder.toString()); msgEt.setSelection(index + mListFaceKeys.get(count).length()); } } } }); return gv; } // 防止亂pageview亂滾動 private OnTouchListener forbidenScroll() { return new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { return true; } return false; } }; } @Override protected void onPause() { // TODO Auto-generated method stub mInputMethodManager.hideSoftInputFromWindow(msgEt.getWindowToken(), 0); faceLinearLayout.setVisibility(View.GONE); isFaceShow = false; super.onPause(); } //處理下拉刷新的效果 @Override public void onRefresh() { MsgPagerNum++; List msgList = initMsgData(); int position = mMessageAdapter.getCount(); mMessageAdapter.setMessageList(msgList); mMsgListView.stopRefresh(); mMsgListView.setSelection(mMessageAdapter.getCount() - position - 1); Log.i(Show,MsgPagerNum = + mMessageAdapter + , adapter.getCount() = + mMessageAdapter.getCount()); } @Override public void onLoadMore() { // TODO Auto-generated method stub } private TransitionEffect mEffects[] = { TransitionEffect.Standard, TransitionEffect.Tablet, TransitionEffect.CubeIn, TransitionEffect.CubeOut, TransitionEffect.FlipVertical, TransitionEffect.FlipHorizontal, TransitionEffect.Stack, TransitionEffect.ZoomIn, TransitionEffect.ZoomOut, TransitionEffect.RotateUp, TransitionEffect.RotateDown, TransitionEffect.Accordion, };// 表情翻頁效果 }
一個手機號可以注冊兩個微信嗎?很多人還不知道一個手機號怎麼注冊2個甚至多個微信號,下面小編就跟大家分享一下方法吧!一個手機號怎麼注冊兩個微信: 登錄你(已
示例效果如下: MainActivity.xml package sn.qdj.popupwindowdemo; import android.su
ATCID主要用來處理PC端傳輸過來的AT命令,從AT命令實際處理的地方來說,主要分為3類: 1. 需要Modem來處理的AT命令; 2. 需要在nativ
基礎配置部分解說過程是從你Native加入rn模塊,新建的rn項目配置的東西都有首先 在你的項目根目錄同級開一個rn目錄(這裡只是舉個例子)mkdir xxx-rn &a