40

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?

Subby
  • 5,370
  • 15
  • 70
  • 125

8 Answers8

119

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.

Community
  • 1
  • 1
Lamorak
  • 10,957
  • 9
  • 43
  • 57
10

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"/>
7

I used

 imageView.setScaleY(-1);

to flip an imageView (scaling of around the pivot point reltive to the unscaled width, compare documentation

Frederic Klein
  • 2,846
  • 3
  • 21
  • 37
Pegah Ahmadvand
  • 97
  • 2
  • 10
4

I used

<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

This rotates the image by 180° which may look like a flip, depending on your image.

Hope this helps :)

Manuel
  • 14,274
  • 6
  • 57
  • 130
2

I tried using rotate animation but the best way is

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

the only problem is with resetting it.

edit: flip is like mirror view rotation. my ans is for rotation and not flip rotate.clarified it.

Shadow
  • 37
  • 9
1

This could happen if the Bitmap you are passing to the flipImageVertically method is the reverse and you are always passing the same bitmap every time. Posting more details could help narrowing down, xml and code.

Catalina
  • 1,954
  • 1
  • 17
  • 25
  • I have added the XML code. I am only passing in the original Bitmap. I'm not altering it at all before hand. – Subby Mar 15 '15 at 15:03
  • The bitmap that you are passing; is it coming from a resource or? try this 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 Mar 15 '15 at 15:08
  • It will alternatively flip the image upside down and upside up alternatively. – Catalina Mar 15 '15 at 15:09
  • Yes it is coming from a resource. I have already tried that code but it still won't work. – Subby Mar 15 '15 at 15:41
  • If it's coming from a resource the flipped image will always be the same. For example if you have an image of an arrow pointing up when flipped it will be an image of an arrow pointing down if loaded from resource every time. Instead of loading it from resource use Bitmap bit = ((BitmapDrawable)imageView.getDrawable()).getBitmap() to get the image that is currently in the image view and use that in the flipImage method. You should also use android:src="@drawable/your_image" in xml as the imageView will not have an image initially – Catalina Mar 15 '15 at 15:47
1

Just to notify that I've developed a new library FlipView that includes and extends this specific animation (flip). I mean a fully customizable library where you will be able to swap any kind of views and layouts with any kind of animation and shapes you desire, included the Gmail image flipping.

For your specific case, the example I provided with the library has a vertical flip as well.

Please have a look.

Davideas
  • 3,226
  • 2
  • 33
  • 51
0

get the drawable from the resources

Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.index);

and then

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);
}

Set ImageView.setImageBitmap()

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300