Android滑动拼图验证码控件使用方法详解

作者:tracydragonlxy 时间:2021-10-03 02:56:06 

简介: 很多软件为了安全防止恶意攻击,会在登录/注册时进行人机验证,常见的人机验证方式有:谷歌点击复选框进行验证,输入验证码验证,短信验证码,语音验证,文字按顺序选择在图片上点击,滑动拼图验证等。

效果图:

Android滑动拼图验证码控件使用方法详解

代码实现:

1、滑块视图类:SlideImageView.java。实现随机选取拼图位置,对拼图位置进行验证等功能。


public class SlideImageView extends View {

Bitmap bitmap;
   Bitmap drawBitmap;
   Bitmap verifyBitmap;
   boolean reset = true;

// 拼图的位置
   int x;
   int y;
   // 验证的地方
   int left, top, right, bottom;
   // 移动x坐标
   int moveX;
   // x坐标最大移动长度
   int moveMax;
   // 正确的拼图x坐标
   int trueX;

public SlideImageView(Context context) {
       super(context);
   }

public SlideImageView(Context context, AttributeSet attrs) {
       super(context, attrs);
   }

public SlideImageView(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
   }

@Override
   protected void onDraw(Canvas canvas) {
       super.onDraw(canvas);
       if (bitmap == null)
           return;

if (reset) {
           /*
   * 背景图
   */
           int width = getWidth();
           int height = getHeight();

drawBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

/*
      * 验证的地方
      */
           int length = Math.min(width, height);
           length /= 4;//1/4长度

// 随机选取拼图的位置
           x = new Random().nextInt(width - length * 2) + length;
           y = new Random().nextInt(height - length * 2) + length;

left = x;
           top = y;
           right = left + length;
           bottom = top + length;

//验证的图片
           verifyBitmap = Bitmap.createBitmap(drawBitmap, x, y, length, length);

// 验证图片的最大移动距离
           moveMax = width - length;
           // 正确的验证位置x
           trueX = x;

reset = false;
       }

Paint paint = new Paint();
       // 画背景图
       canvas.drawBitmap(drawBitmap, 0, 0, paint);
       paint.setColor(Color.parseColor("#66000000"));
       canvas.drawRect(left, top, right, bottom, paint);//画上阴影
       paint.setColor(Color.parseColor("#ffffffff"));
       canvas.drawBitmap(verifyBitmap, moveX, y, paint);//画验证图片

}

public void setImageBitmap(Bitmap bitmap) {
       this.bitmap = bitmap;
   }

public void setMove(double precent) {
       if (precent < 0 || precent > 1)
           return;

moveX = (int) (moveMax * precent);
       invalidate();
   }

public boolean isTrue(double range) {
       if (moveX > trueX * (1 - range) && moveX < trueX * (1 + range)) {
           return true;
       } else {
           return false;
       }
   }

public void setReDraw() {
       reset = true;
       invalidate();
   }
}

2、视图布局文件:activity_main.xml。


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.slideimage.MainActivity">

<com.slideimage.SlideImageView
       android:id="@+id/slide_image_view"
       android:layout_width="240dp"
       android:layout_height="150dp"
       android:layout_marginTop="50dp"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"/>

<View
       android:id="@+id/flash_view"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:visibility="invisible"
       app:layout_constraintLeft_toLeftOf="@id/slide_image_view"
       app:layout_constraintRight_toRightOf="@id/slide_image_view"
       app:layout_constraintTop_toTopOf="@id/slide_image_view"
       app:layout_constraintBottom_toBottomOf="@id/slide_image_view"
       android:background="@mipmap/drag_flash"/>

<SeekBar
       android:id="@+id/seekBar1"
       android:layout_width="240dp"
       android:layout_height="wrap_content"
       android:layout_marginTop="220dp"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"/>

<TextView
       android:id="@+id/show_result"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="280dp"
       android:textSize="20sp"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent"/>

<Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="320dp"
       android:text="重新初始化"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

3、在Activity中使用滑块验证:MainActivity.java。


public class MainActivity extends AppCompatActivity {

private SeekBar seekBar;
   private Button button1;
   private SlideImageView slideImageView;
   private TextView resultText;
   private View flashView;

private static final int flashTime = 800;

private long timeStart = 0;
   private float timeUsed;

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

seekBar = findViewById(R.id.seekBar1);
       button1 = findViewById(R.id.button1);
       slideImageView = findViewById(R.id.slide_image_view);
       flashView = findViewById(R.id.flash_view);
       resultText = findViewById(R.id.show_result);

slideImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.slide_bg));

seekBar.setMax(10000);
       seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

@Override
           public void onProgressChanged(SeekBar seekBar, int progress,
                                         boolean fromUser) {
               slideImageView.setMove(progress*0.0001);
           }

@Override
           public void onStartTrackingTouch(SeekBar seekBar) {
               timeStart = System.currentTimeMillis();
           }

@Override
           public void onStopTrackingTouch(SeekBar seekBar) {
           }

});

seekBar.setOnTouchListener(new View.OnTouchListener(){

@Override
           public boolean onTouch(View v, MotionEvent event) {
               switch(event.getAction()){
                   case MotionEvent.ACTION_UP:
                       timeUsed = (System.currentTimeMillis() - timeStart) / 1000.0f;
                       boolean isTrue = slideImageView.isTrue(0.1);//允许有10%误差
                       if(isTrue) {
                           flashShowAnime();
                           updateText("验证成功,耗时:" + timeUsed + "秒");
                       } else {
                           updateText("验证失败");
                       }
                       break;
               }
               return false;
           }

});

button1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               reInit();
           }
       });
   }

private void updateText(final String s) {
       runOnUiThread(new Runnable() {
           @Override
           public void run() {
               resultText.setText(s);
           }
       });
   }

private void reInit() {
       slideImageView.setReDraw();
       seekBar.setProgress(0);
       resultText.setText("");
       flashView.setVisibility(View.INVISIBLE);
   }

// 成功高亮动画
   private void flashShowAnime() {
       TranslateAnimation translateAnimation = new TranslateAnimation(
               Animation.RELATIVE_TO_SELF, 1f,
               Animation.RELATIVE_TO_SELF, -1f,
               Animation.RELATIVE_TO_SELF, 0f,
               Animation.RELATIVE_TO_SELF, 0f);
       translateAnimation.setDuration(flashTime);
       //translateAnimation.setInterpolator(new LinearInterpolator());
       flashView.setVisibility(View.VISIBLE);
       flashView.setAnimation(translateAnimation);
       translateAnimation.setAnimationListener(new Animation.AnimationListener() {
           @Override
           public void onAnimationStart(Animation animation) {

}

@Override
           public void onAnimationEnd(Animation animation) {
               flashView.setVisibility(View.INVISIBLE);
           }

@Override
           public void onAnimationRepeat(Animation animation) {

}
       });
   }

}

来源:https://blog.csdn.net/tracydragonlxy/article/details/105842780

标签:Android,滑动,验证码
0
投稿

猜你喜欢

  • SWT(JFace)体验之打开多个Form

    2021-11-29 03:19:15
  • 基于Java中的数值和集合详解

    2023-11-25 08:12:13
  • SpringBoot中使用Redis Stream实现消息监听示例

    2021-09-02 04:42:07
  • Go Java算法猜数字游戏示例详解

    2022-03-02 21:01:50
  • Java删除二叉搜索树最大元素和最小元素的方法详解

    2023-09-30 07:27:09
  • 适用于WebForm Mvc的Pager分页组件C#实现

    2022-05-11 22:11:34
  • Java日常练习题,每天进步一点点(40)

    2022-12-20 14:39:53
  • 解决@Transactional注解事务不回滚不起作用的问题

    2022-10-29 09:18:51
  • Linux系统中C语言编程创建函数fork()执行解析

    2023-06-21 01:10:03
  • c# 应用事务的简单实例

    2021-11-24 23:48:34
  • Flutter 分页功能表格控件详细解析

    2023-09-22 20:02:45
  • C#常用日期时间方法汇总

    2023-01-04 23:44:55
  • Java实现解析JSON大文件JsonReader工具详解

    2023-08-21 21:13:14
  • Android实现中轴旋转特效 Android制作别样的图片浏览器

    2023-07-03 00:37:16
  • Java中为什么start方法不能重复调用而run方法可以?

    2023-11-15 03:04:02
  • C# web.config之<customErrors>节点说明案例详解

    2023-07-06 20:20:15
  • InterProcessMutex实现zookeeper分布式锁原理

    2023-08-11 05:46:43
  • Spring Boot简介与快速搭建详细步骤

    2023-05-29 03:06:03
  • Java基础将Bean属性值放入Map中的实例

    2023-10-11 13:57:40
  • C# 重写Notification提示窗口的示例代码

    2021-12-26 19:57:59
  • asp之家 软件编程 m.aspxhome.com