編輯:關於Android編程
先放上這個小Demo的圖片:點擊圖片可以更換新的驗證碼。
驗證碼是以圖片的形式顯示的,所以需要寫一個自定義驗證碼控件
public class ValidateView extends View { /** * 點數 */ private int pointNum = 150;// 背景雜質 /** * 線段數 */ private int lineNum = 3;// 背景雜質 /** * 驗證碼字長 */ private int validateCodeLenght = 6;// 默認長度為6 /** * 驗證碼 */ private String[] validateCode; private Paint mTempPaint = new Paint(); private Context mContext; /** * 驗證碼內容 */ private static final String[] strContent = new String[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, g , h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S , T, U, V, W, X, Y, Z }; public ValidateView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; mTempPaint.setAntiAlias(true); mTempPaint.setTextSize(50); mTempPaint.setStrokeWidth(3); validateCode = new String[validateCodeLenght];// 驗證碼 } public String[] getValidateCode() { return validateCode; } public void setValidateCode(String[] validateCode) { this.validateCode = validateCode; } public void onDraw(Canvas canvas) { canvas.drawColor(Color.GREEN); // 繪制驗證碼 final int height = getHeight(); final int width = getWidth(); int dx = 30; for (int i = 0; i < validateCodeLenght; i++) { canvas.drawText( + validateCode[i], dx, getPositon(height), mTempPaint); dx += width / (validateCodeLenght + 1); } int[] line; for (int i = 0; i < lineNum; i++) { line = getLine(height, width); canvas.drawLine(line[0], line[1], line[2], line[3], mTempPaint); } // 繪制小圓點 int[] point; for (int i = 0; i < pointNum; i++) { point = getPoint(height, width); canvas.drawCircle(point[0], point[1], 1, mTempPaint); } } /** * 更新驗證碼顯示 */ public void invaliChenkNum() { invalidate(); } /** * 生成驗證碼,並顯示在圖片上 * * @return */ public String[] createAndSetValidateCode() { String[] tempValidateCode = new String[validateCodeLenght]; for (int i = 0; i < validateCodeLenght; i++) { tempValidateCode[i] = strContent[(int) (Math.random() * strContent.length)]; } validateCode = tempValidateCode; return tempValidateCode; } /** * 生成驗證碼 * * @return */ public String[] createCheckNum() { String[] tempCheckNum = new String[validateCodeLenght]; for (int i = 0; i < validateCodeLenght; i++) { tempCheckNum[i] = strContent[(int) (Math.random() * strContent.length)]; } return tempCheckNum; } private int[] getLine(int height, int width) { int[] tempCheckNum = new int[validateCodeLenght]; for (int i = 0; i < validateCodeLenght; i += 2) { tempCheckNum[i] = (int) (Math.random() * width); tempCheckNum[i + 1] = (int) (Math.random() * height); } return tempCheckNum; } private int[] getPoint(int height, int width) { int[] tempCheckNum = new int[validateCodeLenght]; tempCheckNum[0] = (int) (Math.random() * width); tempCheckNum[1] = (int) (Math.random() * height); return tempCheckNum; } /** * 驗證是否正確,單元測試通過 * * @param userCheck * 用戶輸入的驗證碼 * @param ValidateCode * 自動生成的驗證碼 * @return */ public boolean validateCode(String userCheck, String[] code) { if (userCheck.length() != validateCodeLenght) { return false; } String checkString = ; for (int i = 0; i < validateCodeLenght; i++) { checkString += code[i]; } if (userCheck.equals(checkString)) { return true; } else { return false; } } /** * 獲取驗證碼的縱坐標 * * @param height * @return */ public int getPositon(int height) { int tempPositoin = (int) (Math.random() * height); if (tempPositoin < 25) { tempPositoin += 25; } return tempPositoin; } public int getPointNum() { return pointNum; } public void setPointNum(int pointNum) { this.pointNum = pointNum; } public int getLineNum() { return lineNum; } public void setLineNum(int lineNum) { this.lineNum = lineNum; } public int getValidateCodeLenght() { return validateCodeLenght; } public void setValidateCodeLenght(int validateCodeLenght) { this.validateCodeLenght = validateCodeLenght; validateCode = new String[validateCodeLenght];// 驗證碼 } }簡單的XML布局:
public class MainActivity extends Activity { ValidateView validateView; private static final int LENGHT = 6;//驗證碼的長度 private String[] code = new String[LENGHT];//驗證碼 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); validateView = (ValidateView) findViewById(R.id.validateView); validateView.setValidateCodeLenght(LENGHT); code = validateView.createAndSetValidateCode(); validateView.setOnClickListener(new OnClickListener() { // 點擊圖片,更換驗證碼 @Override public void onClick(View arg0) { code = validateView.createAndSetValidateCode(); validateView.invalidate(); } }); } }一般使用方式都是獲取自動生成的驗證碼,再從輸入框獲取用戶輸入的驗證碼,相互比對就可以得出結果。
在上一篇文章中《Android 基於Netty的消息推送方案之概念和工作原理(二)》 ,我們介紹過一些關於Netty的概念和工作原理的內容,今天我們先來介紹一個叫做Cha
最近我嘗試使用ViewPager+GridView實現的,看起來一切正常,廢話不多說,具體代碼如下:如圖是效果圖 首先分析下思路1.首先是怎麼布局:整體是一個V
首先我將貼出幾種實現圓角邊框的dmeo程序效果圖:方式一:使用shape元素填充背景,設置圓角/帶弧度的角1、首先在 \res\drawable下新建Shape為根元素的
第三課(第三步):支持以手指觸控的任意點為中心開始縮放關鍵部分是在縮放的時候不斷進行邊界檢測,防止放大後縮小後出現白邊: /** * 在縮放的時候進行邊界控制