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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值