Skip to content

Commit

Permalink
加入模拟请求网络验证码,然后显示
Browse files Browse the repository at this point in the history
  • Loading branch information
Freshman111 committed Dec 8, 2016
1 parent 1ca9fa5 commit 418dd30
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 33 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,20 @@ verificationCodeView.setOnClickListener(new View.OnClickListener() {
}
});
```
```

####请求网络验证码时

```
<sgffsg.com.verifycodeview.VerificationCodeView
android:id="@+id/net_verifycodeview"
android:layout_width="100dp"
android:layout_height="40dp"
android:visibility="gone"
app:isNetCode="true"/>
```


Thanks

[CaptchaImageView](https://github.com/jineshfrancs/CaptchaImageView)
87 changes: 83 additions & 4 deletions app/src/main/java/sgffsg/com/verifycodeview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
package sgffsg.com.verifycodeview;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

private VerificationCodeView verificationCodeView;
private Button btnSubmit;
private EditText edit_input;
private VerificationCodeView verificationCodeView,net_verificationCodeView;
private Button btnSubmit,btnNetSubmit;
private EditText edit_input,net_edit_input;
private ProgressBar progressBar;
private FrameLayout netVcViewLayout;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
String code= (String) msg.obj;
progressBar.setVisibility(View.GONE);
net_verificationCodeView.setVisibility(View.VISIBLE);
net_verificationCodeView.setvCode(code);
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verificationCodeView = (VerificationCodeView) findViewById(R.id.verifycodeview);
net_verificationCodeView = (VerificationCodeView) findViewById(R.id.net_verifycodeview);
btnSubmit= (Button) findViewById(R.id.btn_submit);
btnNetSubmit= (Button) findViewById(R.id.net_btn_submit);
edit_input= (EditText) findViewById(R.id.edit_input);
net_edit_input= (EditText) findViewById(R.id.net_edit_input);
progressBar= (ProgressBar) findViewById(R.id.net_pregressbar);
netVcViewLayout= (FrameLayout) findViewById(R.id.net_verifycodeview_layut);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -40,9 +62,66 @@ public void onClick(View view) {
verificationCodeView.refreshCode();
}
});
}

btnNetSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String input=net_edit_input.getText().toString().trim().toLowerCase();
String code= net_verificationCodeView.getvCode().toLowerCase();
if (!TextUtils.isEmpty(input)&&input.equals(code)){
Toast.makeText(MainActivity.this,"Verify Success!Welcome",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"Verification code error!Try Again",Toast.LENGTH_SHORT).show();
}
}
});
netVcViewLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
net_edit_input.setText("");
progressBar.setVisibility(View.VISIBLE);
net_verificationCodeView.setVisibility(View.GONE);
loadNetCode();
}
});
loadNetCode();
}


/**
* 模拟加载网络验证码
* load network verification code
*/
private void loadNetCode(){
handler.postDelayed(new Runnable() {
@Override
public void run() {
Message message=handler.obtainMessage();
message.obj=getCharAndNumr();
handler.sendMessage(message);
}
},1000);
}

/**
* java生成随机数字和字母组合
* @return 随机验证码
*/
public String getCharAndNumr() {
String val = "";
Random random = new Random();
for (int i = 0; i < 4; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equalsIgnoreCase(charOrNum)) {
// 取得大写字母还是小写字母
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextInt(26));
} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sgffsg.com.verifycodeview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class VerificationCodeView extends View {
private int mWidth;
//控件高度
private int mHeight;
private boolean isInited=false;//是否初始化完成

private Bitmap mbitmap;
private Bitmap codebitmap;
Expand All @@ -58,6 +60,7 @@ public class VerificationCodeView extends View {
private ArrayList<Path> mPaths = new ArrayList<Path>();
private float offset=5;//扭曲偏移
private String vCode;
private boolean isNetCode=false;//是否是网络请求到验证码

public VerificationCodeView(Context context) {
this(context,null);
Expand All @@ -69,6 +72,7 @@ public VerificationCodeView(Context context, AttributeSet attrs) {

public VerificationCodeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
initView();
}

Expand Down Expand Up @@ -102,9 +106,15 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mBounds=new RectF(getLeft(),getTop(),getRight(),getBottom());
mWidth= (int) (mBounds.right-mBounds.left);
mHeight= (int) (mBounds.bottom-mBounds.top);
isInited=true;
createCodeBitmap();
}

private void init(AttributeSet attrs) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.VerificationCodeView);
isNetCode = typedArray.getBoolean(R.styleable.VerificationCodeView_isNetCode,false);
}

/**
* 初始化
*/
Expand Down Expand Up @@ -143,6 +153,8 @@ protected void onDraw(Canvas canvas) {
* 生成验证码图片
*/
private void createCodeBitmap() {
if (!isInited)
return;
mPaths.clear();
// 生成干扰线坐标
for(int i=0;i<2;i++){
Expand All @@ -162,20 +174,20 @@ private void createCodeBitmap() {
tempCode=getCharAndNumr();
//画背景
myCanvas.drawColor(YELLOW_BG_COLOR);

textPaint.getTextBounds(tempCode,0,codeNum,textBound);
float charLength=(textBound.width())/codeNum;
for (int i=0;i<codeNum;i++){
int offsetDegree=mRandom.nextInt(15);
// 这里只会产生0和1,如果是1那么正旋转正角度,否则旋转负角度
offsetDegree = mRandom.nextInt(2) == 1?offsetDegree:-offsetDegree;
myCanvas.save();
myCanvas.rotate(offsetDegree,mWidth/2,mHeight/2);
// 给画笔设置随机颜色,+20是为了去除一些边界值
textPaint.setARGB(255, mRandom.nextInt(200) + 20, mRandom.nextInt(200) + 20, mRandom.nextInt(200) + 20);
myCanvas.drawText(String.valueOf(tempCode.charAt(i)), i * charLength * 1.6f+30, mHeight * 2 / 3f, textPaint);
myCanvas.restore();

if (tempCode!=null&&tempCode.length()>0){
textPaint.getTextBounds(tempCode,0,codeNum,textBound);
float charLength=(textBound.width())/codeNum;
for (int i=0;i<codeNum;i++){
int offsetDegree=mRandom.nextInt(15);
// 这里只会产生0和1,如果是1那么正旋转正角度,否则旋转负角度
offsetDegree = mRandom.nextInt(2) == 1?offsetDegree:-offsetDegree;
myCanvas.save();
myCanvas.rotate(offsetDegree,mWidth/2,mHeight/2);
// 给画笔设置随机颜色,+20是为了去除一些边界值
textPaint.setARGB(255, mRandom.nextInt(200) + 20, mRandom.nextInt(200) + 20, mRandom.nextInt(200) + 20);
myCanvas.drawText(String.valueOf(tempCode.charAt(i)), i * charLength * 1.6f+30, mHeight * 2 / 3f, textPaint);
myCanvas.restore();
}
}
int index=0;
float bitmapwidth= mbitmap.getWidth();
Expand Down Expand Up @@ -213,22 +225,26 @@ private void createCodeBitmap() {
* @return 随机验证码
*/
public String getCharAndNumr() {
String val = "";
Random random = new Random();
for (int i = 0; i < codeNum; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equalsIgnoreCase(charOrNum)) {
// 取得大写字母还是小写字母
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextInt(26));
} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
val += String.valueOf(random.nextInt(10));
if (isNetCode){
return vCode;
}else {
String val = "";
Random random = new Random();
for (int i = 0; i < codeNum; i++) {
// 输出字母还是数字
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 字符串
if ("char".equalsIgnoreCase(charOrNum)) {
// 取得大写字母还是小写字母
int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (choice + random.nextInt(26));
} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
val += String.valueOf(random.nextInt(10));
}
}
vCode=val;
return val;
}
vCode=val;
return val;
}

/**
Expand All @@ -246,4 +262,13 @@ public void refreshCode(){
public String getvCode() {
return vCode;
}

/**
* set verification code, code get from net work
* @return verification code
*/
public void setvCode(String code) {
this.vCode=code;
refreshCode();
}
}
36 changes: 36 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -26,7 +27,42 @@
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_below="@id/verifycodeview"
android:text="Submit"/>

<FrameLayout
android:id="@+id/net_verifycodeview_layut"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="@id/btn_submit">
<ProgressBar
android:id="@+id/net_pregressbar"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
/>
<sgffsg.com.verifycodeview.VerificationCodeView
android:id="@+id/net_verifycodeview"
android:layout_width="100dp"
android:layout_height="40dp"
android:visibility="gone"
app:isNetCode="true"/>
</FrameLayout>

<EditText
android:id="@+id/net_edit_input"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:layout_below="@id/btn_submit"
android:layout_toLeftOf="@id/net_verifycodeview_layut"/>
<Button
android:id="@+id/net_btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/net_verifycodeview_layut"
android:text="Submit"/>

</RelativeLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="VerificationCodeView">
<attr name="isNetCode" format="boolean"/>
</declare-styleable>

</resources>

0 comments on commit 418dd30

Please sign in to comment.