Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android自定义View编写随机验证码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

很多的Android入门程序猿来说对于Android自定义view,可能都是比较恐惧的,但是这又是高手进阶的必经之路,所有准备在自定义view上面花一些功夫,多写一些文章。先总结下自定义view的步骤:
1、自定义view属性
2、在View的构造方法中获得我们自定义属性
[ 3、重写onMesure ]
4、重写onDraw

我把3用[]标出了,所以说3不一定是必须的,当然了大部分情况下还是需要重写的。

1、自定义view属性,首先在res/values/  下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
 <attr name="titleText" format="String" /> 
 <attr name="titleTextColor" format="color" /> 
 <attr name="titleTextSize" format="dimension" /> 
 
 <declare-styleable name="CustomtitleView"> 
 <attr name="titleText" /> 
 <attr name="titleTextColor" /> 
 <attr name="titleTextSize" /> 
 </declare-styleable> 
 
</resources> 

我们定义了字体,字体颜色,字体大小3个属性,format是值该属性的取值类型:
一共有:String,color,demension,Integer,enum,reference,float,Boolean,fraction,flag;不清楚的可以google一把。
然后在布局中声明我们的自定义view

<RelativeLayout xmlns:android="http://scheR_164_11845@as.android.com/apk/res/android" 
 xmlns:tools="http://scheR_164_11845@as.android.com/tools" 
 xmlns:custom="http://scheR_164_11845@as.android.com/apk/res/com.example.customview01" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
 
 <com.example.customview01.view.CustomtitleView 
 android:layout_width="200dp" 
 android:layout_height="100dp" 
 custom@R_590_6964@Text="3712" 
 custom@R_590_6964@TextColor="#ff0000" 
 custom@R_590_6964@TextSize="40sp" /> 
 
</RelativeLayout> 

一定要引入 xmlns:custom="http://scheR_164_11845@as.android.com/apk/res/com.example.customview01"我们的命名空间,后面的包路径指的是项目的package

2、在View的构造方法中,获得我们的自定义的样式

/** 
 * 文本 
 */ 
 private String mtitleText; 
 /** 
 * 文本的颜色 
 */ 
 privatE int mtitleTextColor; 
 /** 
 * 文本的大小 
 */ 
 privatE int mtitleTextSize; 
 
 /** 
 * 绘制时控制文本绘制的范围 
 */ 
 private Rect mBound; 
 private Paint mPaint; 
 
 public CustomtitleView(Co@R_674_10443@t co@R_674_10443@t,AttributeSet attrs) 
 { 
 this(co@R_674_10443@t,attrs,0); 
 } 
 
 public CustomtitleView(Co@R_674_10443@t co@R_674_10443@t) 
 { 
 this(co@R_674_10443@t,null); 
 } 
 
 /** 
 * 获得我自定义的样式属性 
 * 
 * @param co@R_674_10443@t 
 * @param attrs 
 * @param defStyle 
 */ 
 public CustomtitleView(Co@R_674_10443@t co@R_674_10443@t,AttributeSet attrs,int defStylE) 
 { 
 super(co@R_674_10443@t,defStylE); 
 /** 
 * 获得我们所定义的自定义样式属性 
 */ 
 TypedArray a = co@R_674_10443@t.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomtitleView,defStyle,0); 
 int n = a.geTindexCount(); 
 for (int i = 0; i < n; i++) 
 { 
 int attr = a.geTindex(i); 
 switch (attr) 
 { 
 case R.styleable.CustomtitleView_titleText: 
 mtitleText = a.getString(attr); 
 break; 
 case R.styleable.CustomtitleView_titleTextColor: 
 // 认颜色设置为黑色 
 mtitleTextColor = a.getColor(attr,Color.bLACK); 
 break; 
 case R.styleable.CustomtitleView_titleTextSize: 
 // 认设置为16sp,TypeValue也可以把sp转化为px 
 mtitleTextSize = a.getDimensionPixelSize(attr,(int) TypedValue.applyDimension( 
  TypedValue.COMPLEX_UNIT_SP,16,getresources().getDisplaymetrics())); 
 break; 
 
 } 
 
 } 
 a.recycle(); 
 
 /** 
 * 获得绘制文本的宽和高 
 */ 
 mPaint = new Paint(); 
 mPaint.setTextSize(mtitleTextSizE); 
 // mPaint.setColor(mtitleTextColor); 
 mBound = new Rect(); 
 mPaint.getTextBounds(mtitleText,mtitleText.length(),mBound); 
 
 } 

我们重写了3个构造方法认的布局文件调用的是两个参数的构造方法,所以记得让所有的构造调用我们的三个参数的构造,我们在三个参数的构造中获得自定义属性

3、我们重写onDraw,onMesure调用系统提供的:

@Override 
 protected void onMeasure(int widthMeasureSpec,int heightMeasureSpeC) 
 { 
 super.onMeasure(widthMeasureSpec,heightMeasureSpec); 
 } 
 
 @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编写随机验证码

是不是觉得还不错,基本已经实现了自定义view。但是此时如果我们把布局文件的宽和高写成wrap_content,会发现效果并不是我们的预期:

Android自定义View编写随机验证码

系统帮我们测量的高度和宽度都是MATCH_PARNET,当我们设置明确的宽度和高度时,系统帮我们测量的结果就是我们设置的结果,当我们设置为WRAP_CONTENT,或者MATCH_PARENT系统帮我们测量的结果就是MATCH_PARENT的长度。
所以,当设置了WRAP_CONTENT时,我们需要自己进行测量,即重写onMesure方法”:
重写之前先了解MeasureSpec的specMode,一共三种类型:

EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用

下面是我们重写onMeasure代码

@Override 
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpeC) 
{ 
 int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
 int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
 int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
 int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
 int width; 
 int height ; 
 if (widthMode == MeasureSpec.EXACTLY) 
 { 
 width = widthSize; 
 } else 
 { 
 mPaint.setTextSize(mtitleTextSizE); 
 mPaint.getTextBounds(mtitle,mtitle.length(),mBounds); 
 float textWidth = mBounds.width(); 
 int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight()); 
 width = desired; 
 } 
 
 if (heightMode == MeasureSpec.EXACTLY) 
 { 
 height = heightSize; 
 } else 
 { 
 mPaint.setTextSize(mtitleTextSizE); 
 mPaint.getTextBounds(mtitle,mBounds); 
 float textHeight = mBounds.height(); 
 int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom()); 
 height = desired; 
 } 
 
 
 
 setMeasuredDimension(width,height); 
} 

现在我们修改下布局文件

<RelativeLayout xmlns:android="http://scheR_164_11845@as.android.com/apk/res/android" 
 xmlns:tools="http://scheR_164_11845@as.android.com/tools" 
 xmlns:custom="http://scheR_164_11845@as.android.com/apk/res/com.example.customview01" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
 
 <com.example.customview01.view.CustomtitleView 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 custom@R_590_6964@Text="3712" 
 android:padding="10dp" 
 custom@R_590_6964@TextColor="#ff0000" 
 android:layout_centerInParent="true" 
 custom@R_590_6964@TextSize="40sp" /> 
 
</RelativeLayout> 

现在的效果是:

Android自定义View编写随机验证码

完全复合我们的预期,现在我们可以对高度、宽度进行随便的设置了,基本可以满足我们的需求。
当然了,这样下来我们这个自定义view与TextView相比岂不是没什么优势,所有我们觉得给自定义view添加一个事件:
在构造中添加

this.setOnClickListener(new OnClickListener() 
 { 
 
 @Override 
 public void onClick(View v) 
 { 
 mtitleText = randomText(); 
 posTinvalidate(); 
 } 
 
 }); 
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 = @R_262_3320@Buffer(); 
 for (Integer i : set) 
 { 
 sb.append("" + i); 
 } 
 
 return sb.toString(); 
 } 

下面再来运行:

Android自定义View编写随机验证码

我们添加一个点击事件,每次让它随机生成一个4位的随机数,有兴趣的可以在onDraw中添加一点噪点,然后改写为验证码,是不是感觉很不错。

源码下载:http://xiazai.code.net/201610/yuanma/AndroidCustomView(code.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

大佬总结

以上是大佬教程为你收集整理的Android自定义View编写随机验证码全部内容,希望文章能够帮你解决Android自定义View编写随机验证码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:验证码