We can use the matrix below, with the Matrix.postConcat() to generate a mirror image about Y axis.
{ -1, 0, 0,
0, 1, 0,
0, 0, 1
};
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<CheckBox
android:id="@+id/enablemirror"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Mirror Image"
/>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
</LinearLayout>
AndroidMirrorImage.java
package com.exercise.AndroidMirrorImage;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
public class AndroidMirrorImage extends Activity {
private final String imageInSD = "/sdcard/google.png";
CheckBox enableMirror;
ImageView myImageView;
Bitmap bitmap;
int bmpWidth, bmpHeight;
Matrix matrixMirrorY;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
enableMirror = (CheckBox)findViewById(R.id.enablemirror);
myImageView = (ImageView)findViewById(R.id.imageview);
enableMirror.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
drawMatrix();
}});
bitmap = BitmapFactory.decodeFile(imageInSD);
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
initMirrorMatrix();
drawMatrix();
}
private void initMirrorMatrix()
{
float[] mirrorY =
{ -1, 0, 0,
0, 1, 0,
0, 0, 1
};
matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
}
private void drawMatrix()
{
if(enableMirror.isChecked())
{
Matrix matrix = new Matrix();
matrix.postConcat(matrixMirrorY);
Bitmap mirrorBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
myImageView.setImageBitmap(mirrorBitmap);
}
else{
myImageView.setImageBitmap(bitmap);
}
}
}
Download the files.
previous: Skew bitmap image, using Matrix
2 comments:
Thanks! Really needed that function!
this code display null and runtimeexception()
could u please tell why its show exception
Post a Comment