android 自定义控件继承TextView
Posted 0 and 1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
以上是关于android 自定义控件继承TextView的主要内容,如果未能解决你的问题,请参考以下文章
Android 手机卫士--自定义控件(获取焦点的TextView)