Matrix setScale Function未按预期工作

时间:2016-06-09 08:42:13

标签: android image-scaling

我有用于调整图像大小的代码: Java:

 public class MainActivity extends Activity {

    private ImageView iv, iv2;
    private float scaley = 1f;
    private float scaley2 = 1f;
    private GestureDetectorCompat detector;
    Matrix m = new Matrix();
    Matrix m2 = new Matrix();
    private RelativeLayout root;

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


        root = (RelativeLayout) findViewById(R.id.root);
        iv = (ImageView) findViewById(R.id.imageView);
        iv2 = (ImageView) findViewById(R.id.imageView2);

        iv.setScaleType(ImageView.ScaleType.MATRIX);
        iv2.setScaleType(ImageView.ScaleType.MATRIX);
        detector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {


                scaley -= distanceY / root.getHeight();
                m.setScale(1, scaley, 0, 0);
                iv.setImageMatrix(m);


                scaley2 += distanceY / root.getHeight();
                m2.setScale(1, scaley2);
                int imageHeight = iv2.getDrawable().getMinimumHeight();
                m2.postTranslate(0, iv2.getHeight() - (imageHeight * scaley2));
                iv2.setImageMatrix(m2);

              return true;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/root">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_alignParentTop="true"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"

            android:src="@drawable/as" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView"
            android:id="@+id/imageView2"
            android:src="@drawable/as"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            />
</RelativeLayout>

我希望我的second图片底部有一个轴,所以我使用这部分:

scaley2 += distanceY / root.getHeight();
                m2.setScale(1, scaley2);
                int imageHeight = iv2.getDrawable().getMinimumHeight();
                m2.postTranslate(0, iv2.getHeight() - (imageHeight * scaley2));
                iv2.setImageMatrix(m2); 

它起作用,图像看起来固定在底部,但它没有增长,当我尝试增长图像时,它保持默认大小。我怎样才能解决这个问题?感谢。

1 个答案:

答案 0 :(得分:1)

编辑:

我发现最干净的方法是让两个ImageView重叠,然后根据触摸事件计算图像应占据的矩形,然后设置矩阵以将图像边界映射到矩形边界,例如:

        // after setting image resource, get the bounds of the bitmap
        Drawable drawable = mImageView1.getDrawable();
        image1Bounds.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable = mImageView2.getDrawable();
        image2Bounds.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        // use the Y coord of touch event as bottom of imageView1 and top of imageView2
        imageView1Bounds.set(0, 0, mImageView1.getMeasuredWidth(), event.getY());
        imageView2Bounds.set(0, event.getY(), mImageView2.getMeasuredWidth(), mImageView2.getMeasuredHeight());

        // set the matrices to transform the images to their respective bounds
        mMatrix1.setRectToRect(image1Bounds, imageView1Bounds, Matrix.ScaleToFit.FILL);
        mMatrix2.setRectToRect(image2Bounds, imageView2Bounds, Matrix.ScaleToFit.FILL);

        mImageView1.setImageMatrix(mMatrix1);
        mImageView2.setImageMatrix(mMatrix2);

您可以在https://github.com/klarson2/Matrix

查看整个工作项目

如果您想将图片的底部锚定在ImageView的底部,最好的办法是添加一个可以执行此操作的翻译。

            Matrix m = new Matrix();
            scaley += distanceY / root.getHeight();
            m.setScale(1, scaley);  // no pivot here
            int imageHeight = iv.getDrawable().getIntrinsicHeight();
            m.postTranslate(0, iv.getHeight() - (imageHeight * scaley));
            iv.setImageMatrix(m);
            return true;

另外,我建议在初始化期间分配一次新的Matrix,然后在onScroll()中重复使用该矩阵来提高性能。