1. 定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="icon_textview">
<attr name="iconSrc" format="reference"/>
</declare-styleable> <attr name="CustomizeStyle" format="reference" />
</resources>

2. 继承View : CustomTextView.java

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView; /**
* TODO: document your custom TextView class.
*/
public class CustomTextView extends TextView { private static final String TAG = CustomTextView.class.getSimpleName();
private Bitmap bitmap; public CustomTextView(Context context) {
super(context);
} public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.CustomizeStyle);
} public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.icon_textview);
int rsid = a.getResourceId(R.styleable.icon_textview_iconSrc,0);
if (rsid>0) {
bitmap = BitmapFactory.decodeResource(getResources(), rsid);
}
a.recycle(); } @Override
protected void onDraw(Canvas canvas) { RectBitmap(canvas); super.onDraw(canvas);
} public void RectBitmap(Canvas canvas) { if (bitmap != null) { //是否对原图片进行裁切
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); //显示在什么地方
Rect taget = new Rect();
//起点x的坐标
taget.left=0;
//起点Y的坐标:当前View的高度-字体实际占用的高度)/2 能保证图片与文字对齐
taget.top=(int)(getMeasuredHeight()-getTextSize())/2+1; // System.out.println("getMeasuredHeight:"+getMeasuredHeight());
// System.out.println("getTextSize:"+getTextSize());
//保证图片等比缩放:X的坐标
taget.right = (int)(getTextSize() * (bitmap.getWidth() / (float)bitmap.getHeight()));
//Y的坐标
taget.bottom= (int) (taget.top+getTextSize()); canvas.drawBitmap(bitmap, rect, taget, getPaint()); canvas.translate(taget.right+2 , 0.5f);
} }
}

3:布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bingatt="http://schemas.android.com/apk/res/包名" //加入包名
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"> <demo.bing.customstyle.CustomTextView
android:id="@+id/icon36"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我的表情符号36"
android:textSize="36sp"
bingatt:iconSrc="@drawable/icon"
/> <demo.bing.customstyle.CustomTextView
android:id="@+id/icon24"
android:layout_below="@id/icon36"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我的表情符号24"
android:textSize="24sp"
bingatt:iconSrc="@drawable/icon"
/> <demo.bing.customstyle.CustomTextView
android:layout_below="@id/icon24"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我的表情符号12"
android:textSize="12sp"
bingatt:iconSrc="@drawable/icon"
/>
</RelativeLayout>

最新文章

  1. 分页插件思想:pc加载更多功能和移动端下拉刷新加载数据
  2. Oracle相关账户几个语句
  3. 深入剖析 redis RDB 持久化策略
  4. 错误异常 (1)Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly
  5. 【OpenStack】OpenStack系列5之Cinder详解
  6. (转)SQL server 容易让人误解的问题之 聚集表的物理顺序问题
  7. (转)Attention
  8. oracle 11g 分区表
  9. JDBC使用sql语句
  10. SQL Server 的 6 种隔离级别
  11. 不同Activity之间传递线程
  12. PAT1006:Sign In and Sign Out
  13. Java开源生鲜电商平台-性能优化以及服务器优化的设计与架构(源码可下载)
  14. Hadoop入门
  15. MySQL指令笔记
  16. 4.7 Sublime Text3 中配置 Python环境 --之上安装Sublime 3
  17. ES6-课程介绍
  18. Java进阶之路
  19. Oracle 启用归档
  20. java经典开发模式

热门文章

  1. HDU 1393 Weird Clock (英语,纪念题)
  2. Learn CSS
  3. linux下crontab实现定时服务详解
  4. CKEditor上传图片—配置CKFinder
  5. 欧拉工程第55题:Lychrel numbers
  6. PHP组合模式
  7. STM32F05x加入RDP(LV1)后,Segger无法Unlock的解决办法
  8. ThinkPHP:读取数据库数据 (2)
  9. Android百度地图开发01之初体验
  10. DWR3.0 dwr 返回值(数组,集合,Map)