Android Simulation Realizes Sliding Unlocking Interface

  • 2021-11-01 04:44:17
  • OfStack

In this paper, we share the Android simulation sliding unlocking interface for your reference. The specific contents are as follows

Implementation logic

Custom 1 view inherits view class to realize the method inside Load the image resource in the constructor. Get the width and height of the background as the width and height of the custom control in onMeasure Draw the slider in onDraw method, and set the background picture of the control in the layout file of the control Set the touch events of the slider, and calculate the position of the slider when the finger presses the screen, moves and leaves the screen respectively In the process of moving, the position of the slider is limited so that the position of the slider cannot exceed the specified area Determine the position of the finger in the event that the finger leaves the screen. If the slider does not reach the rightmost position, let the slider return to the starting position By overriding the computeScroll method, let the slider return to the starting position if it does not reach the rightmost position when the finger leaves the screen Set up 1 callback interface to monitor whether the slider reaches the rightmost side

Layout file


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center_horizontal"
tools:context="com.example.a7_.MainActivity">

<com.example.a7_.MyView
 android:layout_alignParentBottom="true"
 android:id="@+id/mv"
 android:background="@mipmap/lockviewbg"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</RelativeLayout>

Custom control code


package com.example.a7_;

 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.view.View;
 import android.widget.Scroller;

 /**
 * Created by Administrator on 2017.05.28.0028.
 */

 public class MyView extends View {

 private final Bitmap slidBitmap;
 private final Bitmap bgBitmap;
 private int startx;
 private int destance;
 private float endx;
 private final Scroller scroller;


 public MyView(Context context, AttributeSet attrs) {
 super(context, attrs);

 // Load picture resources 
 slidBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_button);
 bgBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.lockviewbg);
 // Create scroller Object 
 scroller = new Scroller(context);
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 // Set the width and height of the custom control ,, Take the background picture as the standard 
 setMeasuredDimension(bgBitmap.getWidth(),bgBitmap.getHeight());
 }

 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 // Draw a picture 
 canvas.drawBitmap(slidBitmap,0,0,null);
 }

 // Set the touch event of the slider 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 switch (event.getAction()){
  case MotionEvent.ACTION_DOWN:
  // When the finger is pressed, , Record coordinates 
  startx = (int) event.getX();
  break;
  case MotionEvent.ACTION_MOVE:
  // When the finger moves, , Record the distance moved 
  destance = (int)(event.getX()-startx);
  // The interval that limits the movement of the slider 
  if (destance<0){
   destance = 0;
  }else if (destance>bgBitmap.getWidth()-slidBitmap.getWidth()){
   destance = bgBitmap.getWidth()-slidBitmap.getWidth();
  }
  // Move the slider to this position 
  scrollTo( -destance,0);
  break;
  case MotionEvent.ACTION_UP:
  // When the finger leaves , Record the position where the finger leaves 
  if (destance<bgBitmap.getWidth()-slidBitmap.getWidth()){
   // When your fingers are raised , If the slider is not on the right , Then let him go back to the left 
   startx = destance;
   int dx = 0-destance;
   scroller.startScroll(startx,0,dx,0);
   invalidate();
  }else {
   // Open and unlock 
   listener.onUnlock(this);
  }
  break;
 }
 invalidate();
 return true;
 }

 // Rewrite computeScroll Method   Call invalidate After   Will call draw draw Will call this computeScroll
 @Override
 public void computeScroll() {
 if (scroller.computeScrollOffset()){
  // Gets the current position of the slider 
  int currx = scroller.getCurrX();
  scrollTo(-currx,0);
  // Redraw 
  invalidate();
 }
 }

 // Settings 1 Unlocked interfaces 
 interface onUnLockListener{
 void onUnlock(MyView view);
 }

 private onUnLockListener listener;

 public void setOnLockListener(onUnLockListener listener){
  this.listener = listener;
 }

 }

## Core code 

 package com.example.a7_;

 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;

 public class MainActivity extends AppCompatActivity {

 private MyView myView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // Initialize control 
 myView = (MyView) findViewById(R.id.mv);
 // Setting Listening Events 
 myView.setOnLockListener(new MyView.onUnLockListener() {
  @Override
  public void onUnlock(MyView view) {
  // Trigger monitoring , End page 
  finish();
  }
 });
 }
 }

For more articles on sliding function, please click on the topic: "Android sliding function"


Related articles: