安卓翻转图像垂直视图



我正在尝试垂直翻转和ImageView,但它就是不起作用。

爪哇岛:

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根本没有翻转。

有人知道为什么吗?

检查这个答案。您可以使用 xml 参数非常轻松地执行翻转

android:scaleY="-1"

请注意,这在预览版中不起作用,仅在运行应用时。
从Android Studio 2开始,这也适用于预览版。

或者,可以在代码中调用ImageView上的setScaleY(-1f)

在小部件上使用 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"/>

我用过

 imageView.setScaleY(-1);

翻转图像视图(相对于未缩放宽度的枢轴点进行缩放,比较文档

我用过

<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°,这可能看起来像翻转,具体取决于您的图像。

希望这对:)有所帮助

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

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

唯一的问题是重置它。

编辑:翻转就像镜像视图旋转。我的 ANS 用于旋转而不是翻转旋转。

如果您传递给 flipImageVertically 方法的位图正好相反,并且您每次都始终传递相同的位图,则可能会发生这种情况。发布更多详细信息可能有助于缩小范围,xml和代码。

只是为了通知我已经开发了一个新的库 FlipView,其中包含并扩展了这个特定的动画(翻转)。我的意思是一个完全可定制的库,您可以在其中将任何类型的视图和布局与您想要的任何类型的动画和形状交换,包括 Gmail 图像翻转。

对于您的具体情况,我随库提供的示例也具有垂直翻转。

请看一看。

资源中获取可绘制对象

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()

最新更新