文章目录

android自定义控件—添加表情

一、定义layout文件,图片不提供了

\

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quot;
xmlns:tools="http://schemas.android.com/tools&quot;
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="0dp"
android:orientation="vertical" >

 &lt;android.support.v4.view.ViewPager
          android:id=&quot;@+id/viewpager&quot;
          android:layout_width=&quot;fill_parent&quot;
          android:layout_height=&quot;140dip&quot;
          android:layout_gravity=&quot;center&quot;
          android:background=&quot;#ffffff&quot;/&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/page_select&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;#ddd&quot;
        android:gravity=&quot;center_horizontal&quot; &gt;

        &lt;ImageView
            android:id=&quot;@+id/page0_select&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:scaleType=&quot;matrix&quot;
            android:src=&quot;@drawable/page_focused&quot; /&gt;

        &lt;ImageView
            android:id=&quot;@+id/page1_select&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;10dp&quot;
            android:scaleType=&quot;matrix&quot;
            android:src=&quot;@drawable/page_unfocused&quot; /&gt;

        &lt;ImageView
            android:id=&quot;@+id/page2_select&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;10dp&quot;
            android:scaleType=&quot;matrix&quot;
            android:src=&quot;@drawable/page_unfocused&quot; /&gt;
    &lt;/LinearLayout&gt;
&lt;!-- 表情 --&gt;

 &lt;LinearLayout
        android:id=&quot;@+id/page_select_gif&quot;
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;#aaa&quot;
        android:gravity=&quot;left&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/page_normal_select&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:scaleType=&quot;matrix&quot;
            android:src=&quot;@drawable/icon_smile&quot;
            android:layout_marginLeft=&quot;10dp&quot; /&gt;

        &lt;ImageView
            android:id=&quot;@+id/page_gif_select&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;10dp&quot;
            android:scaleType=&quot;matrix&quot;
            android:src=&quot;@drawable/icon_pic&quot; /&gt;
    &lt;/LinearLayout&gt;

</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(&quot;NewApi&quot;)
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&lt;GridView&gt;();
    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&lt;Map&lt;String, Object&gt;&gt; listItems = new ArrayList&lt;Map&lt;String, Object&gt;&gt;();
    // 生成24个表情
    for (int i = 0; i &lt; 24; i++) {
        Map&lt;String, Object&gt; listItem = new HashMap&lt;String, Object&gt;();
        listItem.put(&quot;image&quot;, expressionImages[i]);
        listItems.add(listItem);
    }

    SimpleAdapter simpleAdapter1 = new SimpleAdapter(getContext(), listItems,
            R.layout.singleexpression, new String[] { &quot;image&quot; },
            new int[] { R.id.image });
    gridView.setAdapter(simpleAdapter1);
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView&lt;?&gt; 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[] { &quot;[/f000]&quot;,
        &quot;[/f001]&quot;, &quot;[/f002]&quot;, &quot;[/f003]&quot;, &quot;[/f004]&quot;, &quot;[/f005]&quot;, &quot;[/f006]&quot;,
        &quot;[/f007]&quot;, &quot;[/f008]&quot;, &quot;[/f009]&quot;, &quot;[/f010]&quot;, &quot;[/f011]&quot;, &quot;[/f012]&quot;,
        &quot;[/f013]&quot;, &quot;[/f014]&quot;, &quot;[/f015]&quot;, &quot;[/f016]&quot;, &quot;[/f017]&quot;, &quot;[/f018]&quot;,
        &quot;[/f019]&quot;, &quot;[/f020]&quot;, &quot;[/f021]&quot;, &quot;[/f022]&quot;, &quot;[/f023]&quot; };

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[] { &quot;[/f024]&quot;,
    &quot;[/f025]&quot;, &quot;[/f026]&quot;, &quot;[/f027]&quot;, &quot;[/f028]&quot;, &quot;[/f029]&quot;, &quot;[/f030]&quot;,
    &quot;[/f031]&quot;, &quot;[/f032]&quot;, &quot;[/f033]&quot;, &quot;[/f034]&quot;, &quot;[/f035]&quot;, &quot;[/f036]&quot;,
    &quot;[/f037]&quot;, &quot;[/f038]&quot;, &quot;[/f039]&quot;, &quot;[/f040]&quot;, &quot;[/f041]&quot;, &quot;[/f042]&quot;,
    &quot;[/f043]&quot;, &quot;[/f044]&quot;, &quot;[/f045]&quot;, &quot;[/f046]&quot;, &quot;[/f047]&quot; };

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[] { &quot;[/f048]&quot;,
    &quot;[/f049]&quot;, &quot;[/f050]&quot;, &quot;[/f051]&quot;, &quot;[/f052]&quot;, &quot;[/f053]&quot;, &quot;[/f054]&quot;,
    &quot;[/f055]&quot;, &quot;[/f056]&quot;, &quot;[/f057]&quot;, &quot;[/f058]&quot;, &quot;[/f059]&quot;, &quot;[/f060]&quot;,
    &quot;[/f061]&quot;, &quot;[/f062]&quot;, &quot;[/f063]&quot;, &quot;[/f064]&quot;, &quot;[/f065]&quot;, &quot;[/f066]&quot;,
    &quot;[/f067]&quot;, &quot;[/f068]&quot;, &quot;[/f069]&quot;, &quot;[/f070]&quot;, &quot;[/f071]&quot; };

}

有不懂的可以留言

  
	
文章目录