现在有这样一个需求,实现显示随机随机数可能在代码中直接很简单的就实现了,但是现在我们直接自定义view来实现这个效果,那么我们来分析一波吧,我们允许开发者自己设置这个textvIEw的大小,颜色,和初始四位随机数的文字,那么我们需要提供自定义属性,好吧,首先把自定义属性的简单使用介绍一下吧:
首先在res/values文件夹下建利attrs.xml文件,由于这次我们功能决定我们要提供三个自定义属性,分别是textTitle String类型的,textcolor是color类型的,textSize是dimetion类型,代码如下:
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="MyTextVIEw"> <attr name="TitleText" format="string"/> <attr name="TitleTextcolor" format="color"/> <attr name="TitleTextSize" format="dimension"/> </declare-styleable></resources>
再来看看我们怎么在布局文件中的自定义控件中去使用我们自定义的属性
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:custom="http://schemas.androID.com/apk/res/com.qianmo.VerificationCode" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <com.qianmo.VerificationCode.vIEw.MyTextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="100dp" custom:TitleText="3712" custom:TitleTextcolor="#ff0000" androID:layout_centerInParent="true" custom:TitleTextSize="40sp"/></relativeLayout>
关键的两句代码:
xmlns:custom=”http://schemas.androID.com/apk/res/com.qianmo.VerificationCode” 添加自定义的空间名,com.qianmo.VerificationCode使我们的包名,使用是以custom:开头 ,例如:custom:TitleTextSize
现在自定义的属性搞定了,开始我们的自定义view吧,首先选择,我们继承的是VIEw还是VIEwGroup,很明显,这次我们是一个简单的VIEw,所以选择继承VIEw,下面直接贴出来代码了,每一步代码里面都很详细,就不多给大家解释了
package com.qianmo.VerificationCode.vIEw;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.util.Log;import androID.util.TypedValue;import androID.vIEw.VIEw;import com.qianmo.VerificationCode.R;import java.util.HashSet;import java.util.Random;import java.util.Set;/** * Created by wangjitao on 2016/10/13 0013. * 用于实现获取随机码 */public class MyTextVIEw extends VIEw { /** * 由于是自定义的VIEw,首先我们要确定那些属性是用户可以自己定义的 * 1,VIEw里面显示的字 * 2,显示字的大小 * 3,显示字的颜色 */ private String mTitleText; private int mTitleTextcolor; private int mTitleTextSize; /** * 画笔 */ private Paint mPaint; /** * vIEw的矩形背景 */ private Rect mBound; public MyTextVIEw(Context context) { this(context,null); } public MyTextVIEw(Context context,AttributeSet attrs) { this(context,attrs,0); } /** * 获得自定义的属性 * * @param context * @param attrs * @param defStyleAttr */ public MyTextVIEw(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); /** * 获得我们自定义的一些属性 */ TypedArray a = context.gettheme().obtainStyledAttributes(attrs,R.styleable.MyTextVIEw,defStyleAttr,0); mTitleText = randomText(); //初始化显示的数字 for (int i = 0; i < a.getIndexCount(); i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.MyTextVIEw_TitleText: mTitleText = a.getString(attr); break; case R.styleable.MyTextVIEw_TitleTextcolor: mTitleTextcolor = a.getcolor(attr,color.BLACK); break; case R.styleable.MyTextVIEw_TitleTextSize: //设置默认大小为16 mTitleTextSize = a.getDimensionPixelSize(attr,(int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP,16,getResources().getdisplayMetrics())); break; } } //将TypedArray对象回收 a.recycle(); /** * 初始化画笔 */ mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setTextSize(mTitleTextSize); mPaint.setcolor(mTitleTextcolor); mBound = new Rect(); mPaint.getTextBounds(mTitleText,mTitleText.length(),mBound); /** * 模仿点击换验证码 */ this.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { mTitleText = randomText(); postInvalIDate(); } }); } /** * 获取四位随机数验证码 * * @return */ private String randomText() { Random random = new Random(); Set<Integer> set = new HashSet<Integer>(); while (set.size() < 4) { int randomInt = random.nextInt(10); set.add(randomInt); } StringBuffer sb = new StringBuffer(); for (Integer i : set) { sb.append("" + i); } return sb.toString(); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { /** * 处理当宽高都是wrap_content的情况 */ int wIDthMode = MeasureSpec.getMode(wIDthMeasureSpec); int wIDthSize = MeasureSpec.getSize(wIDthMeasureSpec); int heightmode = MeasureSpec.getMode(heightmeasureSpec); int heightSize = MeasureSpec.getSize(heightmeasureSpec); int wIDth = 0; int height = 0; if (wIDthMode == MeasureSpec.EXACTLY) { wIDth = wIDthSize; } else { mPaint.setTextSize(mTitleTextSize); mPaint.getTextBounds(mTitleText,mBound); float textWIDth = mBound.wIDth(); int desired = (int) (getpaddingleft() + textWIDth + getpaddingRight()); wIDth = desired; } if (heightmode == MeasureSpec.EXACTLY) { height = heightSize; } else { mPaint.setTextSize(mTitleTextSize); mPaint.getTextBounds(mTitleText,mBound); float textWIDth = mBound.height(); int desired = (int) (getpaddingtop() + textWIDth + getpaddingBottom()); height = desired; } setMeasuredDimension(wIDth,height); } @OverrIDe protected voID onDraw(Canvas canvas) { /** * 绘制文字和矩形 */ mPaint.setcolor(color.YELLOW); canvas.drawRect(0,getMeasureDWIDth(),getMeasuredHeight(),mPaint); mPaint.setcolor(mTitleTextcolor); canvas.drawText(mTitleText,getWIDth() / 2 - mBound.wIDth() / 2,getHeight() / 2 + mBound.height() / 2,mPaint); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android自定义View绘制四位数随机码全部内容,希望文章能够帮你解决Android自定义View绘制四位数随机码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)