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);
/画背景图/