android自定义控件---添加表情
android自定义控件---添加表情
一、定义layout文件,图片不提供了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="0dp"
android:orientation="vertical" > <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="140dip"
android:layout_gravity="center"
android:background="#ffffff"/> <LinearLayout
android:id="@+id/page_select"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ddd"
android:gravity="center_horizontal" > <ImageView
android:id="@+id/page0_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="@drawable/page_focused" /> <ImageView
android:id="@+id/page1_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/page_unfocused" /> <ImageView
android:id="@+id/page2_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/page_unfocused" />
</LinearLayout>
<!-- 表情 --> <LinearLayout
android:id="@+id/page_select_gif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#aaa"
android:gravity="left"> <ImageView
android:id="@+id/page_normal_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="@drawable/icon_smile"
android:layout_marginLeft="10dp" /> <ImageView
android:id="@+id/page_gif_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:scaleType="matrix"
android:src="@drawable/icon_pic" />
</LinearLayout>
</LinearLayout>
java类:
public class ExpressionView extends LinearLayout {
// 表情
// private Activity activity;
private EditText msgEditText;
private ViewPager viewPager;
private ArrayList<GridView> grids;
private int[] expressionImages;
private String[] expressionImageNames;
private int[] expressionImages1;
private String[] expressionImageNames1;
private int[] expressionImages2;
private String[] expressionImageNames2;
private ImageView page0;
private ImageView page1;
private ImageView page2;
private GridView gView1;
private GridView gView2;
private GridView gView3; @SuppressLint("NewApi")
public ExpressionView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
} public ExpressionView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
} public ExpressionView(Context context) {
super(context);
init(context);
} private void init(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.expression_view, this); page0 = (ImageView) findViewById(R.id.page0_select);
page1 = (ImageView) findViewById(R.id.page1_select);
page2 = (ImageView) findViewById(R.id.page2_select);
// 引入表情
expressionImages = Expressions.expressionImgs;
expressionImageNames = Expressions.expressionImgNames;
expressionImages1 = Expressions.expressionImgs1;
expressionImageNames1 = Expressions.expressionImgNames1;
expressionImages2 = Expressions.expressionImgs2;
expressionImageNames2 = Expressions.expressionImgNames2;
// 创建ViewPager
viewPager = (ViewPager) findViewById(R.id.viewpager);
initViewPager();
} public void setEditText(EditText msgEditText){
this.msgEditText = msgEditText;
} // 表情
private void initViewPager() {
LayoutInflater inflater = LayoutInflater.from(getContext());
grids = new ArrayList<GridView>();
gView1 = (GridView) inflater.inflate(R.layout.grid1, null); setPage(page0, gView1, expressionImages, expressionImageNames);
grids.add(gView1); gView2 = (GridView) inflater.inflate(R.layout.grid2, null);
grids.add(gView2); gView3 = (GridView) inflater.inflate(R.layout.grid3, null);
grids.add(gView3); // 填充ViewPager的数据适配器
PagerAdapter mPagerAdapter = new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
} @Override
public int getCount() {
return grids.size();
} @Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(grids.get(position));
} @Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(grids.get(position));
return grids.get(position);
} @Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub } @Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub } @Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
} @Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub } };
viewPager.setAdapter(mPagerAdapter);
viewPager.setOnPageChangeListener(new GuidePageChangeListener());
} // ** 指引页面改监听器 */
class GuidePageChangeListener implements OnPageChangeListener { @Override
public void onPageScrollStateChanged(int arg0) {
} @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
} @Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
page0.setImageDrawable(getResources().getDrawable(
R.drawable.page_focused));
page1.setImageDrawable(getResources().getDrawable(
R.drawable.page_unfocused));
break;
case 1:
setPage(page1, gView2, expressionImages1, expressionImageNames1);
break;
case 2:
setPage(page2, gView3, expressionImages2, expressionImageNames2);
break; }
}
} public void setPage(ImageView pageFocused, GridView gridView,
final int[] expressionImages, final String[] expressionImageNames) {
page0.setImageDrawable(getResources().getDrawable(
R.drawable.page_unfocused));
page1.setImageDrawable(getResources().getDrawable(
R.drawable.page_unfocused));
page2.setImageDrawable(getResources().getDrawable(
R.drawable.page_unfocused));
pageFocused.setImageDrawable(getResources().getDrawable(
R.drawable.page_focused));
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
// 生成24个表情
for (int i = 0; i < 24; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("image", expressionImages[i]);
listItems.add(listItem);
} SimpleAdapter simpleAdapter1 = new SimpleAdapter(getContext(), listItems,
R.layout.singleexpression, new String[] { "image" },
new int[] { R.id.image });
gridView.setAdapter(simpleAdapter1);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeResource(getResources(),
expressionImages[arg2 % expressionImages.length]);
ImageSpan imageSpan = new ImageSpan(getContext(), bitmap);
SpannableString spannableString = new SpannableString(
expressionImageNames[arg2]);
spannableString.setSpan(imageSpan, 0,
expressionImageNames[arg2].length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// 编辑框设置数据
msgEditText.append(spannableString);
}
});
} }
使用方法:
在要使用的activitiy的布局文件中加入:
<****.ExpressionView
android:id="@+id/expression_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
在activity的java文件中:
ExpressionView expressionView = (ExpressionView) findViewById(R.id.expression_view);
expressionView.setEditText(msgEditText);
要弹表情时
expressionView.setVisibility(View.VISIBLE);
表情类: 表情资源自己找,命名格式 f000-f071共三页,3*9=27每页
public class Expressions {
public static int[] expressionImgs = new int[] { R.drawable.f000,
R.drawable.f001, R.drawable.f002, R.drawable.f003, R.drawable.f004,
R.drawable.f005, R.drawable.f006, R.drawable.f007, R.drawable.f008,
R.drawable.f009, R.drawable.f010, R.drawable.f011, R.drawable.f012,
R.drawable.f013, R.drawable.f014, R.drawable.f015, R.drawable.f016,
R.drawable.f017, R.drawable.f018, R.drawable.f019, R.drawable.f020,
R.drawable.f021, R.drawable.f022, R.drawable.f023 }; /**
* 本地表情的名字1
*/
public static String[] expressionImgNames = new String[] { "[/f000]",
"[/f001]", "[/f002]", "[/f003]", "[/f004]", "[/f005]", "[/f006]",
"[/f007]", "[/f008]", "[/f009]", "[/f010]", "[/f011]", "[/f012]",
"[/f013]", "[/f014]", "[/f015]", "[/f016]", "[/f017]", "[/f018]",
"[/f019]", "[/f020]", "[/f021]", "[/f022]", "[/f023]" }; public static int[] expressionImgs1 = new int[] { R.drawable.f024,
R.drawable.f025, R.drawable.f026, R.drawable.f027, R.drawable.f028,
R.drawable.f029, R.drawable.f030, R.drawable.f031, R.drawable.f032,
R.drawable.f033, R.drawable.f034, R.drawable.f035, R.drawable.f036,
R.drawable.f037, R.drawable.f038, R.drawable.f039, R.drawable.f040,
R.drawable.f041, R.drawable.f042, R.drawable.f043, R.drawable.f044,
R.drawable.f045, R.drawable.f046, R.drawable.f047 }; /**
* 本地表情的名字2
*/
public static String[] expressionImgNames1 = new String[] { "[/f024]",
"[/f025]", "[/f026]", "[/f027]", "[/f028]", "[/f029]", "[/f030]",
"[/f031]", "[/f032]", "[/f033]", "[/f034]", "[/f035]", "[/f036]",
"[/f037]", "[/f038]", "[/f039]", "[/f040]", "[/f041]", "[/f042]",
"[/f043]", "[/f044]", "[/f045]", "[/f046]", "[/f047]" }; public static int[] expressionImgs2 = new int[] { R.drawable.f048,
R.drawable.f049, R.drawable.f050, R.drawable.f051, R.drawable.f052,
R.drawable.f053, R.drawable.f054, R.drawable.f055, R.drawable.f056,
R.drawable.f057, R.drawable.f058, R.drawable.f059, R.drawable.f060,
R.drawable.f061, R.drawable.f062, R.drawable.f063, R.drawable.f064,
R.drawable.f065, R.drawable.f066, R.drawable.f067, R.drawable.f068,
R.drawable.f069, R.drawable.f070, R.drawable.f071 }; /**
* 本地表情的名字3
*/
public static String[] expressionImgNames2 = new String[] { "[/f048]",
"[/f049]", "[/f050]", "[/f051]", "[/f052]", "[/f053]", "[/f054]",
"[/f055]", "[/f056]", "[/f057]", "[/f058]", "[/f059]", "[/f060]",
"[/f061]", "[/f062]", "[/f063]", "[/f064]", "[/f065]", "[/f066]",
"[/f067]", "[/f068]", "[/f069]", "[/f070]", "[/f071]" }; }
有不懂的可以留言
android自定义控件---添加表情的更多相关文章
- EditText添加表情
package com.kale.edittext02; import java.lang.reflect.Field; import java.util.Random; import android ...
- Android自定义控件之自定义ViewGroup实现标签云
前言: 前面几篇讲了自定义控件绘制原理Android自定义控件之基本原理(一),自定义属性Android自定义控件之自定义属性(二),自定义组合控件Android自定义控件之自定义组合控件(三),常言 ...
- Android自定义控件之自定义属性
前言: 上篇介绍了自定义控件的基本要求以及绘制的基本原理,本篇文章主要介绍如何给自定义控件自定义一些属性.本篇文章将继续以上篇文章自定义圆形百分比为例进行讲解.有关原理知识请参考Android自定义控 ...
- Android自定义控件1
概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...
- 一起来学习Android自定义控件1
概述 Android已经为我们提供了大量的View供我们使用,但是可能有时候这些组件不能满足我们的需求,这时候就需要自定义控件了.自定义控件对于初学者总是感觉是一种复杂的技术.因为里面涉及到的知识点会 ...
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
android自定义styleableattrs源码 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行配置,于是便查询了解了下declare-styleabl ...
- 【转】android 自定义控件 使用declare-styleable进行配置属性(源码角度)
原文网址:http://blog.csdn.net/vipzjyno1/article/details/23696537 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行 ...
- Android自定义控件 开源组件SlidingMenu的项目集成
在实际项目开发中,定制一个菜单,能让用户得到更好的用户体验,诚然菜单的样式各种各样,但是有一种菜单——滑动菜单,是被众多应用广泛使用的.关于这种滑动菜单的实现,我在前面的博文中也介绍了如何自定义去实现 ...
- Android自定义控件系列之应用篇——圆形进度条
一.概述 在上一篇博文中,我们给大家介绍了Android自定义控件系列的基础篇.链接:http://www.cnblogs.com/jerehedu/p/4360066.html 这一篇博文中,我们将 ...
随机推荐
- Lake Counting(poj 2386)
题目描述: Description Due to recent rains, water has pooled in various places in Farmer John's field, wh ...
- cygwin org/apache/zookeeper/KeeperException
以前用cdh3-0.20的hbase,在windows下面直接启动就行了,但是最近安装0.94以上的,就不行了. 报标题的错误,搜遍网络,几乎都是要加HBASE_CLASSPATH的,后来看老外的文章 ...
- repeater一个简单的用法例子
(前台) <asp:Repeater ID="Repeater1" runat="server" onitemdatabound=" ...
- MyEclipse2014不支持jre1.8吗
myeclipse 2015才支持了java 8 也可以用Eclipse Kepler加插件的形式来支持java 8
- C++惯用法:通过成员模板实现隐式转换(Coercion 强迫 by Member Template)
Intent To increase the flexibility of a class template's interface by allowing the class template to ...
- 怎样在VirtualBox 虚拟机中挂载共享目录
啊.好长时间没写博客了.近期有点忙~~ 不得不说 VirtualBox 对于一些不想装非常多个系统又非常想实验新系统的人来说确实是神器: 哈哈.个人还是比較爱玩这些个各种各样的Linux 发型版的,可 ...
- BestCoder Round #50 (div.1) 1002 Run (HDU OJ 5365) 暴力枚举+正多边形判定
题目:Click here 题意:给你n个点,有多少个正多边形(3,4,5,6). 分析:整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可. #include ...
- 网络编程TCP协议-聊天室
网络编程TCP协议-聊天室(客户端与服务端的交互); <span style="font-size:18px;">1.客户端发数据到服务端.</span> ...
- Python 第十三篇之一:前端页面 js和dome
一:JavaScript: JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的 ...
- AppStore安装APP发生错误解决方法
打开网络偏好设置 高级 -> DNS -> + -> 114.114.114.114