unknown os platform
Android code: how to flip image horizontally in Android example | Whats-online.info
whats-online-info

Android code: how to flip image horizontally in Android example


Android
2017/02/24 / Science and Tutorials

Learn how to flip bitmap image horizontally in android programmatically. See an example.





flip bitmap image horizontally in Android example


For this Android tutorial, we will talk about how to flip a bitmap image horizontally programmatically. 



Download source code at the end of this tutorial.

First and foremost, we need to define our project file composition.
We will use the default MainActivity.java to demonstrate this post.

MainActivity.java class

This class defines our imageView widget through the Java code. Get our bitmap image from drawable and pass it to the function for flipping our bitmap image. On activity create, we will call our FlipHorizontal() function and set the returned bitmap data type to the ImageView. 

Besides, we will define a LinearLayout, set content view to the linearlayout and add the ImageView to our linearlayout programmatically.

MainActivity.java Code


package info.whats_online.fliphorizontally; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.os.Bundle; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends Activity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         Bitmap originalImage = BitmapFactory.decodeResource(getResources(),                 R.drawable.bf);         // Create an Image view and add our bitmap with reflection to it         ImageView imageView = new ImageView(this);         imageView.setImageBitmap(FlipHorizontally(originalImage));         // Add the image to a linear layout and display it         LinearLayout linLayout = new LinearLayout(this);         linLayout.addView(imageView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));         linLayout.setBackgroundColor(Color.BLACK);         // set LinearLayout as ContentView         setContentView(linLayout);     }     public Bitmap FlipHorizontally(Bitmap originalImage) {         // The gap we want between the flipped image and the original image         final int flipGap = 4;         int width = originalImage.getWidth();         int height = originalImage.getHeight();         // This will not scale but will flip on the Y axis         Matrix matrix = new Matrix();         matrix.preScale(-1, 1);         // Create a Bitmap with the flip matrix applied to it.         // We only want the bottom half of the image         Bitmap flipImage = Bitmap.createBitmap(originalImage, 0,0 , width, height, matrix, true);         // Create a new bitmap with same width but taller to fit reflection         Bitmap bitmapWithFlip = Bitmap.createBitmap((width + width + flipGap), height, Bitmap.Config.ARGB_8888);         // Create a new Canvas with the bitmap that's big enough for         Canvas canvas = new Canvas(bitmapWithFlip);         //Draw original image         canvas.drawBitmap(originalImage, 0, 0, null);        //Draw the Flipped Image         canvas.drawBitmap(flipImage, width+flipGap, 0, null);         return bitmapWithFlip;     } }

Function code 


public Bitmap FlipHorizontally(Bitmap originalImage) {         // The gap we want between the flipped image and the original image         final int flipGap = 4;         int width = originalImage.getWidth();         int height = originalImage.getHeight();         // This will not scale but will flip on the Y axis         Matrix matrix = new Matrix();         matrix.preScale(-1, 1);         // Create a Bitmap with the flip matrix applied to it.         // We only want the bottom half of the image         Bitmap flipImage = Bitmap.createBitmap(originalImage, 0,0 , width, height, matrix, true);         // Create a new bitmap with same width but taller to fit reflection         Bitmap bitmapWithFlip = Bitmap.createBitmap((width + width + flipGap), height, Bitmap.Config.ARGB_8888);         // Create a new Canvas with the bitmap that's big enough for         Canvas canvas = new Canvas(bitmapWithFlip);         //Draw original image         canvas.drawBitmap(originalImage, 0, 0, null);        //Draw the Flipped Image         canvas.drawBitmap(flipImage, width+flipGap, 0, null);         return bitmapWithFlip;     } }


explanation.
First, we need to get the dimensions of our bitmap image and draw our canvas using those dimensions. However, we will multiply the width of our canvas by two to draw the two bitmap image on the canvas.

We will draw our original image from (0, 0) on the canvas

This code draws our flipped bitmap image. 
Then draw our flipped image from (0+original.width, 0).

Finally, return a bitmap image.

Thanks for your time, subscribe to our newsletter or like us on Facebook for more related Android tutorial posts













You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment