Android Studio实现图形验证码

源代码
源代码MainActivity
效果图32行需要修改,不修改会报错:需要常量表达式,我的代码已修改
在这里插入图片描述
点击后
在这里插入图片描述
MainActivity

import static com.example.graphicverificationcode.RxCaptcha.TYPE.NUMBER;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   
    TextView tvCode;
    ImageView ivCode;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvCode = findViewById(R.id.tv_code);
        ivCode = findViewById(R.id.iv_code);
        ivCode.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
   
        if (view.getId() == R.id.iv_code) {
   
            RxCaptcha.build()
                    .backColor(0xffffff)
                    .codeLength(4)
                    .fontSize(60)
                    .lineNumber(2)
                    .size(200, 70)
                    .type(NUMBER)
                    .into(ivCode);
            tvCode.setText(RxCaptcha.build().getCode());
        }
    }
}

RxCaptcha

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.ImageView;

import java.util.Random;

/**
 * 随机生成验证码,使用方法:
 * 

* 拿到验证码图片ImageView * mIvCode.setImageBitmap(RxCaptcha.getInstance().createBitmap()); * int code=RxCaptcha.getInstance().getCode(); *

* 只需生成验证码值 String * *

* RxCaptcha * * @version 1.0.0 */ public class RxCaptcha { public static RxCaptcha build() { if (rxCaptcha == null) { rxCaptcha = new RxCaptcha(); } return rxCaptcha; } private static final char[] CHARS_NUMBER = { '0',

你可能感兴趣的:(android,studio,android,ide)