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