Saturday, February 23, 2019

Drag view in activity

We can use service to drag view on screen. But we can do it in activity too.
To drag view vertical a layout, we do like this.
Create a file name keoview.xml in folder layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >
    <TextView
        android:layout_width="130dp"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:text="Đây là view kéo được"
        android:textColor="#FF0000" />
    <RelativeLayout
        android:id="@+id/root"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"     android:layout_gravity="center" >
        <TextView
         android:id="@+id/te"
         android:layout_width="wrap_content"
         android:layout_height="40dp"
         android:layout_centerVertical="true"
         android:background="#2292CB"
         android:text="Chạm để kéo" />
    </RelativeLayout>
</LinearLayout>
Here is the class to control.
public class keo extends Activity implements View.OnTouchListener {
     private int _yDelta;
     LayoutParams params;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.keoview);
          findViewById(R.id.te).setOnTouchListener(this);
          params = new WindowManager.LayoutParams(LayoutParams.WRAP_CONTENT,
                     LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_PHONE,
                     LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);   
}
@Override
public boolean onTouch(View view, MotionEvent event) {
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
          case MotionEvent.ACTION_DOWN:
               RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
                          .getLayoutParams();
               _yDelta = Y - lParams.bottomMargin;
               break;
          case MotionEvent.ACTION_UP:
               break;
          case MotionEvent.ACTION_POINTER_DOWN:
               break;
          case MotionEvent.ACTION_POINTER_UP:
               break;
          case MotionEvent.ACTION_MOVE:
               RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                          .getLayoutParams();
               layoutParams.bottomMargin = (Y - _yDelta);
               layoutParams.topMargin = -layoutParams.bottomMargin;
               view.setLayoutParams(layoutParams);
               view.animate().translationY(Y - _yDelta).setDuration(0);
               break;
          }
     findViewById(R.id.root).invalidate();
          return true;
     }
}
To drag view horizontal screen, in file keoview.xml change the layout_width of Linearlayout and RelativeLayout to android:layout_width="match_parent"
In class, declare one more variable.
private int _xDelta;
Command onTouch now like this.
@Override
public boolean onTouch(View view, MotionEvent event) {
          final int X = (int) event.getRawX();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
          case MotionEvent.ACTION_DOWN:
               RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
                          .getLayoutParams();
               _xDelta = X - lParams.leftMargin;
              
               break;
          case MotionEvent.ACTION_UP:
               break;
          case MotionEvent.ACTION_POINTER_DOWN:
               break;
          case MotionEvent.ACTION_POINTER_UP:
               break;
          case MotionEvent.ACTION_MOVE:
               RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                          .getLayoutParams();
              
               layoutParams.leftMargin = (X - _xDelta);
               layoutParams.rightMargin = -layoutParams.leftMargin;
               view.setLayoutParams(layoutParams);
               view.animate().translationX(X - _xDelta).setDuration(0);
               break;
          }
          findViewById(R.id.root).invalidate();
          return true;
   }
If want to drag free anywhere, we combine them like this.
@Override
public boolean onTouch(View view, MotionEvent event) {
     final int X = (int) event.getRawX();
     final int Y = (int) event.getRawY();
     switch (event.getAction() & MotionEvent.ACTION_MASK) {
     case MotionEvent.ACTION_DOWN:
          RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
                          .getLayoutParams();
               _xDelta = X - lParams.leftMargin;
               _yDelta = Y - lParams.bottomMargin;
               break;
          case MotionEvent.ACTION_UP:
               dem=dem+1;
               if(dem==2){
Toast.makeText(getApplicationContext(), "Touch view", Toast.LENGTH_LONG).show();
               dem=0;
               }
               break;
          case MotionEvent.ACTION_POINTER_DOWN:
               break;
          case MotionEvent.ACTION_POINTER_UP:
               break;
          case MotionEvent.ACTION_MOVE:
               RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                          .getLayoutParams();
               layoutParams.bottomMargin = (Y - _yDelta);
               layoutParams.topMargin = -layoutParams.bottomMargin;
              
               layoutParams.leftMargin = (X - _xDelta);
               layoutParams.rightMargin = -layoutParams.leftMargin;
               view.setLayoutParams(layoutParams);
               view.animate().translationX(X - _xDelta).setDuration(0);
               view.animate().translationY(Y - _yDelta).setDuration(0);
               break;
          }
          findViewById(R.id.root).invalidate();
          return true;
   }
Command in case MotionEvent.ACTION_UP: to detect when user touch screen two times.
You must add count variable name dem.

If you want to save new position of view, use SharedPreferences like this post.

No comments:

Post a Comment