android 自定义组件-带图片的textView
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>
android 自定义组件-带图片的textView的更多相关文章
- Android自定义组件之自动换行及宽度自适应View:WordWrapView
目的: 自定义一个ViewGroup,里面的子view都是TextView,每个子view TextView的宽度随内容自适应且每行的子View的个数自适应,并可以自动换行 一:效果图 二:代码 整 ...
- Android自定义组件系列【5】——进阶实践(2)
上一篇<Android自定义组件系列[5]--进阶实践(1)>中对任老师的<可下拉的PinnedHeaderExpandableListView的实现>前一部分进行了实现,这一 ...
- Android自定义组件系列【7】——进阶实践(4)
上一篇<Android自定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识,这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpan ...
- Android自定义组件系列【6】——进阶实践(3)
上一篇<Android自定义组件系列[5]--进阶实践(2)>继续对任老师的<可下拉的PinnedHeaderExpandableListView的实现>进行了分析,这一篇计划 ...
- Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动
在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...
- Android 自定义组件之如何实现自定义组件
参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699 简介 Android提供了用于构建UI的强大的组件模型.两个基类:View和 ...
- Android 自定义组件,自定义LinearLayout,ListView等样式的组件
今天讲的其实以前自己用过,就是在网上拿下来的把图片裁剪成圆形的方法,之前的随笔也介绍过的, 用法就是,在布局里写控件或者组件的时候得把从com开始到你写的那个类的所有路径写下来. 至于我们该怎么创建呢 ...
- Android自定义组件
[参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...
- Android自定义组件系列【3】——自定义ViewGroup实现侧滑
有关自定义ViewGroup的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...
随机推荐
- git的安装使用和代码自动部署
1.安装 http://www.cnblogs.com/sunada2005/archive/2013/06/06/3121098.html http://www.cnblogs.com/zhcncn ...
- 10个免费开源的JS音乐播放器插件
点这里 音乐播放器在网页设计中有时候会用到,比如一些时尚类.音乐或影视类等项目,但这些 网页播放器 插件比较少见,所以这里为大家整理一个集合,也许会有用到的时候. 下面整理的播放器有些是支持自适应的, ...
- window内存管理与内存原理
转自: http://blog.csdn.net/iamfranter/article/details/6826270 WIndows为每个进程分配了4GB的虚拟地址空间,让每个进程都认为自己拥有4G ...
- oci.dll文件是用来干嘛的? 如果没有安装ORACLE客户端提示oci.dll未加载
oracle数据库开发编程中,没有找到oci.dll,一般是系统的 path 设置有问题, 查找oci.dll, 然后加入到系统路径.oci.dll 可下载解压到系统盘的system32目录下.然后打 ...
- Project Euler 109 :Darts 飞镖
Darts In the game of darts a player throws three darts at a target board which is split into twenty ...
- React组件生命周期-正确执行运行阶段的函数
一. 二. <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...
- WordPress主题制作教程9:文章形式
wordpress的文章形式: aside -----------------日志:不显示标题的标准文章image --------------图像:单张图像.文章中的首个 <img /> ...
- 注册表修改PSD关联photoshop
当psd文件右键点击--打开方式--选择默认程序photoshop也没用的时候,那应该是注册表未关联,可以试下以下方法: 第一步:在运行框中输入regedit,打开注册表编辑器,将HKEY_CLASS ...
- 《Java编程那点事儿》读书笔记(五)——System,Integer,Calendar,Random和容器
System 1)arraycopy int[] a = {1.2.3.4}; int[] b = new int[5]; System.arraycopy(a,1,b,3,2); //把数组a中从下 ...
- linux系统的文件和文件类型
文件 当你创建一个文件的时候,系统保存了有关该文件的全部信息,包括:• 文件的位置.• 文件类型.• 文件长度.• 哪位用户拥有该文件,哪些用户可以访问该文件.• i节点.• 文件的修改时间.• 文件 ...