Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Flip ImageView Vertically

I am trying to flip and ImageView vertically but it just won't work.

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>

The ImageView isn't flipping at all.

Anyone know why?

like image 794
Subby Avatar asked Mar 15 '15 14:03

Subby


2 Answers

Check this answer. You can perform flip very easily using an xml parameter

android:scaleY="-1"

Note that this does not work in preview, only when you run the app.
Since Android Studio 2, this works in preview as well.

Alternatively you can call setScaleY(-1f) on your ImageView in code.

like image 180
Lamorak Avatar answered Oct 24 '22 07:10

Lamorak


Use rotationY attribute on your widget,

  android:rotationY="180"

For example:

<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"/>
like image 8
MohammadReza Eram Avatar answered Oct 24 '22 07:10

MohammadReza Eram