Android滑动拼图验证码控件使用方法详解
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) {
}
});
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播