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的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...
随机推荐
- Java 序列化的高级认识
序列化 ID 问题 情境:两个客户端 A 和 B 试图通过网络传递对象数据,A 端将对象 C 序列化为二进制数据再传给 B,B 反序列化得到 C. 问题:C 对象的全类路径假设为 com.inout. ...
- android 启动adb
1.命令行进入 sdk/platform-tools 2.执行命令 adb kill-server 3.执行命令 adb start-server
- JavaWeb项目开发案例精粹-第6章报价管理系统-002辅助类及配置文件
1. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...
- Python模块包中__init__.py文件的作用
转载自:http://hi.baidu.com/tjuer/item/ba37ac4ce7482a0f6dc2f08b 模块包: 包通常总是一个目录,目录下为首的一个文件便是 __init__.py. ...
- MVC新手指南
MVC新手指南 2010-04-06 09:54:23 18839 次阅读 0 条评论 本文感谢东西提供 模型-视图-控制器(MVC)可能是近年来网络编程圈子里最常被提及的模式之一.目前与网络应用 ...
- Android笔记——导入Github开源项目CircleRefreshLayout
百度n久都找不到android studio导入第三方类库的正确方法,纠结睡不着 ,最后终于蒙到了方法,原来想太多了 ---------------------------------------- ...
- Hibernate笔记——hql总结
原文:http://www.cnblogs.com/xiaoluo501395377/p/3376256.html ------------------------------------------ ...
- angularjs transclude demo
<!doctype html> <html lang="en" ng-app="expanderModule"> <head> ...
- spring mvc 导出 excel
// js 触发导出 excel 方法 导出当前页的数据 含有条件查询的结果 // js 框架使用的 是 easyui function doExport(){ var optins = $(&quo ...
- [Topcoder]AvoidRoads(dp,hash)
题目连接:https://community.topcoder.com/stat?c=problem_statement&pm=1889&rd=4709 题意:给一张n*m的地图,上面 ...