Flip ImageView example in android

In my previous tutorial  move-zoom-rotate-imageview i am explaining how to  move ,zoom , and rotate an ImageView .Here we will learn how to flip an ImageView in android. We just need to use preScale function in Matrix. You just need to apply the following code to your imageview.

private Bitmap flipImage(Bitmap image_bitmap) {

// create new matrix for transformation
Matrix matrix = new Matrix();
matrix.preScale(-1.0f, 1.0f);

Bitmap flipped_bitmap = Bitmap.createBitmap(image_bitmap, 0, 0, image_bitmap.getWidth(), image_bitmap.getHeight(), matrix, true);

return flipped_bitmap;

}

FullCode

In this example there will be one button and an imageview , imageview will be flipped whenever the button is clicked.

Let us start by adding imageview and button to layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.codesfor.flip.MainActivity"
tools:showIn="@layout/activity_main">

<ImageView
android:id="@+id/im_flip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/flip_background"/>

<Button
android:id="@+id/btn_flip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:textColor="#FFFFFF"
android:layout_alignParentBottom="true"
android:text="flip"/>

</RelativeLayout>

Here is the code for your Activity class file.

public class MainActivity extends AppCompatActivity {

Toolbar toolbar;
ImageView im_flip;
Button btn_flip;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

init();

btn_flip.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

im_flip.setImageBitmap(flipImage(((BitmapDrawable) im_flip.getDrawable()).getBitmap()));

}
});

}


//method to do flip action

private Bitmap flipImage(Bitmap image_bitmap) {

// create new matrix for transformation
Matrix matrix = new Matrix();
matrix.preScale(-1.0f, 1.0f);

Bitmap flipped_bitmap = Bitmap.createBitmap(image_bitmap, 0, 0, image_bitmap.getWidth(), image_bitmap.getHeight(), matrix, true);

return flipped_bitmap;

}

//method for initialisation

private void init() {

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Flip");
im_flip = (ImageView) findViewById(R.id.im_flip);
btn_flip = (Button) findViewById(R.id.btn_flip);

}

}

output

flip-imageview
flip-imageview

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

Your email address will not be published. Required fields are marked *