自定义View textview

来源:互联网 发布:斯米诺黑牌伏特加知乎 编辑:程序博客网 时间:2024/05/23 11:39

attrs

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:openxu="http://schemas.android.com/apk/res-auto"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.bwie.test.textviewlian.MainActivity">    <com.bwie.test.textviewlian.MyView        android:layout_width="200dip"        android:layout_height="100dip"        openxu:mTextSize="25sp"        openxu:mText="i love you"        openxu:mTextColor ="#0000ff"        android:background="#ff0000" /></RelativeLayout>

package com.bwie.test.textviewlian;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.view.View;/** * Created by 景瑾 on 2017/9/28. */public class MyView extends View{    /**     * 需要绘制的文字     */    private String mText;    /**     * 文本的颜色     */    private int mTextColor;    /**     * 文本的大小     */    private float mTextSize;    /**     * 绘制时控制文本绘制的范围     */    private Rect mBound;    private Paint mPaint;    public MyView(Context context) {        this(context, null);    }    public MyView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);       /* //初始化        mText = "Udf32fA";        mTextColor = Color.BLACK;        mTextSize = 20;        mPaint = new Paint();        mPaint.setTextSize(mTextSize);        mPaint.setColor(mTextColor);        //获得绘制文本的宽和高        mBound = new Rect();        mPaint.getTextBounds(mText, 0, mText.length(), mBound);*/        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTextView, defStyleAttr, 0);        mText = a.getString(R.styleable.MyTextView_mText);        mTextColor = a.getColor(R.styleable.MyTextView_mTextColor, Color.BLACK);        mTextSize = a.getDimension(R.styleable.MyTextView_mTextSize, 100);        a.recycle();  //注意回收        mPaint = new Paint();        mPaint.setTextSize(mTextSize);        mPaint.setColor(mTextColor);        //获得绘制文本的宽和高        mBound = new Rect();        mPaint.getTextBounds(mText, 0, mText.length(), mBound);    }    //API21//    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {//        super(context, attrs, defStyleAttr, defStyleRes);//        init();//    }    @Override    protected void onDraw(Canvas canvas) {        //绘制文字        canvas.drawText(mText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);    }}


原创粉丝点击