Android Custom View Realization of Automatic Adsorption Function

  • 2021-11-14 07:13:51
  • OfStack

In this paper, we share the specific code of Android to realize automatic adsorption function for your reference. The specific contents are as follows

1. Brief introduction

In the recent development of app, it is necessary to realize the automatic adsorption function after dragging view, so it is necessary to customize view to use animation in onTouchEvent to realize this function

2. Function code section


import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

public class AdsorbentViews extends ImageView {
 
 private int maxWidth;
  private int maxHeight;
  private int viewWidth;
  private int viewHeight;
  private float downx;
  private float downy;
  private Context mContext;
  public CustomViews(Context context) {
    this(context, null);
  }
 
  public CustomViews(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
 
  public CustomViews(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
  }
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    DisplayMetrics outMetrics = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getRealMetrics(outMetrics);
    // Width of screen 
    maxWidth = outMetrics.widthPixels;
    // Height of screen 
    maxHeight = outMetrics.heightPixels;
    /**
     *  Width and height of the control 
     */
    viewWidth = canvas.getWidth();
    viewHeight = canvas.getHeight();
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        clearAnimation();
        downx = event.getX();
        downy = event.getY();
        return true;
      case MotionEvent.ACTION_MOVE:
        float moveX = event.getRawX() - downx;
        float moveY = event.getRawY() - downy;
        moveX = moveX < 0 ? 0 : (moveX + viewWidth > maxWidth) ? (maxWidth - viewWidth) : moveX;
        moveY = moveY < 0 ? 0 : (moveY + viewHeight) > maxHeight ? (maxHeight - viewHeight) : moveY;
        this.setY(moveY);
        this.setX(moveX);
        return true;
      case MotionEvent.ACTION_UP:
        // Do adsorption effect 
        float centerX = getX() + viewWidth / 2;
        if (centerX > maxWidth/2){
          // Right side adsorption 
          animate().setInterpolator(new DecelerateInterpolator())
              .setDuration(500)
              .x(maxWidth-viewWidth)
              .y(maxHeight-viewHeight)
              .start();
        }else {
          animate().setInterpolator(new DecelerateInterpolator())
              .setDuration(500)
              .x(0)
              .y(maxHeight-viewHeight)
              .start();
        }
        return true;
      default:
        return super.onTouchEvent(event);
    }
  }
}

Related articles: