Please Enable JavaScript!
Gon[ Enable JavaScript ]

반응형

안드로이드(Android) 행열(Matrix) 클래스의 postConcat() 함수이용해 거울이미지구현

 

환경 : Eclipse Mars, Android 4.2.2

 

이번예제는 Matrix 객체를 이용해서 이미지의 뒤집어서 볼수 있는 기능입니다. 거울이미지를 구현하는 것인데 X , Y 축을 기준으로 뒤집어서 보여줍니다. 이 기능을 구현하기 위해 postConcat() 함수를 사용합니다.

 

 

상을 뒤집기 위해 Matrix postConcat() 함수는 인수로 행열값이 포함된 데이터를 받습니다.

 

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

 

축을 기준으로 이미지를 뒤집기위한 셋팅을

한후 postConcat() 함수의 인수로

객체를 넘기게 되면 행렬값에 맞게

이미지의 상을 뒤집을수 있습니다.

 

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);
	imageView.setImageBitmap(mirrorBitmap);
}

 

예제에서는 Spinner 를 사용하여 축별로 구분하여 이벤트가 발생하면 drawMatrix() 함수를 수행하게 하였습니다.

 

mirrorSelection.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
	public void onItemSelected(AdapterView<?> arg0, 
View arg1, int arg2, long arg3) {

		drawMatrix();
	}
	public void onNothingSelected(AdapterView<?> arg0) {
		mirrorSelection.setSelection(0);
	}
});

 

다음은 메인 activity 의 레이아웃 xml 에 대한 내용입니다.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#3F0099"
        android:paddingBottom="@dimen/abc_action_bar_icon_vertical_padding"
        android:paddingTop="@dimen/abc_action_bar_icon_vertical_padding"
        android:text="거울 이미지 구현 "
        android:textColor="#FFFFFF" />

    <Spinner
        android:id="@+id/mirrorselection"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:scaleType="center" />

</LinearLayout>

 

아래는 메인 activity 에 대한 전체 소스 입니다.


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

public class ImageMirrorMatrixActivity extends Activity {

	private String imageSdcard;

	private static final String[] optionMirror = { 
		"정상화면", "X축기준", "Y축기준", "Center" };
	private ArrayAdapter<String> adapter;

	Spinner mirrorSelection;
	ImageView imageView;

	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.activity_image_mirror_matrix);
		imageSdcard = Environment.getExternalStorageDirectory().toString() + 
				"/Download/view04.png";
		imageView = (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() {
			public void onItemSelected(AdapterView<?> arg0,
					View arg1, int arg2, long arg3) {
				drawMatrix();
			}
			public void onNothingSelected(AdapterView<?> arg0) {
				mirrorSelection.setSelection(0);
			}
		});

		bitmap = BitmapFactory.decodeFile(imageSdcard);
		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);
		imageView.setImageBitmap(mirrorBitmap);
	}
}

 

 

안드로이드(Android) 행열(Matrix) 클래스의 postConcat() 함수이용해 거울이미지구현

 

반응형
Posted by 녹두장군1
,