Android自定义控件之对原生控件的拓展
来源:互联网 发布:godaddy域名怎么绑定ip 编辑:程序博客网 时间:2024/06/15 05:06
在Android开发中,原生控件时常会无法满足我们的需要。这时,我们就需要进行自定义控件了,而对原生控件的拓展即是解决方法之一。
对原生控件的拓展
对原生控件的拓展是指在Android提供的控件的基础上进行修改,实现符合需求的控件。做法是通过创建一个自定义的控件类,继承要进行拓展的控件的类。在继承来的方法中进行拓展。这样的好处在于,不仅控件的展示能符合要求,使用方法也和所继承的原生控件的使用方法一致。
案例
案例都是来源于最近正在看的书————《Android群英传》。
带矩形边框的文本
创建一个自定义的CustomTextView继承TextView
public class CustomTextView extends TextView {private Paint paint1,paint2;public CustomTextView(Context context) { super(context); initView();}public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); initView();}public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView();}private void initView(){ paint1 = new Paint(); paint1.setColor(Color.BLUE); paint1.setStyle(Paint.Style.FILL); paint2 = new Paint(); paint2.setColor(Color.YELLOW); paint2.setStyle(Paint.Style.FILL);}@Overrideprotected void onDraw(Canvas canvas) { //绘制外层矩形 canvas.drawRect(0,0,getMeasuredWidth()+6,getMeasuredHeight()+6,paint1); //绘制内层矩形 canvas.drawRect(3, 3, getMeasuredWidth() - 3, getMeasuredHeight() - 3, paint2); canvas.save(); //绘制文字前平移3个像素 canvas.translate(3, 0); //父类完成的方法,绘制文本 super.onDraw(canvas); canvas.restore();}}在布局中使用<custom.CustomTextView andro
id:id="@+id/customTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义带矩形的边框" />其实
从上面的代码也可以看出,带边框就是一个小矩形叠在一个稍大点的矩形上形成的效果。
闪动的文字
渐变闪动,使用的是LinearGradient(又称线性渲染)来实现的。
public class CustomTextView2 extends TextView { private LinearGradient mLinearGradient; private Matrix mMatrix; private Paint paint; private int mViewWidth = 0; private int mTranslate = 0; public CustomTextView2(Context context, AttributeSet attrs) { super(context, attrs); } @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){ paint = getPaint(); mLinearGradient = new LinearGradient( 0, 0, mViewWidth, 0, new int[]{Color.BLUE,0xffffffff, Color.BLUE}, null, Shader.TileMode.CLAMP); paint.setShader(mLinearGradient); mMatrix = new Matrix(); } } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mMatrix != null){ mTranslate += mViewWidth / 5 ; if (mTranslate > 2 * mViewWidth){ mTranslate = - mViewWidth; } mMatrix.setTranslate(mTranslate,0); mLinearGradient.setLocalMatrix(mMatrix); postInvalidateDelayed(100); } }}
阅读全文
0 0
- Android自定义控件之对原生控件的拓展
- Android自定义控件之对原生控件的拓展
- 自定义控件之对现有控件拓展(一)
- 自定义控件之对现有控件拓展(一)
- Android自定义控件之拓展TextView(ExpandTextView)
- 《Android 群英传》读书笔记:自定义 View 之对现有控件进行拓展
- Android View之对现有控件进行拓展实例
- Android 自定义控件----( 原生控件修改 onDraw )
- 使用Android原生控件自定义Switch控件
- Android之原生控件列表
- 拓展控件 - 拓展的TextView
- Android之自定义控件
- Android之自定义控件
- android之自定义控件
- Android自定义控件之自定义日历控件
- Android自定义控件之自定义组合控件
- Android 自定义控件之自定义组合控件
- Android自定义控件之自定义组合控件
- Linux编程学习之信号篇-pause与sigsuspend
- Windows进程管理
- 写给后端程序员的 HTTP 缓存原理介绍
- shell脚本中cd命令无效的解决方案
- js-ES6学习笔记-字符串的扩展
- Android自定义控件之对原生控件的拓展
- FreeRtos移植到Arduino UNO
- MIME type列表大全
- POJ 1775 Sum of Factorials 笔记
- 交换排序
- 74.windbg-wt(运行并统计函数的调用)
- 深度学习keras连接
- Http中Content-Type的详解
- Sql 优化2