This article shares the specific code for Android to implement View drag and drop for your reference. The specific content is as follows
Preface
The principle of realizing view drag and drop is actually very simple. It is nothing more than obtaining the displacement information of the finger, and then the view moves the corresponding position based on the displacement information of the finger.
First, obtain the displacement information of the mobile phone, and can be divided into two types according to different needs.
- Drag and drop the view itself, and the view can be moved. Then set the setOnTouchListener of the view.
- Slide randomly in the activity, and the view will reflect the action. Then override the activity onTouchEvent method.
There are several ways to move, there are also several ways to
- Set a tween animation for the view, the animation time is 0. (Not recommended, because the moving is only the shadow of the view, and the local area is still in place)
- Change the margin of the view. (Not recommended, it will affect the layout of viewgroup)
- According to the principle of attribute animation, change setTranslationX and setTranslationY. (This method is recommended, it 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) { (savedInstanceState); setContentView(.activity_view_test); mTv1= (TextView) findViewById(.tv01); (new () { @Override public boolean onTouch(View v, MotionEvent event) { double x=(); double y=(); (TAG, "onTouch: "+()); if (()==MotionEvent.ACTION_DOWN){ lastx=x; lastY=y; }else if (()==MotionEvent.ACTION_MOVE){ double dx=x-lastx; double dy=y-lastY; (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=(); // double y=(); // (TAG, "onTouch: "+()); // if (()==MotionEvent.ACTION_DOWN){ // lastx=x; // lastY=y; // }else if (()==MotionEvent.ACTION_MOVE){ // double dx=x-lastx; // double dy=y-lastY; // (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) { ((float) (()+dx)); ((float) (()+dy)); } //According to margin principle private void moveMethod1(double dx, double dy) { marginLayoutParams= () (); +=dx; +=dy; (marginLayoutParams); } private void startAnimation(double dx, double dy) { ObjectAnimator objectAnimator=(mTv1,"translationX", (float) (()+dx)).setDuration(3000); (); ObjectAnimator objectAnimator2=(mTv1,"translationY", (float) (()+dy)).setDuration(3000); (); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.