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);}@Overrideprotected 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;// 正确的验证位置xtrueX = 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();}
}
  1. 视图布局文件:activity_main.xml。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns: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.SlideImageViewandroid: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"/><Viewandroid: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"/><SeekBarandroid: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"/><TextViewandroid: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"/><Buttonandroid: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>
  1. 在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")@Overrideprotected 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(){@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {slideImageView.setMove(progress*0.0001);}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {timeStart = System.currentTimeMillis();}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}});seekBar.setOnTouchListener(new View.OnTouchListener(){@Overridepublic 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() {@Overridepublic void onClick(View view) {reInit();}});}private void updateText(final String s) {runOnUiThread(new Runnable() {@Overridepublic 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() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {flashView.setVisibility(View.INVISIBLE);}@Overridepublic void onAnimationRepeat(Animation animation) {}});}}

Android 滑动拼图验证码控件相关推荐

  1. 小视频app源码,Android 滑动拼图验证码控件

    小视频app源码,Android 滑动拼图验证码控件 代码实现: 滑块视图类:SlideImageView.java.实现小视频APP源码随机选取拼图位置,对拼图位置进行验证等功能. public c ...

  2. Android滑块拼图验证码控件

    转自  王小冉的文章:https://blog.csdn.net/w690333243/article/details/90311187 https://github.com/luozhanming/ ...

  3. 仿斗鱼滑动拼图验证码控件

    今日科技快讯 上个月,谷歌在应用商店推出了一款重新设计的谷歌键盘(Google keyboard)APP,更名为Gboard,带有新的功能和几项升级.而近日,这款应用的下载量已经突破5亿,创下了应用商 ...

  4. Android拼图滑块验证码控件

    大咖好,博主毕业工作半年多了.今天给大噶呈献博主博客处女作--Android拼图滑块验证码控件.由于初写博客,很多地方可能不够好,望各位多多给点意见.工作半年才送出第一篇博客很惭愧555. 概述 验证 ...

  5. Android开源库集合(控件)

    RecycleView: RecycleView功能增强 https://github.com/Malinskiy/SuperRecyclerView RecycleView功能增强(拖拽,滑动删除, ...

  6. 滑动拼图验证码操作步骤:_拼图项目:延期的后果

    滑动拼图验证码操作步骤: Mark Reinhold先生于2012年7月宣布 ,他们计划从Java 8撤消Jigsaw项目 ,因为Jigsaw计划于2013年9月(从现在开始一年)推迟其发布. 这个日 ...

  7. Android 画布Canvas之控件连线操作

    一.需求: 1.在画布中的控件A长按能进行控件的连接,只有在控件B范围内抬起控件之间的连线才能连接成功: 2.当控件连线成功后,拖动控件AB之间的连线随着控件的拖动随之变化: 3.控件连线在屏幕上随着 ...

  8. android自定义刻度线,Android自定义控件之刻度尺控件

    今天我做的是一个自定义刻度尺控件,由于项目需求需要使用刻度尺那样滑动选择,由于对自定义控件的认识还不够深入,于是花了一周多时间才把这个控件给整出来,也是呕心沥血的经历啊,也让我对自定义控件有了自己的认 ...

  9. Android常用酷炫控件(开源项目)github地址汇总

    转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...

最新文章

  1. LeetCode简单题之长按键入
  2. 显示电池电量的小工具
  3. ubuntu 下通过 sh 命令运行脚本产生如下错误:[: y: unexpected operator
  4. 2020全国大学生数学建模竞赛【论文格式、时间节点及作品提交要求、竞赛题目下载、评分要点】【微信公众号:校苑数模】
  5. B--Bookshelf 2
  6. linux系统迁移的重要配置文件,mylinuxbackup
  7. github怎么隐藏自己的pr记录_记便签的软件哪个好?怎么及时记录自己的想法
  8. 将openstack的Token认证信息存储在memcache中
  9. Windows 2000/XP中对窗口进行透明化
  10. 进程环境之命令行参数
  11. 去除Xcode6创建工程时自带的storyboard
  12. 推荐一款好用的jeDate日期控件
  13. w7计算机快捷键设置方法,win7快捷键设置,windows常用24个快捷键
  14. 开源的文件服务器有哪些,开源文件服务器
  15. SpringBoot构建电商基础秒杀项目
  16. ftp服务器文件保存位置,ftp服务器和文件保存路径
  17. 金蝶系统服务器名称填什么,金蝶怎样输入服务器地址
  18. 人与自然超越彩虹-上
  19. 2019阿里暑期实习一面
  20. 基于深度信念网络的硬件模拟器研究(Matlab代码实现)

热门文章

  1. 各CCFA类核心期刊的信息汇总与评价总结(科技领域)
  2. python爬虫表格中清除空格_Pandas中如何去掉空格
  3. js数组遍历方法总结
  4. matlab矩阵程序,matlab矩阵基本操作
  5. linux如何批量处理图片名,Linux下批量修改图片名称的方法详解
  6. vue3的v-model
  7. 劳动仲裁成功的几率大吗?
  8. Android C++ Native 内存泄露检查工具Raphael使用介绍
  9. 一种从文本语义信息中抽取地理属性的社会感知新方法(内附公众号)
  10. [转载]手机号码IP地址归属地查询