IconTextView是一个带图标的TextView,从TextView继承。
IconTextView类:
package com.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
public class IconTextView extends TextView {
private Bitmap bitmap;
public IconTextView(Context context, AttributeSet attrs) {
super(context, attrs);
int srcId = attrs.getAttributeResourceValue(null, "iconSrc", 0);
if(srcId > 0) {
bitmap = BitmapFactory.decodeResource(getResources(), srcId);
}
}
@Override
protected void onDraw(Canvas canvas) {
if(bitmap != null) {
//从原图上截取区域
Rect src = new Rect();
//目标区域
Rect target = new Rect();
src.left = 0;
src.top = 0;
src.right = bitmap.getWidth();
src.bottom= bitmap.getHeight();
int textHigh = (int)getTextSize();
target.left = 0;
target.top = (int)((getMeasuredHeight()-getTextSize())/2)+1;
target.bottom = target.top + textHigh;
target.right = (int)(textHigh * (bitmap.getWidth()/(float)bitmap.getHeight()));
canvas.drawBitmap(bitmap, src, target, getPaint());
canvas.translate(target.right+2, 0);
}
super.onDraw(canvas);
}
}
效果图:
mian.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.view.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我只能够呵呵"
android:textSize="20px"
iconSrc="@drawable/hehe"
/>
<com.view.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我只能够呵呵"
android:textSize="30px"
/>
<com.view.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我只能够呵呵"
android:textSize="40px"
iconSrc="@drawable/hehe"
/>
<com.view.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我只能够呵呵"
android:textSize="50px"
iconSrc="@drawable/hehe"
/>
<com.view.IconTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我只能够呵呵"
android:textSize="60px"
iconSrc="@drawable/hehe"
/>
</LinearLayout>
- 大小: 22 KB
分享到:
相关推荐
Android自定义控件---联系人列表A-Z排序 此demo的博客地址:http://blog.csdn.net/u012814441
Android 自定义view-仿新浪微博#话题#插入EditText 博客地址http://blog.csdn.net/u014702653/article/details/52799715
Android自定义控件--下拉刷新的实现,直接在项目中可用,可访问博客:http://blog.csdn.net/qq_20889581/article/details/51008917 进行详细浏览,欢迎留下宝贵意见!
Android自定义控件---“取消”视图 此Demo博客地址:http://blog.csdn.net/u012814441/article/details/51220396
左边一个减少的按钮,右边一个增加的按钮,中间是输入框。 做成通用的组件,可以公用
可以在一个固定宽度的区域显示多个不定长度的字符串,类似于淘宝上商品详情的尺码和颜色分类那种。
Android自定义桌面-选择要显示的应用
刮刮卡,引用在:http://gundumw100.iteye.com/blog/1932020
Android自定义控件---继承ProgressBar功能扩展 博客地址:http://blog.csdn.net/u012814441
android自定义日历-实现签到订约功能 基于IDEA的工程源码
android 自定义控件----编辑框
可以在一个固定宽度的区域显示多个不定长度的字符串,类似于淘宝上商品详情的尺码和颜色分类那种。
Android自定义 View - 仿淘宝 淘抢购进度条
Android高级应用源码-android自定义控件-侧滑菜单.zip
Android自定义Dialog--DanmakuFlame弹幕发送框界面的实现
Android自定义View---秒表/时钟 博客地址 http://blog.csdn.net/liujunpen/article/details/53737624
安卓自定义View---视频音量调控 资源为博客实例:http://blog.csdn.net/wangxiyue0311/article/details/77929846 有问题博客留言
android自定义view-打造圆形ImageView(四)终结篇,对应博客地址:http://blog.csdn.net/smarticeberg/article/details/51172900
Android自定义View-开关按钮
Android自定义View-实现圆形水波进度条