安卓如何垂直翻转ImageView

41

我试图在垂直方向上翻转 ImageView,但它根本无法工作。

Java:

public static void flipImageVertically(final Bitmap bmp, final ImageView imageView) {
    final Matrix matrix = new Matrix();

    matrix.preScale(1.0f, -1.0f);

    imageView.setImageBitmap(Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true));
}

XML:

<LinearLayout                
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/red" />

</LinearLayout>
ImageView 没有翻转。有人知道原因吗?
8个回答

120

查看这个答案。你可以使用xml参数轻松地执行翻转。

android:scaleY="-1"

请注意,此方法无法在预览中运行,只有在应用程序运行时才有效。
自Android Studio 2以来,此方法也可以在预览中使用。

或者您可以在代码中调用setScaleY(-1f)来改变ImageView的比例。


那对我不起作用。我已经尝试过了,它仍然无法翻转。 - Subby
1
它在预览中不起作用,你尝试运行了吗? - Lamorak

10

在您的小部件上使用rotationY属性:

  android:rotationY="180"

例如:
<androidx.appcompat.widget.AppCompatImageView
   android:layout_width="@dimen/button_height_small"
   android:layout_height="@dimen/button_height_small"
   android:layout_gravity="center"
   android:padding="@dimen/space_medium"
   android:rotationY="180"/>

7

我使用了

 imageView.setScaleY(-1);

要翻转一个imageView(相对于未缩放的宽度绕中心点进行缩放),请参考文档


android:scaleX="-1" 可以使用。 - Shakir

4
我使用了。
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="YOUR_DRAWABLE_HERE"
    android:rotation="180"/>  // USE THIS TO ROTATE THE IMAGE

这会将图像旋转180度,具体效果可能看起来像翻转,这取决于你的图像。
希望对你有所帮助 :)

这里有一个好答案。 - FireZenk
3
旋转不等于翻转,澄清了答案。 - Manuel

2

我尝试使用旋转动画,但最好的方法是

    image.setRotationX(180); for vertical 
    image.setRotationY(180); for horizontal 

唯一的问题就是重置它。
编辑: 翻转是像镜子视图旋转。我的答案是关于旋转而不是翻转旋转。澄清了一下。

旋转并不意味着翻转。 - Devansh Maurya
1
哦,哈恩...翻转就像镜像视图旋转一样。 - Shadow

1
从资源中获取可绘制对象
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.index);

然后

public static Bitmap flip(Bitmap src, Direction type) {
    Matrix matrix = new Matrix();

    if(type == Direction.VERTICAL) {
        matrix.preScale(1.0f, -1.0f);
    }
    else if(type == Direction.HORIZONTAL) {
        matrix.preScale(-1.0f, 1.0f);
    } else {
        return src;
    }

    return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
}

设置ImageView.setImageBitmap()

1
如果传递给flipImageVertically方法的位图是反向的,并且每次都传递相同的位图,则可能会发生这种情况。发布更多细节可以帮助缩小范围,包括xml和代码。

我已添加了XML代码。我只传递原始位图。事先我没有做任何修改。 - Subby
你传递的位图是来自资源文件吗?请尝试以下代码:private static boolean flipped;public static void flipImageVertically(Bitmap bmp, final ImageView imageView) { final Matrix matrix = new Matrix(); if (flipped) { matrix.preScale(1.0f, -1.0f); } else { matrix.preScale(-1.0f, 1.0f); } flipped = !flipped;imageView.setImageBitmap(Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true));} - Catalina
它将交替地将图像上下翻转。 - Catalina
是的,它来自资源。我已经尝试过那段代码,但它仍然无法正常工作。 - Subby
如果它来自资源,翻转后的图像将始终相同。例如,如果你有一个指向上方的箭头图像,在从资源加载时翻转后,它将成为一个指向下方的箭头图像。不要从资源加载它,而是使用Bitmap bit = ((BitmapDrawable)imageView.getDrawable()).getBitmap()来获取当前在图像视图中的图像,并在flipImage方法中使用它。你还应该在xml中使用android:src="@drawable/your_image"作为imageView最初没有图像。 - Catalina

1
只是想通知您,我已经开发了一个新的库FlipView,其中包括并扩展了这个特定的动画(翻转)。我的意思是一个完全可定制的库,您可以交换任何类型的视图和布局以及任何您想要的动画和形状,包括Gmail图像翻转。
对于您的具体情况,我提供的库示例也有垂直翻转。
请看一下。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接