Android:如何在触摸视图中移动所有对象(ACTION_MOVE)?

时间:2015-09-22 09:44:35

标签: android imageview move ontouchlistener

关于这个帖子的参考: android: move a view on touch move (ACTION_MOVE)

如果我在屏幕上移动手指,我想移动所有对象(例如两个ImageView)。

到目前为止我做了什么:

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.touchmoveimage);


    background = (ImageView) findViewById(R.id.background);
    image = (ImageView) findViewById(R.id.image);
    root = (ViewGroup) findViewById(R.id.root);
    root.setId(R.id.root);
    image.setId(R.id.image);
    background.setId(R.id.background);

    MyTouchListener touchListener = new MyTouchListener();
    Bitmap bitmap;
    bitmap = decodeFile(R.drawable.background);
    background.setImageBitmap(bitmap);
    image.setImageResource(R.drawable.fire);
    image.setOnTouchListener(touchListener);
    background.setOnTouchListener(touchListener);

    ((ViewGroup) image.getParent()).removeView(image);
    ((ViewGroup) background.getParent()).removeView(background);
    root.addView(background);
    root.addView(image);

这是MyTouchListener

public class MyTouchListener implements View.OnTouchListener{

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (view.getId()) {
            case R.id.background:
                ImageView view2 = (ImageView) view;
                view2.setScaleType(ImageView.ScaleType.MATRIX);
                float scale;

                dumpEvent(event);
                //Handle touch events here...

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN: //first finger down only
                        savedMatrix.set(matrix);
                        start.set(event.getX(), event.getY());
                        Log.d(TAG, "mode=DRAG");
                        mode = DRAG;
                        break;
                    case MotionEvent.ACTION_UP://first finger lifted

                    case MotionEvent.ACTION_POINTER_UP: //second finger lifted
                        mode = NONE;
                        Log.d(TAG, "mode=NONE");
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN: //first and second finger down
                        oldDist = spacing(event);
                        Log.d(TAG, "oldDist=" + oldDist);
                        if (oldDist > 10f) {
                            savedMatrix.set(matrix);
                            midPoint(mid, event);
                            mode = ZOOM;
                            Log.d(TAG, "mode=ZOOM");
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (mode == DRAG) {
                            matrix.set(savedMatrix);
                            image.setImageMatrix(matrix);
                            matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); //create the transformation in the matrix of points
                        } else if (mode == ZOOM) {
                            float[] f = new float[9];
                            //pinch zooming
                            float newDist = spacing(event);
                            Log.d(TAG, "newDist=" + newDist);

                            if (newDist > 10f) {
                                matrix.set(savedMatrix);
                                scale = newDist / oldDist; //setting the scaling of the matrix...
                                //if scale > 1 means zoom in
                                // if scale < 1 means zoom out
                                matrix.postScale(scale, scale, mid.x, mid.y);
                                //scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
                            }
                            matrix.getValues(f);
                            float scaleX = f[Matrix.MSCALE_X];
                            float scaleY = f[Matrix.MSCALE_Y];

                            //Einstellungen minimaler oder maximaler zoom
                            if (scaleX <= MIN_ZOOM) {
                                matrix.postScale((MIN_ZOOM) / scaleX, (MIN_ZOOM) / scaleY, mid.x, mid.y);

                            } else if (scaleX >= MAX_ZOOM) {
                                matrix.postScale((MAX_ZOOM) / scaleX, (MAX_ZOOM) / scaleY, mid.x, mid.y);
                            }
                        }
                        break;
                }
                view2.setImageMatrix(matrix); //display the transformation on screen
                break;
            case R.id.image:
                final int X = (int) event.getRawX();
                final int Y = (int) event.getRawY();
                //Definition MotionEvent.ACTION_MASK: Bit mask of the parts of the action code that are the action itself.
                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:
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                                .getLayoutParams();

                        layoutParams.leftMargin = X - _xDelta;
                        layoutParams.topMargin = Y - _yDelta;
                        layoutParams.rightMargin = -250;
                        layoutParams.bottomMargin = -250;
                        view.setLayoutParams(layoutParams);
                        break;

                }
                root.invalidate();

                break;
        }
        return true;
    }
}

这是我布局的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/skizze"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="matrix" />

    <ImageView
        android:id="@+id/feuerloescherID"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_alignParentTop="false"
        android:layout_gravity="center_horizontal|top" />


</RelativeLayout>

我希望我已经向您提供了所有相关信息。 我需要在代码中添加/更改什么才能在我的布局中移动所有对象?

亲切的问候!

0 个答案:

没有答案