티스토리 뷰

반응형

[Android] Bitmap 회전, 좌우 / 상하 반전 적용하기 ( inverse, rotate )


[Android] Bitmap 크기 조절 / 겹치기 / 잘라내기 바로가기



   구현 예제




   Matrix를 이용한 Bitmap 변형


Bitmap을 생성할때 matrix에 속성을 넣어 생성하면 Matrix에 따른 옵션을 변경 할 수 있다.

회전, 반전, 이동, 크기변경 등 여러가지 속성을 가지고 있다.




   Bitmap 상하 / 좌우 반전하기

Matrix sideInversion = new Matrix();
sideInversion.setScale(1, -1); // 상하반전

sideInversion.setScale(-1, 1); // 좌우반전
Bitmap sideInversionImg = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), sideInversion, false);



   Activity 회전 Rotate

Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(회전할 각도); //-360~360


Bitmap sideInversionImg = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, false);



   Activity 전체 소스 ( BitmapRotateActivity.java )
package com.tistory.dwfox.dwfoxtest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

/**
* Created by Administrator on 2016-03-22.
*/
public class BitmapRotateActivity extends Activity implements View.OnClickListener{

private ImageView imageView;
private int invertIndex = 0;
private int rotate = 0;
Bitmap bitmap = null;

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

imageView = (ImageView)findViewById(R.id.bitmap);

bitmap = null;

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
imageView.setImageBitmap(bitmap);
findViewById(R.id.invertion_button).setOnClickListener(this);
findViewById(R.id.rotate_button).setOnClickListener(this);
}


public Bitmap InversionBitmap(Bitmap bitmap, int inverse) {

Matrix sideInversion = new Matrix();

if(inverse ==0 )
sideInversion.setScale(1, 1); // 원본
else if(inverse == 1)
sideInversion.setScale(-1, 1); // 좌우반전
else if(inverse == 2)
sideInversion.setScale(1, 1); // 원본
else
sideInversion.setScale(1, -1); // 상하반전

Bitmap sideInversionImg = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), sideInversion, false);

return sideInversionImg;
}

public Bitmap rotateBitmap(Bitmap bitmap, int rotate){

Log.d("TEST", "ROTATE : " + rotate);
Matrix rotateMatrix = new Matrix();

if(rotate == 0 )
rotateMatrix.postRotate(0);
else if(rotate == 1)
rotateMatrix.postRotate(45);
else if(rotate == 2)
rotateMatrix.postRotate(90);
else if(rotate == 3)
rotateMatrix.postRotate(135);
else if(rotate == 4)
rotateMatrix.postRotate(180);
else if(rotate == 5)
rotateMatrix.postRotate(225);
else if(rotate == 6)
rotateMatrix.postRotate(270);
else
rotateMatrix.postRotate(315);

Bitmap sideInversionImg = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), rotateMatrix, false);

return sideInversionImg;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.invertion_button:
invertIndex++;
if(invertIndex == 4) invertIndex =0;
imageView.setImageBitmap(InversionBitmap(bitmap, invertIndex % 4));
break;
case R.id.rotate_button:
rotate++;
if(rotate == 8) rotate =8;
imageView.setImageBitmap(rotateBitmap(bitmap, rotate % 8));
break;
}
}
}


   Layout 전체 소스 ( bitmap_rotate_activity.xml )


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:id="@+id/bitmap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitCenter" />

<Button
android:id="@+id/invertion_button"
android:layout_width="125dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="25dp"
android:text="Inversion"
android:layout_marginLeft="50dp"
android:background="@drawable/btn" />

<Button
android:id="@+id/rotate_button"
android:layout_width="125dp"
android:layout_height="50dp"
android:text="Rotate"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="25dp"
android:layout_marginRight="50dp"
android:background="@drawable/btn" />

</RelativeLayout>



   예제 이미지


반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함