android的自定义表情

android的自定义表情啊?貌似还没有发现有客户端有,都是图片,如果能像在电脑上那样自定义表情的功能多好,那位大哥知道,麻烦告知一声,呵呵。写完,睡觉。


我们仔细来观察下腾讯微博的qq表情发送规律,由/开始,1到3个中文或者英文字符.


写个工具类来测试已测试正则表达式来匹配表情。




在上方输入框中可以输入查询 格式为 @你选择的列表值


这个是话题输入界面,格式为#话题#


表情选择页面,这个其实是一个每行5列的GridView



此界面可看到你写的微博的内容,点击发送,发送成功


哈哈,看到了吧,我的微博首页已经显示了我刚才发送的带有话题@person和表情的微博了。


接下来,上代码。


Java代码

  1. public
  2. class AddWeiboActivity extends Activity implements OnClickListener{    



  3. private DataHelper dataHelper;    

  4. private UserInfo user;    

  5. private String user_default_name;    

  6. private MyWeiboSync weibo;    

  7. private ListView listView;    

  8. private EditText weibo_content;    

  9. private Button send_btn;    

  10. private Button add_cmamera_btn;    

  11. private Button add_at_btn;    

  12. private Button add_topic_btn;    

  13. private Button add_expression_btn;    

  14. private Button add_location_btn;    

  15. private GridView expressionGrid;    

  16. private List<Map<String,Object>> expressionList;    

  17. private ExpressionAdapter expressionAdapter;    

  18. private FrameLayout operation_layout;    

  19. private RelativeLayout add_top_bar;    



  20. private ListView atListView;    

  21. private RelativeLayout atRootLayout;    

  22. private EditText atEditText;    

  23. private Button atEnterBtn;    

  24. private TextView topic_tip;    



  25. private RelativeLayout.LayoutParams atEdiLayoutParams,atEnterBtnLayoutParams,atListViewLayoutParams,topicTipViewLayoutParams;    



  26. private JSONArray array;    

  27. private Handler handler;    

  28. private ArrayAdapter atAdapter;    

  29. private List<String> atList;    

  30. private AtThread thread;    

  31. private List<String> matchStrList;//选择atList匹配的字符串



  32. private
  33. int flag;    

  34. private
  35. static
  36. int FLAG_1 = 1;    

  37. private
  38. static
  39. int FLAG_2 = 2;//1和2代表atEnterBtn的父亲控件不同





  40. @Override


  41. protected
  42. void onCreate(Bundle savedInstanceState) {    

  43. super.onCreate(savedInstanceState);    
  44.         setContentView(R.layout.add_weibo);    


  45.         setUpViews();    
  46.         setUpListeners();    


  47.         dataHelper = DataBaseContext.getInstance(getApplicationContext());    
  48.         weibo = WeiboContext.getInstance();    


  49.         SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);    
  50.         user_default_name = preferences.getString("user_default_name", "");//取得微博默认登录账号信息




  51.         handler = new AtHandler();    
  52.         thread = new AtThread();    
  53.         thread.start();//开启一个线程获取数据


  54.     }    



  55. private
  56. void setUpViews(){    
  57.         weibo_content = (EditText)findViewById(R.id.weibo_content);    
  58.         send_btn = (Button)findViewById(R.id.send_btn);    
  59.         add_cmamera_btn = (Button)findViewById(R.id.add_cmamera_btn);    
  60.         add_at_btn = (Button)findViewById(R.id.add_at_btn);    
  61.         add_topic_btn = (Button)findViewById(R.id.add_topic_btn);    
  62.         add_expression_btn = (Button)findViewById(R.id.add_expression_btn);    
  63.         add_location_btn = (Button)findViewById(R.id.add_location_btn);    


  64.         add_top_bar = (RelativeLayout)findViewById(R.id.add_top_bar);    
  65.         operation_layout = (FrameLayout)findViewById(R.id.operation_layout);    
  66.         expressionGrid = new GridView(this);    
  67.         expressionGrid.setNumColumns(5);    
  68.         expressionList = buildExpressionsList();    
  69.         expressionAdapter = new ExpressionAdapter(AddWeiboActivity.this, expressionList);    
  70.         expressionGrid.setAdapter(expressionAdapter);    



  71. //以下代码至本方法setUpViews结束,是个人纯粹蛋疼联系纯代码布局,各位老大可以改成xml布局,淡定




  72.         atRootLayout = new RelativeLayout(AddWeiboActivity.this);    


  73.         atEditText = new EditText(AddWeiboActivity.this);    
  74.         atEditText.setId(10000);    


  75.         atEnterBtn = new Button(AddWeiboActivity.this);    
  76.         atEnterBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_enter_selector));    


  77.         atListView = new ListView(AddWeiboActivity.this);    
  78.         atListView.setCacheColorHint(Color.TRANSPARENT);//防止滑屏时出现黑快,不信可以注释掉此句试一试


  79.         atListView.setDivider(getResources().getDrawable(R.drawable.list_divider));//设置分割线


  80.         atListView.setBackgroundColor(Color.argb(255, 239, 239, 239));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的




  81.         topic_tip = new TextView(AddWeiboActivity.this);    
  82.         topic_tip.setText("请输入话题");    
  83.         topic_tip.setTextSize(20);    
  84.         topic_tip.setTextColor(Color.argb(255, 90, 142, 189));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的




  85.         atRootLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));    
  86.         atEdiLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,80);    
  87.         atEnterBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);    
  88.         atListViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);    
  89.         topicTipViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);    





  90. //添加布局约束


  91.         atEdiLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);    


  92.         atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);    
  93.         atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);    
  94.         atEnterBtnLayoutParams.setMargins(0, 10, 10, 0);//设置边距,分别代表左,上,右,下




  95.         atListViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);    
  96.         atListViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());    


  97.         topicTipViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());    


  98.     }    



  99. private
  100. void setUpListeners(){    
  101.         send_btn.setOnClickListener(this);    
  102.         add_cmamera_btn.setOnClickListener(this);    
  103.         add_at_btn.setOnClickListener(this);    
  104.         add_topic_btn.setOnClickListener(this);    
  105.         add_expression_btn.setOnClickListener(this);    
  106.         add_location_btn.setOnClickListener(this);    
  107.         expressionGrid.setOnItemClickListener(new GridItemClickListener());    
  108.         atListView.setOnItemClickListener(new AtListViewItemListener());    
  109.         atEditText.addTextChangedListener(new MyTextWatcher());    
  110.         atEnterBtn.setOnClickListener(new OnClickListener() {    

  111. @Override


  112. public
  113. void onClick(View v) {    
  114.                 add_top_bar.setVisibility(View.VISIBLE);    
  115.                 weibo_content.setVisibility(View.VISIBLE);    
  116.                 operation_layout.setVisibility(View.GONE);    
  117.                 operation_layout.removeAllViews();//别忘记要移除掉



  118. if(flag==FLAG_1){    
  119.                     weibo_content.setText(weibo_content.getText()+"@");    
  120.                 }else
  121. if(flag==FLAG_2){    
  122.                     weibo_content.setText(weibo_content.getText()+"#"+atEditText.getText()+"#");    
  123.                 }    




  124.             }    
  125.         });    
  126.     }    



  127. class AtThread extends Thread {    

  128. @Override


  129. public
  130. void run() {    
  131.             String jsonStr = weibo.getFans(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), 20, 0, user_default_name);    

  132. try {    
  133.                 JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");    
  134.                 array = dataObj.getJSONArray("info");    
  135.             } catch (JSONException e) {    
  136.                 e.printStackTrace();    
  137.             }    

  138. //通知handler处理数据


  139.             Message msg = handler.obtainMessage();    
  140.             handler.sendMessage(msg);    
  141.         }    
  142.     }    



  143. class AtHandler extends Handler {    

  144. @Override


  145. public
  146. void handleMessage(Message msg){    

  147. int size = array.length();    
  148.             atList = new ArrayList<String>();    

  149. for(int i = 0;i<size;i++){    
  150.                 JSONObject data = array.optJSONObject(i);    

  151. try {    
  152.                     atList.add(data.getString("nick")+"("+data.getString("name")+")");    
  153.                 } catch (JSONException e) {    
  154.                     e.printStackTrace();    
  155.                 }    
  156.             }    
  157.             matchStrList = new ArrayList<String>();    
  158.             matchStrList.addAll(atList);    
  159.             atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,atList);    
  160.             atListView.setAdapter(atAdapter);    
  161.         }    
  162.     }    



  163. class ExpressionAdapter extends BaseAdapter {    

  164. private Context context;    

  165. private LayoutInflater inflater;    

  166. private List<Map<String,Object>> list;    



  167. public ExpressionAdapter(Context context, List<Map<String,Object>> list) {    

  168. super();    

  169. this.context = context;    

  170. this.list = list;    

  171. this.inflater = LayoutInflater.from(context);    
  172.         }    



  173. @Override


  174. public
  175. int getCount() {    

  176. return list.size();    
  177.         }    



  178. @Override


  179. public Object getItem(int position) {    

  180. return list.get(position);    
  181.         }    



  182. @Override


  183. public
  184. long getItemId(int position) {    

  185. return position;    
  186.         }    



  187. @Override


  188. public View getView(final
  189. int position, View convertView, ViewGroup parent){    
  190.             Map<String,Object> map = list.get(position);    
  191.             ImageView image = new ImageView(context);    
  192.             image.setImageDrawable((Drawable)map.get("drawable"));    

  193. return image;    
  194.         }    
  195.     }    



  196. @Override


  197. public
  198. void onClick(View v) {    

  199. if(operation_layout.getChildCount()>0){    
  200.             add_top_bar.setVisibility(View.VISIBLE);    
  201.             weibo_content.setVisibility(View.VISIBLE);    
  202.             operation_layout.setVisibility(View.GONE);    
  203.             operation_layout.removeAllViews();//别忘记要移除掉



  204. return;    
  205.         }    

  206. switch (v.getId()) {    



  207. case R.id.send_btn:{    
  208.             String returnStr = weibo.publishMsg(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), weibo_content.getText().toString());    

  209. try {    
  210.                 JSONObject dataObj = new JSONObject(returnStr);    

  211. if("ok".equals(dataObj.getString("msg"))){    
  212.                     Toast.makeText(AddWeiboActivity.this, "发送成功", Toast.LENGTH_SHORT).show();//我日,记得要show,每次都搞忘记


  213.                 }    
  214.             } catch (JSONException e) {    
  215.                 e.printStackTrace();    
  216.             }    
  217.         }    

  218. break;    

  219. case R.id.add_cmamera_btn:{    


  220.         }    

  221. break;    

  222. case R.id.add_at_btn:{    

  223. // 动态的组装view


  224.             atRootLayout.removeAllViews();// 组装前先把所有的孩子拿掉


  225.             atEditText.setText("@");    
  226.             flag = FLAG_1;//区分atEnterBtn是在哪个界面按的


  227.             atRootLayout.addView(atEditText, atEdiLayoutParams);    
  228.             atRootLayout.addView(atEnterBtn, atEnterBtnLayoutParams);    
  229.             atRootLayout.addView(atListView, atListViewLayoutParams);    
  230.             operation_layout.addView(atRootLayout);    


  231.             add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响


  232.             weibo_content.setVisibility(View.GONE);    
  233.             operation_layout.setVisibility(View.VISIBLE);    
  234.         }    

  235. break;    

  236. case R.id.add_topic_btn:{    

  237. //动态的组装view


  238.             atRootLayout.removeAllViews();//组装前先把所有的孩子拿掉


  239.             atEditText.setText("");    
  240.             flag = FLAG_2;//区分atEnterBtn是在哪个界面按的


  241.             atRootLayout.addView(atEditText,atEdiLayoutParams);    
  242.             atRootLayout.addView(atEnterBtn,atEnterBtnLayoutParams);    
  243.             atRootLayout.addView(topic_tip,topicTipViewLayoutParams);    
  244.             operation_layout.addView(atRootLayout);    


  245.             add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响


  246.             weibo_content.setVisibility(View.GONE);    
  247.             operation_layout.setVisibility(View.VISIBLE);    
  248.         }    

  249. break;    

  250. case R.id.add_expression_btn:{    
  251.             add_top_bar.setVisibility(View.GONE);//隐藏上面的bar和文本编辑框,不让之与表情选择的gridView相互影响


  252.             weibo_content.setVisibility(View.GONE);    
  253.             operation_layout.addView(expressionGrid);    
  254.             operation_layout.setVisibility(View.VISIBLE);    
  255.         }    

  256. break;    

  257. case R.id.add_location_btn:{    


  258.         }    

  259. break;    

  260. default:    

  261. break;    
  262.         }    
  263.     }    

  264. private List<Map<String,Object>> buildExpressionsList(){    
  265.         List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();    
  266.         DecimalFormat df = new DecimalFormat("000");//格式化数字



  267. for(int i = 0;i<105;i++){    
  268.             Map<String,Object> map = new HashMap<String, Object>();    
  269.             String formatStr = "h"+df.format(i);    

  270. int drawableId = 0 ;    

  271. try {    
  272.                 drawableId = R.drawable.class.getDeclaredField(formatStr).getInt(this);//反射取得id,这个地方循环套反射,是不是很耗性能啊,我没测试过,麻烦有好办法的兄弟姐妹分享一下


  273.             } catch (IllegalArgumentException e) {    
  274.                 e.printStackTrace();    
  275.             } catch (SecurityException e) {    
  276.                 e.printStackTrace();    
  277.             } catch (IllegalAccessException e) {    
  278.                 e.printStackTrace();    
  279.             } catch (NoSuchFieldException e) {    
  280.                 e.printStackTrace();    
  281.             }    
  282.             Drawable drawable = getResources().getDrawable(drawableId);    
  283.             map.put("drawableId", formatStr);    
  284.             map.put("drawable",drawable);    
  285.             list.add(map);    
  286.         }    

  287. return list;    
  288.     }    



  289. class GridItemClickListener implements OnItemClickListener {    

  290. @Override


  291. public
  292. void onItemClick(AdapterView<?> adapterView, View view, int position,long arg3) {    
  293.             Map<String, Object> map = expressionList.get(position);    
  294.             String drawableId = (String)map.get("drawableId");    


  295.             add_top_bar.setVisibility(View.VISIBLE);    
  296.             weibo_content.setVisibility(View.VISIBLE);    
  297.             operation_layout.setVisibility(View.GONE);    
  298.             operation_layout.removeAllViews();//别忘记要移除掉




  299.             String expressionStr=null;    
  300.             expressionStr = TextUtil.drawableIdToFaceName.get(drawableId);    
  301.             expressionStr="/"+expressionStr;    
  302.             weibo_content.setText(weibo_content.getText().toString()+expressionStr);    
  303.         }    
  304.     }    



  305. class MyTextWatcher implements TextWatcher{    

  306. @Override


  307. public
  308. void afterTextChanged(Editable s){    
  309.             String changingStr = atEditText.getText().toString();    

  310. if(changingStr.indexOf("@")!=-1){    
  311.                 changingStr = changingStr.substring(1);    
  312.             }    



  313. int size = atList.size();    
  314.             matchStrList.clear();    

  315. for(int i = 0;i<size;i++){    
  316.                 String currentStr = atList.get(i);    

  317. if(currentStr.indexOf(changingStr)!=-1){    
  318.                     matchStrList.add(currentStr);    
  319.                 }    
  320.             }    
  321.             atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,matchStrList);    
  322.             atAdapter.notifyDataSetChanged();    
  323.             atListView.setAdapter(atAdapter);    
  324.         }    



  325. @Override


  326. public
  327. void beforeTextChanged(CharSequence s, int start, int count,    

  328. int after) {    


  329.         }    



  330. @Override


  331. public
  332. void onTextChanged(CharSequence s, int start, int before,int count) {    


  333.         }    
  334.     }    



  335. class AtListViewItemListener implements OnItemClickListener{    

  336. @Override


  337. public
  338. void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3){    
  339.             add_top_bar.setVisibility(View.VISIBLE);    
  340.             weibo_content.setVisibility(View.VISIBLE);    
  341.             operation_layout.setVisibility(View.GONE);    
  342.             operation_layout.removeAllViews();//别忘记要移除掉




  343.             String str = matchStrList.get(position);    
  344.             String nickStr = str.substring(0,str.indexOf("("));    
  345.             weibo_content.setText(weibo_content.getText()+"@"+nickStr);    
  346.         }    
  347.     }    
  348. }  
复制代码

Java代码





  1. public
  2. class AddWeiboActivity extends Activity implements OnClickListener{    



  3. private DataHelper dataHelper;    

  4. private UserInfo user;    

  5. private String user_default_name;    

  6. private MyWeiboSync weibo;    

  7. private ListView listView;    

  8. private EditText weibo_content;    

  9. private Button send_btn;    

  10. private Button add_cmamera_btn;    

  11. private Button add_at_btn;    

  12. private Button add_topic_btn;    

  13. private Button add_expression_btn;    

  14. private Button add_location_btn;    

  15. private GridView expressionGrid;    

  16. private List<Map<String,Object>> expressionList;    

  17. private ExpressionAdapter expressionAdapter;    

  18. private FrameLayout operation_layout;    

  19. private RelativeLayout add_top_bar;    



  20. private ListView atListView;    

  21. private RelativeLayout atRootLayout;    

  22. private EditText atEditText;    

  23. private Button atEnterBtn;    

  24. private TextView topic_tip;    



  25. private RelativeLayout.LayoutParams atEdiLayoutParams,atEnterBtnLayoutParams,atListViewLayoutParams,topicTipViewLayoutParams;    



  26. private JSONArray array;    

  27. private Handler handler;    

  28. private ArrayAdapter atAdapter;    

  29. private List<String> atList;    

  30. private AtThread thread;    

  31. private List<String> matchStrList;//选择atList匹配的字符串



  32. private
  33. int flag;    

  34. private
  35. static
  36. int FLAG_1 = 1;    

  37. private
  38. static
  39. int FLAG_2 = 2;//1和2代表atEnterBtn的父亲控件不同





  40. @Override


  41. protected
  42. void onCreate(Bundle savedInstanceState) {    

  43. super.onCreate(savedInstanceState);    
  44.         setContentView(R.layout.add_weibo);    


  45.         setUpViews();    
  46.         setUpListeners();    


  47.         dataHelper = DataBaseContext.getInstance(getApplicationContext());   
  48.         weibo = WeiboContext.getInstance();    


  49.         SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);    
  50.         user_default_name = preferences.getString("user_default_name", "");//取得微博默认登录账号信息




  51.         handler = new AtHandler();    
  52.         thread = new AtThread();    
  53.         thread.start();//开启一个线程获取数据


  54.     }    



  55. private
  56. void setUpViews(){    
  57.         weibo_content = (EditText)findViewById(R.id.weibo_content);    
  58.         send_btn = (Button)findViewById(R.id.send_btn);    
  59.         add_cmamera_btn = (Button)findViewById(R.id.add_cmamera_btn);    
  60.         add_at_btn = (Button)findViewById(R.id.add_at_btn);    
  61.         add_topic_btn = (Button)findViewById(R.id.add_topic_btn);    
  62.         add_expression_btn = (Button)findViewById(R.id.add_expression_btn);    
  63.         add_location_btn = (Button)findViewById(R.id.add_location_btn);    


  64.         add_top_bar = (RelativeLayout)findViewById(R.id.add_top_bar);    
  65.         operation_layout = (FrameLayout)findViewById(R.id.operation_layout);    
  66.         expressionGrid = new GridView(this);    
  67.         expressionGrid.setNumColumns(5);    
  68.         expressionList = buildExpressionsList();    
  69.         expressionAdapter = new ExpressionAdapter(AddWeiboActivity.this, expressionList);    
  70.         expressionGrid.setAdapter(expressionAdapter);    



  71. //以下代码至本方法setUpViews结束,是个人纯粹蛋疼联系纯代码布局,各位老大可以改成xml布局,淡定




  72.         atRootLayout = new RelativeLayout(AddWeiboActivity.this);    


  73.         atEditText = new EditText(AddWeiboActivity.this);    
  74.         atEditText.setId(10000);    


  75.         atEnterBtn = new Button(AddWeiboActivity.this);    
  76.         atEnterBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_enter_selector));    


  77.         atListView = new ListView(AddWeiboActivity.this);    
  78.         atListView.setCacheColorHint(Color.TRANSPARENT);//防止滑屏时出现黑快,不信可以注释掉此句试一试


  79.         atListView.setDivider(getResources().getDrawable(R.drawable.list_divider));//设置分割线


  80.         atListView.setBackgroundColor(Color.argb(255, 239, 239, 239));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的




  81.         topic_tip = new TextView(AddWeiboActivity.this);    
  82.         topic_tip.setText("请输入话题");    
  83.         topic_tip.setTextSize(20);    
  84.         topic_tip.setTextColor(Color.argb(255, 90, 142, 189));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的




  85.         atRootLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));    
  86.         atEdiLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,80);    
  87.         atEnterBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);    
  88.         atListViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);    
  89.         topicTipViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);    





  90. //添加布局约束


  91.         atEdiLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);    


  92.         atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);    
  93.         atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);    
  94.         atEnterBtnLayoutParams.setMargins(0, 10, 10, 0);//设置边距,分别代表左,上,右,下




  95.         atListViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);    
  96.         atListViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());    


  97.         topicTipViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());    


  98.     }    



  99. private
  100. void setUpListeners(){    
  101.         send_btn.setOnClickListener(this);    
  102.         add_cmamera_btn.setOnClickListener(this);    
  103.         add_at_btn.setOnClickListener(this);    
  104.         add_topic_btn.setOnClickListener(this);    
  105.         add_expression_btn.setOnClickListener(this);    
  106.         add_location_btn.setOnClickListener(this);    
  107.         expressionGrid.setOnItemClickListener(new GridItemClickListener());    
  108.         atListView.setOnItemClickListener(new AtListViewItemListener());    
  109.         atEditText.addTextChangedListener(new MyTextWatcher());    
  110.         atEnterBtn.setOnClickListener(new OnClickListener() {    

  111. @Override


  112. public
  113. void onClick(View v) {    
  114.                 add_top_bar.setVisibility(View.VISIBLE);    
  115.                 weibo_content.setVisibility(View.VISIBLE);    
  116.                 operation_layout.setVisibility(View.GONE);    
  117.                 operation_layout.removeAllViews();//别忘记要移除掉



  118. if(flag==FLAG_1){    
  119.                     weibo_content.setText(weibo_content.getText()+"@");    
  120.                 }else
  121. if(flag==FLAG_2){    
  122.                     weibo_content.setText(weibo_content.getText()+"#"+atEditText.getText()+"#");    
  123.                 }    




  124.             }    
  125.         });    
  126.     }    



  127. class AtThread extends Thread {    

  128. @Override


  129. public
  130. void run() {    
  131.             String jsonStr = weibo.getFans(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), 20, 0, user_default_name);    

  132. try {    
  133.                 JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");    
  134.                 array = dataObj.getJSONArray("info");    
  135.             } catch (JSONException e) {    
  136.                 e.printStackTrace();    
  137.             }    

  138. //通知handler处理数据


  139.             Message msg = handler.obtainMessage();    
  140.             handler.sendMessage(msg);    
  141.         }    
  142.     }    



  143. class AtHandler extends Handler {    

  144. @Override


  145. public
  146. void handleMessage(Message msg){    

  147. int size = array.length();    
  148.             atList = new ArrayList<String>();    

  149. for(int i = 0;i<size;i++){    
  150.                 JSONObject data = array.optJSONObject(i);    

  151. try {    
  152.                     atList.add(data.getString("nick")+"("+data.getString("name")+")");    
  153.                 } catch (JSONException e) {    
  154.                     e.printStackTrace();    
  155.                 }    
  156.             }    
  157.             matchStrList = new ArrayList<String>();    
  158.             matchStrList.addAll(atList);    
  159.             atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,atList);    
  160.             atListView.setAdapter(atAdapter);    
  161.         }    
  162.     }    



  163. class ExpressionAdapter extends BaseAdapter {    

  164. private Context context;    

  165. private LayoutInflater inflater;    

  166. private List<Map<String,Object>> list;    



  167. public ExpressionAdapter(Context context, List<Map<String,Object>> list) {   

  168. super();    

  169. this.context = context;    

  170. this.list = list;    

  171. this.inflater = LayoutInflater.from(context);    
  172.         }    



  173. @Override


  174. public
  175. int getCount() {    

  176. return list.size();    
  177.         }    



  178. @Override


  179. public Object getItem(int position) {    

  180. return list.get(position);    
  181.         }    



  182. @Override


  183. public
  184. long getItemId(int position) {    

  185. return position;    
  186.         }    



  187. @Override


  188. public View getView(final
  189. int position, View convertView, ViewGroup parent){    
  190.             Map<String,Object> map = list.get(position);    
  191.             ImageView image = new ImageView(context);    
  192.             image.setImageDrawable((Drawable)map.get("drawable"));    

  193. return image;    
  194.         }    
  195.     }    



  196. @Override


  197. public
  198. void onClick(View v) {    

  199. if(operation_layout.getChildCount()>0){    
  200.             add_top_bar.setVisibility(View.VISIBLE);    
  201.             weibo_content.setVisibility(View.VISIBLE);    
  202.             operation_layout.setVisibility(View.GONE);    
  203.             operation_layout.removeAllViews();//别忘记要移除掉



  204. return;    
  205.         }    

  206. switch (v.getId()) {    



  207. case R.id.send_btn:{    
  208.             String returnStr = weibo.publishMsg(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), weibo_content.getText().toString());    

  209. try {    
  210.                 JSONObject dataObj = new JSONObject(returnStr);    

  211. if("ok".equals(dataObj.getString("msg"))){    
  212.                     Toast.makeText(AddWeiboActivity.this, "发送成功", Toast.LENGTH_SHORT).show();//我日,记得要show,每次都搞忘记


  213.                 }    
  214.             } catch (JSONException e) {    
  215.                 e.printStackTrace();    
  216.             }    
  217.         }    

  218. break;    

  219. case R.id.add_cmamera_btn:{    


  220.         }    

  221. break;    

  222. case R.id.add_at_btn:{    

  223. // 动态的组装view


  224.             atRootLayout.removeAllViews();// 组装前先把所有的孩子拿掉


  225.             atEditText.setText("@");    
  226.             flag = FLAG_1;//区分atEnterBtn是在哪个界面按的


  227.             atRootLayout.addView(atEditText, atEdiLayoutParams);    
  228.             atRootLayout.addView(atEnterBtn, atEnterBtnLayoutParams);    
  229.             atRootLayout.addView(atListView, atListViewLayoutParams);    
  230.             operation_layout.addView(atRootLayout);    


  231.             add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响


  232.             weibo_content.setVisibility(View.GONE);    
  233.             operation_layout.setVisibility(View.VISIBLE);    
  234.         }    

  235. break;    

  236. case R.id.add_topic_btn:{    

  237. //动态的组装view


  238.             atRootLayout.removeAllViews();//组装前先把所有的孩子拿掉


  239.             atEditText.setText("");    
  240.             flag = FLAG_2;//区分atEnterBtn是在哪个界面按的


  241.             atRootLayout.addView(atEditText,atEdiLayoutParams);    
  242.             atRootLayout.addView(atEnterBtn,atEnterBtnLayoutParams);    
  243.             atRootLayout.addView(topic_tip,topicTipViewLayoutParams);    
  244.             operation_layout.addView(atRootLayout);    


  245.             add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响


  246.             weibo_content.setVisibility(View.GONE);    
  247.             operation_layout.setVisibility(View.VISIBLE);    
  248.         }    

  249. break;    

  250. case R.id.add_expression_btn:{    
  251.             add_top_bar.setVisibility(View.GONE);//隐藏上面的bar和文本编辑框,不让之与表情选择的gridView相互影响


  252.             weibo_content.setVisibility(View.GONE);    
  253.             operation_layout.addView(expressionGrid);    
  254.             operation_layout.setVisibility(View.VISIBLE);    
  255.         }    

  256. break;    

  257. case R.id.add_location_btn:{    


  258.         }    

  259. break;    

  260. default:    

  261. break;    
  262.         }    
  263.     }    

  264. private List<Map<String,Object>> buildExpressionsList(){    
  265.         List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();    
  266.         DecimalFormat df = new DecimalFormat("000");//格式化数字



  267. for(int i = 0;i<105;i++){    
  268.             Map<String,Object> map = new HashMap<String, Object>();    
  269.             String formatStr = "h"+df.format(i);    

  270. int drawableId = 0 ;    

  271. try {    
  272.                 drawableId = R.drawable.class.getDeclaredField(formatStr).getInt(this);//反射取得id,这个地方循环套反射,是不是很耗性能啊,我没测试过,麻烦有好办法的兄弟姐妹分享一下


  273.             } catch (IllegalArgumentException e) {    
  274.                 e.printStackTrace();    
  275.             } catch (SecurityException e) {    
  276.                 e.printStackTrace();    
  277.             } catch (IllegalAccessException e) {    
  278.                 e.printStackTrace();    
  279.             } catch (NoSuchFieldException e) {    
  280.                 e.printStackTrace();    
  281.             }    
  282.             Drawable drawable = getResources().getDrawable(drawableId);    
  283.             map.put("drawableId", formatStr);    
  284.             map.put("drawable",drawable);    
  285.             list.add(map);    
  286.         }    

  287. return list;    
  288.     }    



  289. class GridItemClickListener implements OnItemClickListener {    

  290. @Override


  291. public
  292. void onItemClick(AdapterView<?> adapterView, View view, int position,long arg3) {    
  293.             Map<String, Object> map = expressionList.get(position);    
  294.             String drawableId = (String)map.get("drawableId");    


  295.             add_top_bar.setVisibility(View.VISIBLE);    
  296.             weibo_content.setVisibility(View.VISIBLE);    
  297.             operation_layout.setVisibility(View.GONE);    
  298.             operation_layout.removeAllViews();//别忘记要移除掉




  299.             String expressionStr=null;    
  300.             expressionStr = TextUtil.drawableIdToFaceName.get(drawableId);   
  301.             expressionStr="/"+expressionStr;    
  302.             weibo_content.setText(weibo_content.getText().toString()+expressionStr);    
  303.         }    
  304.     }    



  305. class MyTextWatcher implements TextWatcher{    

  306. @Override


  307. public
  308. void afterTextChanged(Editable s){    
  309.             String changingStr = atEditText.getText().toString();    

  310. if(changingStr.indexOf("@")!=-1){    
  311.                 changingStr = changingStr.substring(1);    
  312.             }    



  313. int size = atList.size();    
  314.             matchStrList.clear();    

  315. for(int i = 0;i<size;i++){    
  316.                 String currentStr = atList.get(i);    

  317. if(currentStr.indexOf(changingStr)!=-1){    
  318.                     matchStrList.add(currentStr);    
  319.                 }    
  320.             }    
  321.             atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,matchStrList);    
  322.             atAdapter.notifyDataSetChanged();    
  323.             atListView.setAdapter(atAdapter);    
  324.         }    



  325. @Override


  326. public
  327. void beforeTextChanged(CharSequence s, int start, int count,    

  328. int after) {    


  329.         }    



  330. @Override


  331. public
  332. void onTextChanged(CharSequence s, int start, int before,int count) {    


  333.         }    
  334.     }    



  335. class AtListViewItemListener implements OnItemClickListener{    

  336. @Override


  337. public
  338. void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3){    
  339.             add_top_bar.setVisibility(View.VISIBLE);    
  340.             weibo_content.setVisibility(View.VISIBLE);    
  341.             operation_layout.setVisibility(View.GONE);    
  342.             operation_layout.removeAllViews();//别忘记要移除掉




  343.             String str = matchStrList.get(position);    
  344.             String nickStr = str.substring(0,str.indexOf("("));    
  345.             weibo_content.setText(weibo_content.getText()+"@"+nickStr);    
  346.         }    
  347.     }    
  348. }  
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义相机是一种使用相机API来定制自己的相机界面或开发特殊相机功能的方法。它适用于需要对照片进行裁剪、滤镜处理、添加贴纸、表情、地点标签等操作的场景。相比直接通过Intent调用系统相机组件,自定义相机能够提供更多的灵活性和可定制性。在使用自定义相机之前,需要进行系统权限配置,包括相机权限、文件读写权限以及相机自动对焦配置。同时,应该检查设备是否具备相机功能,以避免在没有相机的设备上使用相关功能。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Androd自定义控件(五)打造自己的Camera](https://blog.csdn.net/sdkfjksf/article/details/51137408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Android自定义相机—Camera篇](https://blog.csdn.net/m0_56146626/article/details/128595800)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值