15

Here below is my xml File. It's name is main.xml


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:src="@drawable/icon"
        android:scaleType="matrix">
    </ImageView>
    <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:src="@drawable/bg_sound"
        android:scaleType="matrix"></ImageView>
</FrameLayout>

And My Java File is below


import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Test1Activity extends Activity implements OnTouchListener {
    private static final String TAG = "Touch";
    // These matrices will be used to move and zoom image
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();

    // We can be in one of these 3 states

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView view = (ImageView) findViewById(R.id.imageView);
        view.setOnTouchListener(this);

        ImageView view1 = (ImageView) findViewById(R.id.imageView1);
        view1.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView view = (ImageView) v;
        // Dump touch event to log

        // Handle touch events here...
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            break;
        case MotionEvent.ACTION_UP:
            savedMatrix.set(matrix);
            //matrix.postRotate(90);

            break;
        case MotionEvent.ACTION_MOVE:
                // ...
                matrix.set(savedMatrix);
                matrix.postTranslate(event.getX() - start.x, event.getY()
                        - start.y);
            break;
        }

        view.setImageMatrix(matrix);
        view.invalidate();
        return true; // indicate event was handled
    }

}

I am able to achieve move on touch but as there are 2 imageviews added only latest added imageviews is movable.

I guess problem is layout_width="fill_parent" which causes only front imageview to be recognized on touch. and If I am using layout_width="wrap_content" than imageview only moves in that image sized area and being invisible.

How to resolve this problem ?

Thanks,

surhidamatya
  • 2,419
  • 32
  • 56
Nikhil
  • 16,194
  • 20
  • 64
  • 81

4 Answers4

2

I almost solved your problem. Because of time I am not move forward I made some changes in your main.xml file a follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

        <ImageView android:id="@+id/imageView" android:layout_width="fill_parent"
            android:layout_height="100dp" android:src="@drawable/ic_launcher"
            android:scaleType="matrix"
            android:layout_alignParentTop="true"
            >
        </ImageView>
        <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:src="@drawable/toyota_altis"
            android:scaleType="matrix"
            android:layout_below="@+id/imageView"></ImageView>

</RelativeLayout>

Keep in mind use your own images and make appropriate changes Two Images I used are

  1. ic_launcher
  2. toyota_altis
j0k
  • 22,600
  • 28
  • 79
  • 90
Yuvi
  • 639
  • 6
  • 9
1

Only one view process onTouch action. so there a few options:

1) return "false" - it may help you

2) use "wrap_content" and don't use ImageMatrix. Move the left top corner of images

Zaz Gmy
  • 4,236
  • 3
  • 19
  • 30
Alex Klimashevsky
  • 2,457
  • 3
  • 26
  • 58
1

u can also add a toggle button and on click of the toggle button u can bring the imageview to front by using this

imageView1.bringToFront()/imageView2.bringToFront() 
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
0

Write Below Code into your Activity File.

int windowwidth = getWindowManager().getDefaultDisplay().getWidth();
int windowheight = getWindowManager().getDefaultDisplay().getHeight();


tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams1.leftMargin = x_cord - 25;
                layoutParams1.topMargin = y_cord - 75;
                tv1.setLayoutParams(layoutParams1);
                break;
            default:
                break;
        }
        return true;
    }
});

tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {         

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
        switch(event.getActionMasked())
        {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();
                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }
                layoutParams2.leftMargin = x_cord - 25;
                layoutParams2.topMargin = y_cord - 75;
                tv2.setLayoutParams(layoutParams2);
                break;
            default:
                break;
        }
        return true;
    }
});

main.xml File:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="50sp" android:layout_height="50sp"
        android:id="@+id/image" android:src="@drawable/image">
    </ImageView>
    <ImageView android:layout_y="30dip" android:layout_x="118dip"
        android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
        android:src="@drawable/image1">
    </ImageView>
</RelativeLayout>

and see below SO answer's link for more information.

Drag & Drop Two Images

Community
  • 1
  • 1
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128