自定义验证码

来源:互联网 发布:数据加密芯片 编辑:程序博客网 时间:2024/05/16 10:10
  1. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
  2. {  
  3.     int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  4.     int widthSize = MeasureSpec.getSize(widthMeasureSpec);  
  5.     int heightMode = MeasureSpec.getMode(heightMeasureSpec);  
  6.     int heightSize = MeasureSpec.getSize(heightMeasureSpec);  
  7.     int width;  
  8.     int height ;  
  9.     if (widthMode == MeasureSpec.EXACTLY)  
  10.     {  
  11.         width = widthSize;  
  12.     } else  
  13.     {  
  14.         mPaint.setTextSize(mTitleTextSize);  
  15.         mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);  
  16.         float textWidth = mBounds.width();  
  17.         int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());  
  18.         width = desired;  
  19.     }  
  20.   
  21.     if (heightMode == MeasureSpec.EXACTLY)  
  22.     {  
  23.         height = heightSize;  
  24.     } else  
  25.     {  
  26.         mPaint.setTextSize(mTitleTextSize);  
  27.         mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds);  
  28.         float textHeight = mBounds.height();  
  29.         int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());  
  30.         height = desired;  
  31.     }  
  32.       
  33.       
  34.   
  35.     setMeasuredDimension(width, height);  
  36. }  

现在我们修改下布局文件:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <com.example.customview01.view.CustomTitleView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         custom:titleText="3712"  
  11.         android:padding="10dp"  
  12.         custom:titleTextColor="#ff0000"  
  13.         android:layout_centerInParent="true"  
  14.         custom:titleTextSize="40sp" />  
  15.   
  16. </RelativeLayout>  

现在的效果是:


完全复合我们的预期,现在我们可以对高度、宽度进行随便的设置了,基本可以满足我们的需求。

当然了,这样下来我们这个自定义View与TextView相比岂不是没什么优势,所有我们觉得给自定义View添加一个事件:

在构造中添加:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. this.setOnClickListener(new OnClickListener()  
  2.         {  
  3.   
  4.             @Override  
  5.             public void onClick(View v)  
  6.             {  
  7.                 mTitleText = randomText();  
  8.                 postInvalidate();  
  9.             }  
  10.   
  11.         });  

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private String randomText()  
  2.     {  
  3.         Random random = new Random();  
  4.         Set<Integer> set = new HashSet<Integer>();  
  5.         while (set.size() < 4)  
  6.         {  
  7.             int randomInt = random.nextInt(10);  
  8.             set.add(randomInt);  
  9.         }  
  10.         StringBuffer sb = new StringBuffer();  
  11.         for (Integer i : set)  
  12.         {  
  13.             sb.append("" + i);  
  14.         }  
  15.   
  16.         return sb.toString();  
  17.     }  

下面再来运行:


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


0 0
原创粉丝点击