【问题标题】:Drag,Zoom or Rotate an ImageView拖动、缩放或旋转 ImageView
【发布时间】:2014-06-27 10:55:40
【问题描述】:

Here 是一个执行 ImageView 的拖动、缩放和旋转的类。

xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

</FrameLayout>

但这一切都是在 ImageView 的 Matrix 上使用 imageView.setImageMatrix(matrix);

完成的

不是实际的图像视图。

因此得到以下结果:

初始图像视图:

向右拖动:

关于放大:

旋转时:

有没有办法可以修改 ImageView 以根据它的 Matrix 进行更改?

【问题讨论】:

    标签: android image matrix


    【解决方案1】:

    您可以致电 View.setRotation()、View.setPivotX()、View.setPivotY()、View.setScaleX()、View.setScaleY()、View.setTranslationX() 和 View.setTranslationY() 以及任何视图,包括 ImageView。

    参考:http://developer.android.com/reference/android/view/View.html

    【讨论】:

      【解决方案2】:

      你真的要翻译 imageView 吗?如果不是,您可以使用 setRectToRect (RectF src, RectF dst, Matrix.ScaleToFit stf) 将翻译后的图像显示在原始视图的中心。

      Matrix m = imageView.getImageMatrix();
      RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
      RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight());
      m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
      imageView.setImageMatrix(m);
      

      【讨论】:

        【解决方案3】:

        对于拖动,计算移动的距离并将其添加到左边距和上边距

        喜欢这个

        x = event.getRawX();
        y = event.getRawY();
        
        parms.leftMargin = (int) ((x - dx) + scalediff);
        parms.topMargin = (int) ((y - dy) + scalediff);
        

        对于缩放,您可以使用 ScaleX 和 ScaleY 方法

        view.setScaleX(scale);
        view.setScaleY(scale);
        

        旋转

        view.animate().rotationBy(angle).setDuration(0).setInterpolator(new LinearInterpolator()).start();
        

        这里是完整代码Move zoom and rotate imageview in android

        【讨论】:

        • 此代码在与不同的父布局(如 ConstraintLayout)一起使用时出现意外行为,将图像旋转 180 度并开始拖动后,视图快速开始移动并从屏幕上消失。修复工作将很快发布。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 2012-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多