Thursday, July 22, 2010

More on Generate a mirror image using Matrix.postConcat()

More function of mirror image, such as mirror about X axis, mirror about center, will be as in this exercise; base on the last exercise "Generate a mirror image using Matrix.postConcat()".

More on Generate a mirror image using Matrix.postConcat()

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"
/>
<Spinner
android:id="@+id/mirrorselection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<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.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

public class AndroidMirrorImage extends Activity {

private final String imageInSD = "/sdcard/google.png";

private static final String[] optionMirror =
{"Normal", "Mirror about X", "Mirror about Y", "Mirror about Center"};
private ArrayAdapter<String> adapter;

Spinner mirrorSelection;
ImageView myImageView;

Bitmap bitmap;
int bmpWidth, bmpHeight;

Matrix matrixMirrorNormal, matrixMirrorX, matrixMirrorY, matrixMirrorC;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myImageView = (ImageView)findViewById(R.id.imageview);
mirrorSelection = (Spinner)findViewById(R.id.mirrorselection);

adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, optionMirror);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mirrorSelection.setAdapter(adapter);

mirrorSelection.setSelection(0);

mirrorSelection.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
drawMatrix();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
mirrorSelection.setSelection(0);
}});

bitmap = BitmapFactory.decodeFile(imageInSD);
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();

initMirrorMatrix();

drawMatrix();
}

private void initMirrorMatrix()
{
float[] mirrorNormal =
{ 1, 0, 0,
0, 1, 0,
0, 0, 1
};

float[] mirrorX =
{ 1, 0, 0,
0, -1, 0,
0, 0, 1
};

float[] mirrorY =
{ -1, 0, 0,
0, 1, 0,
0, 0, 1
};

float[] mirrorC =
{ -1, 0, 0,
0, -1, 0,
0, 0, 1
};
matrixMirrorNormal = new Matrix();
matrixMirrorNormal.setValues(mirrorNormal);
matrixMirrorX = new Matrix();
matrixMirrorX.setValues(mirrorX);
matrixMirrorY = new Matrix();
matrixMirrorY.setValues(mirrorY);
matrixMirrorC = new Matrix();
matrixMirrorC.setValues(mirrorC);
}

private void drawMatrix()
{
Matrix matrix = new Matrix();
switch (mirrorSelection.getSelectedItemPosition()){
case 0: //Normal
matrix.postConcat(matrixMirrorNormal);
break;
case 1: //Mirror about X
matrix.postConcat(matrixMirrorX);
break;
case 2: //Mirror about Y
matrix.postConcat(matrixMirrorY);
break;
case 3: //Mirror about Center
matrix.postConcat(matrixMirrorC);
break;
}
Bitmap mirrorBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
myImageView.setImageBitmap(mirrorBitmap);
}
}


Download the files.

No comments: