拖动,缩放或旋转ImageView

时间:2014-06-27 10:55:41

标签: android image matrix

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.setImageMatrix(matrix);

完成了 ImageView 矩阵

不是实际的图片视图。

因此以下结果:

初始ImageView:

enter image description here

向右拖动:

enter image description here

放大:

enter image description here

关于旋转:

enter image description here

有没有办法可以修改 ImageView 按照矩阵进行更改?

3 个答案:

答案 0 :(得分:3)

您可以在任何视图上致电View.setRotation()View.setPivotX()View.setPivotY()View.setScaleX()View.setScaleY()View.setTranslationX()View.setTranslationY()包括ImageView

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

答案 1 :(得分:1)

你真的想要翻译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);

答案 2 :(得分:0)

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

像这样

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

相关问题