Android-自定义点选文字验证,看完直接怼项目经理

本文介绍了如何在Android中自定义一个滑动验证码视图(SlidingVerificationView),包括Bitmap处理、触摸事件监听和验证逻辑。通过实例代码展示了如何创建背景图、剪裁图片并响应用户触摸操作以验证成功或失败。
摘要由CSDN通过智能技术生成

import android.graphics.Color;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.graphics.PorterDuff;

import android.graphics.PorterDuffXfermode;

import android.graphics.Rect;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.Toast;

import java.util.Random;

public class SlidingVerificationView extends View {

private Bitmap bgBitmap;

private Bitmap newBgBitmap;

private Bitmap srcBitmap;

private Paint paintShadow;

private Paint paintSrc;

private float curX;

private float lastX;

private int dx;

private int shadowSize = dp2px(60);

private int padding = dp2px(40);

private int shadowLeft;

private int srcLeft = padding;

private int width, height;

private Paint bgPaint;

private OnVerifyListener listener;

public SlidingVerificationView(Context context) {

this(context, null);

}

public SlidingVerificationView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public SlidingVerificationView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

paintShadow = new Paint();

paintShadow.setAntiAlias(true);

paintShadow.setColor(Color.parseColor("#AA000000"));

paintSrc = new Paint();

paintSrc.setAntiAlias(true);

paintSrc.setFilterBitmap(true);

paintSrc.setStyle(Paint.Style.FILL_AND_STROKE);

paintSrc.setColor(Color.WHITE);

bgPaint = new Paint();

bgPaint.setMaskFilter(new BlurMaskFilter(5, BlurMaskFilter.Blur.OUTER));

bgPaint.setAntiAlias(true);

bgPaint.setStyle(Paint.Style.FILL);

bgBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.syzt);

}

public void setVerifyListener(OnVerifyListener listener) {

this.listener = listener;

}

public Bitmap clipBitmap(Bitmap bm, int newWidth, int newHeight) {

int width = bm.getWidth();

int height = bm.getHeight();

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

}

public Bitmap createSmallBitmap(Bitmap var) {

Bitmap bitmap = Bitmap.createBitmap(shadowSize, shadowSize, Bitmap.Config.ARGB_8888);

Canvas canvas1 = new Canvas(bitmap);

canvas1.drawCircle(shadowSize / 2, shadowSize / 2, shadowSize / 2, paintSrc);

/设置混合模式/

paintSrc.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

/在指定范围随机生成空缺部分坐标,保证空缺部分出现在View右侧/

int min = width / 3;

int max = width - shadowSize / 2 - padding;

Random random = new Random();

shadowLeft = random.nextInt(max) % (max - min + 1) + min;

Rect rect = new Rect(shadowLeft, (height - shadowSize) / 2, shadowSize + shadowLeft, (height + shadowSize) / 2);

RectF rectF = new RectF(0, 0, shadowSize, shadowSize);

canvas1.drawBitmap(var, rect, rectF, paintSrc);

paintSrc.setXfermode(null);

return bitmap;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

curX = event.getRawX();

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

lastX = event.getRawX();

break;

case MotionEvent.ACTION_MOVE:

dx = (int) (curX - lastX);

srcLeft = dx + padding;

invalidate();

break;

case MotionEvent.ACTION_UP:

boolean isSuccess = Math.abs(srcLeft - shadowLeft) < 8;

if (isSuccess) {

Toast.makeText(getContext(), “验证成功!”, Toast.LENGTH_SHORT).show();

Log.d(“w”, “check success!”);

} else {

Toast.makeText(getContext(), “验证失败!”, Toast.LENGTH_SHORT).show();

Log.d(“w”, “check fail!”);

srcBitmap = createSmallBitmap(newBgBitmap);

srcLeft = padding;

invalidate();

}

if (listener != null) {

listener.onResult(isSuccess);

}

break;

}

return true;

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, he

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 免费领取

ightMeasureSpec);

int minimumWidth = getSuggestedMinimumWidth();

/根据原背景图宽高比设置画布尺寸/

width = measureSize(minimumWidth, widthMeasureSpec);

float scale = width / (float) bgBitmap.getWidth();

height = (int) (bgBitmap.getHeight() * scale);

setMeasuredDimension(width, height);

/根据画布尺寸生成相同尺寸的背景图/

newBgBitmap = clipBitmap(bgBitmap, width, height);

/根据新的背景图生成填充部分/

srcBitmap = createSmallBitmap(newBgBitmap);

}

private int measureSize(int defaultSize, int measureSpec) {

int mode = MeasureSpec.getMode(measureSpec);

int size = MeasureSpec.getSize(measureSpec);

int result = defaultSize;

switch (mode) {

case MeasureSpec.UNSPECIFIED:

result = defaultSize;

break;

case MeasureSpec.AT_MOST:

case MeasureSpec.EXACTLY:

result = size;

break;

}

return result;

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

RectF rectF = new RectF(0, 0, width, height);

/画背景图/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值