Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android multiple ImageView moving on touch

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,

like image 532
Nikhil Avatar asked Oct 19 '11 14:10

Nikhil


2 Answers

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
like image 163
Yuvi Avatar answered Sep 28 '22 18:09

Yuvi


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

like image 20
Alex Klimashevsky Avatar answered Sep 28 '22 18:09

Alex Klimashevsky