0

我能够在单个 imageView 上实现拖动和缩放,从这里也是代码;

public class Touch extends Activity {
private static final String TAG = "Touch";
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
private PointF start = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;

// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

ImageView tv1;
LayoutParams layoutParams1;
ImageView tv2;
LayoutParams layoutParams2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final int windowwidth = getWindowManager().getDefaultDisplay()
            .getWidth();
    final int windowheight = getWindowManager().getDefaultDisplay()
            .getHeight();

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

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            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.imageView2);
    tv2.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            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"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="matrix"
    android:src="@drawable/ic_launcher" />

    </LinearLayout>

现在我的问题是我想动态添加多个 imageView 并在每个 imageView 上应用拖动、缩放。谁能帮我实现这一目标?我见过很多其他方式,但都有一些限制。

4

1 回答 1

0

示例中,您可以将图像拖动为图库视图并进行缩放。在这个例子中你有一个适配器类..所以你可以动态绑定图像。

以这种方式绑定...

for(int i=0;i<bmpList.size();i++)
            {
                ImageViewTouch imageView = new ImageViewTouch(Philately_image_view.this);
                imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                Options options = new Options();
                options.inSampleSize = 2;
                //Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), options);
                imageView.setImageBitmap(bmpList.get(i));
                arrayAdapter.add(imageView);


            }

            galleryTouch.setAdapter(arrayAdapter);

在这里我有位图的arraylist ...和GalleryTouch galleryTouch对象..

于 2012-11-20T09:44:09.677 回答