android 进阶自定义控件1
16lz
2021-01-26
在原生控件上进行扩展,增加新的功能
一般是在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}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubPaint 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;@Overrideprotected 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();}}}@Overrideprotected 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>
更多相关文章
- ViewPager And Fragment
- Android(安卓)自定义View实现动画形式加载环形图
- Android(安卓)自定义控件属性赋值
- Android(安卓)Material Design-UI
- GridLayout用法
- Android之单元测试——下
- Android(安卓)高级控件(一)
- GitHub 上受欢迎的 Android(安卓)UI Library 整理二
- android inflate初探