Subby
Subby

Reputation: 5480

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?

Upvotes: 42

Views: 45332

Answers (8)

MohammadReza Eram
MohammadReza Eram

Reputation: 568

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

Upvotes: 10

Shadow
Shadow

Reputation: 33

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.

Upvotes: 2

Pegah Ahmadvand
Pegah Ahmadvand

Reputation: 107

I used

 imageView.setScaleY(-1);

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

Upvotes: 7

Ali Goher Shabir
Ali Goher Shabir

Reputation: 395

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

Upvotes: 4

Lamorak
Lamorak

Reputation: 11137

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.

Upvotes: 122

Davideas
Davideas

Reputation: 3276

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.

Upvotes: 1

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

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

Upvotes: 1

Catalina
Catalina

Reputation: 2098

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.

Upvotes: 1

Related Questions