Android Realizes View Drag and Drop

  • 2021-12-04 19:38:15
  • OfStack

In this article, we share the specific code of Android to realize View drag and drop for your reference. The specific contents are as follows

Preface

To realize the drag and drop of View, the principle is actually very simple. It is nothing more than obtaining the displacement information of fingers, and then view moves the corresponding position according to the displacement information of fingers.

Firstly, the displacement information of mobile phone can be divided into two types according to different needs

Drag the view itself and the view moves. setOnTouchListener of view is set. Slide freely in activity, and view will reflect the action. The activity onTouchEvent method is overridden.

And there are several ways to move

Give view an inter-animation with an animation time of 0. (Not recommended, because only the shadow of view is moved, and the local area is still in place) Change margin for view. (Not recommended, it will affect the layout of viewgroup) Change setTranslationX and setTranslationY according to the principle of attribute animation. (This method is recommended and will not affect the layout of the principle.)

Code


public class ViewTestActivity extends AppCompatActivity {
  private static final String TAG = "ViewTestActivity";
  private TextView mTv1,mTv2;
  private double lastx,lastY;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_test);
    mTv1= (TextView) findViewById(R.id.tv01);
    mTv1.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        double x=event.getRawX();
        double y=event.getRawY();
        Log.d(TAG, "onTouch: "+event.getAction());
        if (event.getAction()==MotionEvent.ACTION_DOWN){
          lastx=x;
          lastY=y;
        }else if (event.getAction()==MotionEvent.ACTION_MOVE){
          double dx=x-lastx;
          double dy=y-lastY;
          Log.d(TAG, "onTouch: dx=="+dx+",dy=="+dy);
//      startAnimation(dx,dy);

          // moveMethod1(dx, dy);
          moveMethod2(dx, dy);

          lastx=x;
          lastY=y;
        }
        return true;
      }
    });
  }

//  @Override
//  public boolean onTouchEvent(MotionEvent event) {
//    double x=event.getRawX();
//    double y=event.getRawY();
//    Log.d(TAG, "onTouch: "+event.getAction());
//    if (event.getAction()==MotionEvent.ACTION_DOWN){
//      lastx=x;
//      lastY=y;
//    }else if (event.getAction()==MotionEvent.ACTION_MOVE){
//      double dx=x-lastx;
//      double dy=y-lastY;
//      Log.d(TAG, "onTouch: dx=="+dx+",dy=="+dy);
////      startAnimation(dx,dy);
//
//     // moveMethod1(dx, dy);
//      moveMethod2(dx, dy);
//
//      lastx=x;
//      lastY=y;
//    }
//    return true;
//  }

  // According to the principle of attribute animation 
  private void moveMethod2(double dx, double dy) {
    
    mTv1.setTranslationX((float) (mTv1.getTranslationX()+dx));
    mTv1.setTranslationY((float) (mTv1.getTranslationY()+dy));
  }
  
  // According to margin  Principle 
  private void moveMethod1(double dx, double dy) {
    ViewGroup.MarginLayoutParams marginLayoutParams= (ViewGroup.MarginLayoutParams) mTv1.getLayoutParams();
    marginLayoutParams.leftMargin+=dx;
    marginLayoutParams.topMargin+=dy;
    mTv1.setLayoutParams(marginLayoutParams);
  }

  private void startAnimation(double dx, double dy) {
    ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(mTv1,"translationX", (float) (mTv1.getTranslationX()+dx)).setDuration(3000);
    objectAnimator.start();

    ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(mTv1,"translationY", (float) (mTv1.getTranslationY()+dy)).setDuration(3000);
    objectAnimator2.start();
  }
}

Related articles: