Move a picture anywhere on the touch screen by tapping and dragging, with touch events in Android. Create new project with Application name: Move Image; Minimum SDK: API 27Android 8.1 (Oreo). Add a picture (picture.png) in res->drawable folder. In app->res->layout->activity_main.xml write the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/picture" />

</RelativeLayout>
In app->java->MainActivity.java write the code:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ViewGroup mainLayout;
    private ImageView image;

    private int xDelta;
    private int yDelta;

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

        mainLayout = (RelativeLayout) findViewById(R.id.main);
        image = (ImageView) findViewById(R.id.image);

        image.setOnTouchListener(onTouchListener());
    }

    private OnTouchListener onTouchListener() {
        return new OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @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.topMargin;
                        break;

                    case MotionEvent.ACTION_UP:
                        Toast.makeText(MainActivity.this,
                                "I'm here!", Toast.LENGTH_SHORT)
                                .show();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                                .getLayoutParams();
                        layoutParams.leftMargin = x - xDelta;
                        layoutParams.topMargin = y - yDelta;
                        layoutParams.rightMargin = 0;
                        layoutParams.bottomMargin = 0;
                        view.setLayoutParams(layoutParams);
                        break;
                }

                mainLayout.invalidate();
                return true;
            }
        };
    }
}
Run app in mobile device or Emulator (API 27 Android 8.1) and see how it works. Follow video tutorial:
Download this app for free and install it on your mobile device to see how it works: https://android-coffee.com/wp-content/uploads/projecs/MoveImage.apk Download project with Source Code from here: https://android-coffee.com/wp-content/uploads/projecs/MoveImage.7z Please subscribe here to our YouTube channel. Thank you! Check next tutorials to learn all about Android. For further questions leave a message.

2 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

Hello Sir good afternoon!

I need your help. I want image size should not decrease, when I reached to the corner of screen.

I hope I’m not late.

follow the example

1: Go to Desingn
click on your image that will appear.
your image has 4 balls

in these four balls the left and the top are with arrows pointing to their respective function

now do the same with the one on the right and the one below
select the circle and pull it to the end of the edge of the screen it is on, this way it will be the same as the previous two

when you do that the ball will turn blue.

make sure the four balls are blue and pointing to their respective position

if there is no blue but it is pointing, select the circle again and pull to the edge corner

do not let go of the edge or it will not work.

success.

O Brasil é brilhante!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image Refresh

*