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) {

  }

  });

  }

  }