2

我的要求是动态生成多个图像并对其执行所有操作,如拖放、缩放、旋转。我希望每个图像都可以在整个布局上移动。但问题是在哪个区域 1 图像可以移动另一个图像不能移动。我希望用户可以随时选择任何图像,并且任何图像都可以在布局中的任何位置移动。

我的代码在onclick内随机生成图像

LinearLayout.LayoutParams imParams = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    imge = new ImageView(MultiTouch.this);
    imge.setImageResource(R.drawable.ic_launcher);
    imge.setScaleType(ScaleType.MATRIX);

    imge.buildDrawingCache();
    BitmapDrawable drawable = (BitmapDrawable) imge.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    imge.setId(i++);

    ll.addView(imge, imParams);

我的 ontouch listnner :

  public boolean onTouch(View v, MotionEvent event) {
        // Toast.makeText(MultiTouch.this, "onTouch", Toast.LENGTH_LONG).show();
        ImageView view = (ImageView) v;

         boolean defaultResult = v.onTouchEvent(event);



        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:



            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            mode = DRAG;
            lastEvent = null;
            System.out.println("ACTION_DOWN     " );

            break;


    case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                }
                lastEvent = new float[4];
                lastEvent[0] = event.getX(0);
                lastEvent[1] = event.getX(1);
                lastEvent[2] = event.getY(0);
                lastEvent[3] = event.getY(1);
                d = rotation(event);
                System.out.println("ACTION_POINTER_DOWN     " );
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                lastEvent = null;
                v.clearFocus();
                System.out.println("ACTION_POINTER_UP     " );
                return false;
    //          break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    matrix.set(savedMatrix);
                    float dx = event.getX() - start.x;
                    float dy = event.getY() - start.y;
                    matrix.postTranslate(dx, dy);
                } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = (newDist / oldDist);
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                    if (lastEvent != null && event.getPointerCount() == 2) {
                        newRot = rotation(event);
                        float r = newRot - d;
                        float[] values = new float[9];
                        matrix.getValues(values);
                        float tx = values[2];
                        float ty = values[5];
                        float sx = values[0];
                        float xc = (view.getWidth() / 2) * sx;
                        float yc = (view.getHeight() / 2) * sx;
                        // matrix.postRotate(r, tx + xc, ty + yc);
                        matrix.postRotate(r, view.getWidth() / 2,
                                view.getHeight() / 2);
                    }
                }

                System.out.println("ACTION_MOVE     " );
                break;
             default:
                  return defaultResult;
            }

            view.setImageMatrix(matrix);

            return super.onTouchEvent(event);
        }

和我的 xml 代码:

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

    android:layout_height="fill_parent"
    >



    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button" />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:background="@drawable/backgrd"
        android:orientation="vertical" >

    </LinearLayout>

</RelativeLayout>

谁能告诉我如何在整个布局中随时移动任何图像?我需要制作图层吗,如果是的话怎么做。

4

0 回答 0