移动&在单个活动上缩放多个imageView

时间:2012-11-20 09:38:57

标签: android imageview zoom move multi-touch

我可以在单个imageView上实现拖放和缩放,来自here这也是代码;

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。谁能帮我实现这个目标?我见过许多其他方法,但都有一些限制。

1 个答案:

答案 0 :(得分:0)

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

以这种方式绑定......

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

这里我有Bitmap ...和GalleryTouch galleryTouch对象的arraylist ..