Android自定义View绘制四位数随机码
程序员文章站
2023-12-13 22:56:40
现在有这样一个需求,实现显示随机随机数可能在代码中直接很简单的就实现了,但是现在我们直接自定义view来实现这个效果,那么我们来分析一波吧,我们允许开发者自己设置这个tex...
现在有这样一个需求,实现显示随机随机数可能在代码中直接很简单的就实现了,但是现在我们直接自定义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, attrs, 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, 0, 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, 0, mtitletext.length(), 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, 0, mtitletext.length(), 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, 0, getmeasuredwidth(), getmeasuredheight(), mpaint); mpaint.setcolor(mtitletextcolor); canvas.drawtext(mtitletext, getwidth() / 2 - mbound.width() / 2, getheight() / 2 + mbound.height() / 2, mpaint); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。