How to customize View in Android to achieve sliding Verification effect
BlockSize & & dY
< (bottom - top)) { isBlockArea = true; } return true; case MotionEvent.ACTION_MOVE: if (isBlockArea) { mX = (int) event.getX() - dX; //设置范围 if ((blockWidth + blockSize + mX) < parentWidth && (blockSize + mX) >= blockSize) {/ / calculate offset invalidate (); startX = (int) event.getX ()-blockWidth / 2 } else if ((blockSize + mX) > = blockSize) {/ / over reset mX = (int) parentWidth-blockWidth-blockSize; invalidate ();} isMove = true;} return true Case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: isBlockArea = false; isFinish = mRight = = parentWidth-blockSize; if (isFinish) {/ / listen callback if (finishListener! = null) {finishListener.finish () }} if (! isFinish & & isMove) {reset (startX);} break;} return super.onTouchEvent (event) } / * release springback animation * / private void reset (int start) {ValueAnimator valueAnimator = ValueAnimator.ofInt (start, 0); valueAnimator.setDuration (500); valueAnimator.start (); valueAnimator.addUpdateListener (animation-> {mX = (int) animation.getAnimatedValue (); / / Refresh invalidate ();}) ValueAnimator.addListener (new AnimatorListenerAdapter () {@ Override public void onAnimationEnd (Animator animation) {isMove = false; isFinish = false; startX = 0;}});} / * get the measured size * / private int getMyWSize (int measureSpec) {int result Int specMode = MeasureSpec.getMode (measureSpec); int specSize = MeasureSpec.getSize (measureSpec); if (specMode = = MeasureSpec.EXACTLY) {result = specSize;// exact size, so give the resulting size to view} else if (specMode = = MeasureSpec.AT_MOST) {result = Math.min (getScreenWidth ()-20, specSize);} else {result = getScreenWidth ()-20 } return result;} / * get the measured size * / private int getMyHSize (int measureSpec) {int result; int specMode = MeasureSpec.getMode (measureSpec); int specSize = MeasureSpec.getSize (measureSpec); if (specMode = = MeasureSpec.EXACTLY) {result = specSize / / exact size, so give the resulting size to view} else if (specMode = = MeasureSpec.AT_MOST) {result = Math.min (DEFAULT_HEIGHT, specSize);} else {result = DEFAULT_HEIGHT-20;} return result } / * get screen width * / private int getScreenWidth () {WindowManager windowManager = (WindowManager) getContext () .getSystemService (Context.WINDOW_SERVICE); DisplayMetrics displayMetrics = new DisplayMetrics (); windowManager.getDefaultDisplay () .getMetrics (displayMetrics); return displayMetrics.widthPixels;} / * * API callback method * / public interface FinishListener {void finish () }}
Usage
At this point, I believe you have a deeper understanding of "Android how to customize View to achieve sliding verification effect". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!