android 自定义控件继承TextView

在原生控件上进行扩展,增加新的功能

一般是在onDraw() 方法中对原生控件进行扩展

下面以一个TextView 为例,来看看如何使用扩展原生控件的方法创建新的控件

/*
 * 对现有控件进行扩展
 * */

public class M_TextView extends TextView{



	public M_TextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	public M_TextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public M_TextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		Paint mPaint1 = new Paint();
		mPaint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
		mPaint1.setStyle(Paint.Style.FILL);
		Paint mPaint2 = new Paint();
		mPaint2.setColor(Color.YELLOW);
		mPaint2.setStyle(Paint.Style.FILL);
		//绘制外层矩形
		canvas.drawRect(0, 0, getMeasuredWidth(),getMeasuredHeight(), mPaint1);
		//绘制内层矩形
		canvas.drawRect(10, 10, getMeasuredWidth()-10, getMeasuredHeight()-10, mPaint2);
		canvas.save();
		//绘制文字前平移10像素
		canvas.translate(10, 0);
		super.onDraw(canvas);
		canvas.restore();
	}

}
在布局文件中使用:

 <com.example.kongjian_1.M_TextView
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="android"
        android:textColor="@android:color/black"
        android:textSize="25sp" >
    </com.example.kongjian_1.M_TextView>

实例二:给文字增加闪动效果:

public class M_TextView2  extends TextView {

	public M_TextView2(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public M_TextView2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public M_TextView2(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	private LinearGradient mLinearGradient;
	private Matrix mGradientMatrix;
	private Paint mPaint;
	private int mViewWidth = 0;
	private int mTranslate = 0;


	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		if (mViewWidth == 0) {
			mViewWidth = getMeasuredWidth();
			if (mViewWidth > 0) {
				mPaint = getPaint();
				mLinearGradient = new LinearGradient(
						0,
						0,
						mViewWidth,
						0,
						new int[]{
								Color.BLUE, 0xffffffff,
								Color.BLUE},
								null,
								Shader.TileMode.CLAMP);
				mPaint.setShader(mLinearGradient);
				mGradientMatrix = new Matrix();
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (mGradientMatrix != null) {
			mTranslate += mViewWidth / 5;
			if (mTranslate > 2 * mViewWidth) {
				mTranslate = -mViewWidth;
			}
			mGradientMatrix.setTranslate(mTranslate, 0);
			mLinearGradient.setLocalMatrix(mGradientMatrix);
			postInvalidateDelayed(50);
		}
	}
}

xml中使用:

<com.example.kongjian_1.M_TextView2
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="android"
        android:textColor="@android:color/black"
        android:textSize="25sp" >
    </com.example.kongjian_1.M_TextView2>




  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值