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的文章已经很多了,我为什么写这篇文章,对于初学者或者对自定义组件比较生疏的朋友虽然可以拿来主义的用了,但是要一步一步的实现和了解其中的过程和原理才能真真脱离别人的代码,举一 ...
随机推荐
- POJ 2075
#include<iostream> #include<stdio.h> #include<string> #include<map> #include ...
- SQL技术内幕-13 SQL优化方法论之分析实例级别的等待
优化方法论的第一步是在实例级别上找出什么类型的等待占用了大部分的等待时间,这可以通过查询动态管理图(DMV,dynamic management view)sys.dm_os_wait_stats 运 ...
- BZOJ 1143 1143: [CTSC2008]祭祀river 最长反链
1143: [CTSC2008]祭祀river Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动. ...
- js对象小结
前奏 对象是js的基本数据类型,准确来说除了字符串,数字,boolean值,null与undifine之外,js中的值都是对象.js中的对象是一种复合值,他将很多值(原始值或其他对象)聚合在一起,可以 ...
- linux系统下挂载windows共享目录
在工作中有时我们需要在linux上挂载windows共享目录.首先我们需要学会在linux上查看windows共享了那些目录.查看操作需要安装samba-client. [root@ ~]# yum ...
- 如何学好oracle?(准备)
循序渐进 多练习 http://www.tudou.com/listplay/ScoGxMJZGQc/Nw9HE62XiGo.html
- 李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView
李洪强iOS开发之后使用纯代码实现横向滚动的UIScrollView (VTmagic是一个实现左右滚动的控制器的框架,也可以实现此功能) 实现的效果: 01 - 创建四个控制器 02 - 定义需要 ...
- Facebook揭密:如何让MySQL数据库集群自主运行
Facebook运行着全球最大的MySQL数据库集群,该集群分布在两个大洲上的多个数据中心中数以千计的服务器上.让人不解的是,Facebook只动用了一个很小的团队来管理这个庞大的MySQL数据库集群 ...
- Python概述_软件安装_常见问题
1. Python安装 目前python有2个大版本,2和3,由于2和3语法有差别,现有的许多库都是基于python2开发,本系列文章以python2为主. 1.1 重要概念 1. 动态语言 运行 ...
- WebSphere常用设置
WebSphere常用设置 1.查看环境配置信息D:\Program Files\IBM\WebSphere\AppServer\profiles\AppSrv01\logs\AboutThisPro ...