【问题标题】:Move & Zoom multiple imageViews on a single Activity在单个 Activity 上移动和缩放多个 imageView
【发布时间】:2012-11-20 09:38:57
【问题描述】:

我能够在单个 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 上应用拖动、缩放。谁能帮我实现这一目标?我见过很多其他方法,但都有一些限制。

【问题讨论】:

    标签: android imageview zooming move multi-touch


    【解决方案1】:

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

    这里我有位图的数组列表...和 ​​GalleryTouch galleryTouch 对象..

    【讨论】:

    • 此示例从厨房加载随机图像,但我无法移动或缩放该图像
    • github.com/kilaka/ImageViewZoom/blob/master/ImageViewTouchTest/… ... 看到这个 galeryTousttest.java... 并从你的数组中加载图像 .. 所以你在那里做些细微的改变
    • 该类从图库中获取图像并将图像的arrayAdapter 放在图库小部件上。我需要在缩放时动态添加图像视图,单独移动这些图像。这通常发生在图像拼贴应用程序中
    • 但我不想在画廊中显示它们,我想在单个屏幕上移动缩放和旋转。就像在单个活动上制作图像拼贴一样。
    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 2012-11-17
    • 2012-07-08
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    相关资源
    最近更新 更多