Search code examples
androidontouchlistener

How to create image each time the user touch on screen


There are two ImageViews in my RelativeLayout. I can move the image by touching and dragging it on anywhere on screen. The images are in drawable folder

But now I want the followings:

  1. where ever the user touch on screen it should show another same image.
  2. keep displaying image (without removing previous one) on dragging the image.

Sample Screenshot of what I want to do

activity_main.xml

<?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:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/backgroundIv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:src="@drawable/windows_xp_desktop" />

    <ImageView
        android:id="@+id/foregroundIv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:scaleType="fitXY"
        android:src="@drawable/win_xp_error" />

</RelativeLayout>

Main Activity.java

package com.example.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    ImageView backgroundIv, foregroundIv;
    RelativeLayout relativeLayout;

    private ViewGroup rootLayout;
    private int xDelta;
    private int yDelta;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Hiding Title bar of this activity screen */
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        //Making this activity, full screen */
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        backgroundIv = findViewById(R.id.backgroundIv);
        foregroundIv = findViewById(R.id.foregroundIv);
        relativeLayout = findViewById(R.id.relativeLayout);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(350, 200);
        foregroundIv.setLayoutParams(layoutParams);
        foregroundIv.setOnTouchListener(new ChoiceTouchListener());
    }

    private class ChoiceTouchListener 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:
                    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 = -250;
                    layoutParams.bottomMargin = -250;
                    v.setLayoutParams(layoutParams);
                    break;

            }
            relativeLayout.invalidate();
            return true;
        }
    }

}

Solution

  • Do it like this

    public class MainActivity extends AppCompatActivity {
    
        ImageView backgroundIv, foregroundIv;
        RelativeLayout relativeLayout;
    
        private ViewGroup rootLayout;
        private int xDelta;
        private int yDelta;
        private ChoiceTouchListener listener;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);
            foregroundIv = findViewById(R.id.foregroundIv);
            relativeLayout = findViewById(R.id.relativeLayout);
    
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(350, 200);
            foregroundIv.setLayoutParams(layoutParams);
            listener = new ChoiceTouchListener();
            foregroundIv.setOnTouchListener(listener);
        }
    
        private void addNewImage(int X, int Y) {
            ImageView img = new ImageView(this);
            img.setImageResource(R.drawable.mcblauhp);
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            img.setOnTouchListener(listener);
            relativeLayout.addView(img);
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) img.getLayoutParams();
            lParams.leftMargin = X;
            lParams.topMargin = Y;
            img.setLayoutParams(lParams);
        }
    
        private class ChoiceTouchListener 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;
                        //addNewImage(lParams.leftMargin, lParams.topMargin);
                        //v.bringToFront(); // this is to ensure that the moved image is always on top
                        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) v.getLayoutParams();
                        addNewImage(layoutParams.leftMargin, layoutParams.topMargin);
                        v.bringToFront();
                        layoutParams.leftMargin = X - xDelta;
                        layoutParams.topMargin = Y - yDelta;
                        layoutParams.rightMargin = -250;
                        layoutParams.bottomMargin = -250;
                        v.setLayoutParams(layoutParams);
                        break;
    
                }
                relativeLayout.invalidate();
                return true;
            }
        }
    }