如何同时缩放两个ImageView?

时间:2016-07-07 16:53:18

标签: android android-imageview image-zoom

我想同时点击和放大两个ImageView。换句话说,我想放大左视图,在其他ImageView中看到完全相同的效果,没有任何延迟。

是否有图书馆已经这样做了?我没有任何成功地环顾四周:(

由于

3 个答案:

答案 0 :(得分:7)

实际上,使用library时非常简单:

布局activity_main

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="30dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/photo1"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/photo2"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1" />

</LinearLayout>

活动中的代码

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

    ImageView pic1 = (ImageView) findViewById(R.id.photo1);
    ImageView pic2 = (ImageView) findViewById(R.id.photo2);

    Drawable bitmap = getResources().getDrawable(R.mipmap.ic_launcher);
    pic1.setImageDrawable(bitmap);
    pic2.setImageDrawable(bitmap);

    PhotoViewAttacher photoViewAttacher1 = new PhotoViewAttacher(pic1);
    final PhotoViewAttacher photoViewAttacher2 = new PhotoViewAttacher(pic2);
    photoViewAttacher1.setOnScaleChangeListener(new PhotoViewAttacher.OnScaleChangeListener() {
        @Override
        public void onScaleChange(float v, float v1, float v2) {
            photoViewAttacher2.onScale(v, v1, v2);
        }
    });
}

结果:

enter image description here

答案 1 :(得分:3)

实际上让它工作(没有飞行......如果你能用飞行做的话会很棒)我必须覆盖PhotoViewAttacher方法并将它们链接到其他方法。实际上我不希望只重新启动比例选项,但也拖延了。我是这样做的:

private boolean DEBUG = true;
private boolean actionL = false;
private boolean actionR = false;
private boolean scalingL = false;
private boolean scalingR = false;

//[...]

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    photoViewAttacherL = new PhotoViewAttacher(photoL){
        @Override
        public void onDrag(float dx, float dy) {
            if (!scalingL && !actionL) {
                actionL = true;
                if (DEBUG) Dbg.d("DRAG", "x: " + dx + " y: " + dy);
                photoViewAttacherR.onDrag(dx, dy);
                super.onDrag(dx, dy);
                actionL = false;
            }
        }

        @Override
        public void onFling(float startX, float startY, float velocityX, float velocityY) {
        }

        @Override
        public boolean onTouch(View v, MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_UP){
                scalingL = false;
                scalingR = false;
            }
            return super.onTouch(v, ev);
        }

        @Override
        public void onScale(float scaleFactor, float focusX, float focusY) {
            if (!actionL) {
                actionL = true;
                if (DEBUG)
                    Dbg.d("SCALE", "factor: " + scaleFactor + " x: " + focusX + " y: " + focusY);
                scalingL = true;
                photoViewAttacherR.onScale(scaleFactor, focusX, focusY);
                super.onScale(scaleFactor, focusX, focusY);
                actionL = false;
            }
        }
    };

    photoViewAttacherR = new PhotoViewAttacher(photoR){
        @Override
        public void onDrag(float dx, float dy) {
            if (!scalingR && !actionR) {
                actionR = true;
                if (DEBUG) Dbg.d("DRAG", "x: " + dx + " y: " + dy);
                photoViewAttacherL.onDrag(dx, dy);
                super.onDrag(dx, dy);
                actionR = false;
            }
        }

        @Override
        public void onFling(float startX, float startY, float velocityX, float velocityY) {
        }

        @Override
        public boolean onTouch(View v, MotionEvent ev) {
            if (ev.getAction() == MotionEvent.ACTION_UP){
                scalingL = false;
                scalingR = false;
            }
            return super.onTouch(v, ev);
        }

        @Override
        public void onScale(float scaleFactor, float focusX, float focusY) {
            if (!actionR) {
                actionR = true;
                if (DEBUG)
                    Dbg.d("SCALE", "factor: " + scaleFactor + " x: " + focusX + " y: " + focusY);
                scalingR = true;
                photoViewAttacherL.onScale(scaleFactor, focusX, focusY);
                super.onScale(scaleFactor, focusX, focusY);
                actionR = false;
            }
        }
    };

//[...]
}

答案 2 :(得分:0)

如果您使用的是Imageview,我建议您查看Matrix.mapreact并使用图像的perticuler矩阵

您也可以使用GestureImageView

public class SampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        GestureImageView view = new GestureImageView(this);
        view.setImageResource(R.drawable.image);
        view.setLayoutParams(params);

        ViewGroup layout = (ViewGroup) findViewById(R.id.layout);

        layout.addView(view);
    }
}