English 中文(简体)
多元 图像观察
原标题:Android multiple ImageView moving on touch

下面是我的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 档案如下:


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="liver_other", which causes only front cultureview to be recognized onpass. 如果我使用<代码>layout_width=“wrap_content”,则光是图像调查在该图像区移动,而且看不见。

如何解决这一问题?

感谢

问题回答

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

只有一种观点程序<代码>on Touch。 因此,有一些选择:

(1) 退还“<条码>false - 并可帮助您

(2) 使用“<条码>wrap_content ,不使用<条码>。 1. 改变图像左上角

u还可以添加一个角球纽,而点击gle球纽,则可以通过利用这个途径,使图像面貌得以向前。

imageView1.bringToFront()/imageView2.bringToFront() 

摘录

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;
    }
});

主编:xml 文件:

<?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>

详见下文SO答复。

Drag & Drop Two Values





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签