How to move object with touch events on the mobile screen Drag and drop images or objects

How to move object with touch events on the mobile screen Drag and drop images or objects




Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.






activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relative_layout"
    tools:context=".MainActivity">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Drag &amp; Drop"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:src="@mipmap/ic_launcher_round" />



</RelativeLayout>


 







MainActivity.java


package com.usmtip.myapplication;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {


    //declare ui views
    private ImageView imageView;
    private RelativeLayout relativeLayout;

    private int xDelta;
    private int yDelta;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //init ui views
        imageView = findViewById(R.id.imageView);
        relativeLayout = findViewById(R.id.relative_layout);

        //setup layout params
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(800, 600);
        imageView.setLayoutParams(layoutParams);
        //setup touch listener
        imageView.setOnTouchListener(new CustomTouchListener());

    }

    private class CustomTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, 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) v.getLayoutParams();

                    xDelta = X - lParams.leftMargin;
                    yDelta = Y - lParams.topMargin;

                    break;

                case MotionEvent.ACTION_UP:
                    Toast.makeText(MainActivity.this, "Image is on new Location!", Toast.LENGTH_SHORT).show();
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
                    layoutParams.leftMargin = X - xDelta;
                    layoutParams.topMargin = Y - yDelta;
                    layoutParams.rightMargin = 0;
                    layoutParams.bottomMargin = 0;
                    v.setLayoutParams(layoutParams);
                    break;

            }
            relativeLayout.invalidate();
            return true;
        }


    }
}












All Server Links to Download This File:



Previous Post
Next Post

post written by:

0 Comments: