본문 바로가기

프로그래밍/Android

[Android] Matrix를 이용한 이미지 확대 축소

이미지를 처리할 때 mastrix를 사용하면 더욱 다양한 효과를 얻을 수 있다. 
주로 사용하는 명령등은 아래와 같다.
matrix.postScale(float x, float y) // 확대, 축소 
matrix.postRotate(float degrees) // 이미지 회전 
matrix.postSkew(float sx, float sy) // 이미지 비틀기 
matrix.postTranslate(float dx, float dy) // 이미지 이동 

그리고 적용하는 방법에는 현재의 ImageView에 바로 적용하는 방식
imageview.setImageMatrix(matrix);

와 새로운 파일을 만드는 방법
NewBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false); 
를 사용하여 imageview.setImageBitmap(NewBitmap); 등을 이용할 수 있는 방법이 있다.

또한 중요한 포인트 중의 하나는 
imageview.setScaleType(ScaleType.MATRIX); 를 사용하여 현재 scaletype를 변경해 두어야 합니다.
xml에서 변경코저 한다면 
android:scaleType="matrix" 처럼 처리해두어야 합니다.

<ImageView android:layout_height="wrap_content" 
android:id="@+id/imageViewForMatrix" 
android:src="@drawable/icon" 
android:layout_width="fill_parent"
android:scaleType="matrix"
>

아래는 각각에 대한 간단한 샘플을 나타내었습니다.

matrixSample.java

  1. package com.matrixSample;

  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Matrix;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.ImageView.ScaleType;

  13. public class matrixSample extends Activity {
  14.     Button resetButton, reversxButton, reversyButton, upscaleButton, downscaleButton, moveleftButton, moverightButton, skewButton;
  15.    
  16.     ImageView iv;
  17.     Bitmap bitmap;
  18.     /** Called when the activity is first created. */
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.        
  24.         iv = (ImageView) findViewById(R.id.imageViewForMatrix);
  25.         iv.setScaleType(ScaleType.MATRIX);//혹은 현재 xml에서 android:scaleType="matrix" 처리
  26.        
  27.         bitmap = BitmapFactory.decodeResource(getResources(),  R.drawable.sample_image_jpg);
  28.         iv.setImageBitmap(bitmap);
  29.        // iv.setImageBitmap(setReverse());
  30.        
  31.         // y축을 기준으로 x축 바꾸기
  32.         resetButton = (Button) findViewById(R.id.reset);
  33.         resetButton.setOnClickListener(new OnClickListener() {
  34.             public void onClick(View v) {
  35.                 bitmap = BitmapFactory.decodeResource(getResources(),  R.drawable.sample_image_jpg);
  36.                 iv.setImageBitmap(bitmap);
  37.             }
  38.         });
  39.        
  40.        
  41.        // y축을 기준으로 x축 바꾸기
  42.        reversxButton = (Button) findViewById(R.id.reversx);
  43.        reversxButton.setOnClickListener(new OnClickListener() {
  44.             public void onClick(View v) {
  45.                 //setReverse();
  46.                 iv.setImageBitmap(setReverse());
  47.             }
  48.        });
  49.        
  50.        // x축을 기준으로 y축 바꾸기
  51.        reversyButton = (Button) findViewById(R.id.reversy);
  52.        reversyButton.setOnClickListener(new OnClickListener() {
  53.             public void onClick(View v) {
  54.                 iv.setImageBitmap(setReverseY());
  55.             }
  56.        });  
  57.        
  58.      
  59.        upscaleButton = (Button) findViewById(R.id.upscale);
  60.        upscaleButton.setOnClickListener(new OnClickListener() {
  61.             public void onClick(View v) {
  62.                 iv.setImageBitmap(setScale(1));
  63.             }
  64.        });  
  65.        
  66.        downscaleButton = (Button) findViewById(R.id.downscale);
  67.        downscaleButton.setOnClickListener(new OnClickListener() {
  68.             public void onClick(View v) {
  69.                 iv.setImageBitmap(setScale(2));
  70.             }
  71.        });  
  72.        
  73.        moveleftButton = (Button) findViewById(R.id.moveleft);
  74.        moveleftButton.setOnClickListener(new OnClickListener() {
  75.             public void onClick(View v) {
  76.                 //iv.setImageBitmap(setMove(1));
  77.                 setMove(1);
  78.             }
  79.        });  
  80.        
  81.        moverightButton = (Button) findViewById(R.id.moveright);
  82.        moverightButton.setOnClickListener(new OnClickListener() {
  83.             public void onClick(View v) {
  84.                 //iv.setImageBitmap(setMove(2));
  85.                 setMove(2);
  86.             }
  87.        });  
  88.        
  89.        
  90.        skewButton = (Button) findViewById(R.id.skew);
  91.        skewButton.setOnClickListener(new OnClickListener() {
  92.             public void onClick(View v) {
  93.                 iv.setImageBitmap(setSkew());
  94.                 //setSkew();
  95.             }
  96.        });        

  97.        
  98.        
  99.     }
  100.    
  101.     private Bitmap setReverse(){
  102.     //private void setReverse(){
  103.         Bitmap rtnBitmap;
  104.         Matrix matrix = new Matrix();
  105.         matrix.postScale(-1, 1); //y 축을 대칭으로 x를 반대로 한다.
  106.        
  107.         //iv.setImageMatrix(matrix);
  108.         //기존 이미지의 크기(height & width)를 구한다.
  109.         int w   = bitmap.getWidth();
  110.         int h   = bitmap.getHeight();
  111.        
  112.         //앞에서 제작된 y축 대칭 매트릭스를 이미지에 적용한다.
  113.         bitmap  = rtnBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
  114.        
  115.         return rtnBitmap;
  116.     }  
  117.    
  118.     private Bitmap setReverseY(){
  119.         Bitmap rtnBitmap;
  120.         Matrix matrix = new Matrix();
  121.         matrix.postScale(1, -1); //x 축을 대칭으로 y를 반대로 한다.
  122.        
  123.         //기존 이미지의 크기(height & width)를 구한다.
  124.         //bitmap    = BitmapFactory.decodeResource(getResources(), R.drawable.sample_image_jpg);
  125.         int w   = bitmap.getWidth();
  126.         int h   = bitmap.getHeight();
  127.        
  128.         //앞에서 제작된 y축 대칭 매트릭스를 이미지에 적용한다.
  129.         bitmap  = rtnBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
  130.         return rtnBitmap;
  131.     }
  132.    
  133.     private Bitmap setScale(int flag){
  134.         Bitmap rtnBitmap;
  135.         Matrix matrix = new Matrix();
  136.         switch(flag){
  137.         case 1://확대
  138.             matrix.postScale(1.1f, 1.1f); //
  139.             break;
  140.         case 2://축소
  141.             matrix.postScale(0.9f, 0.9f); //
  142.             break;
  143.         }
  144.        
  145.         //기존 이미지의 크기(height & width)를 구한다.
  146.         int w   = bitmap.getWidth();
  147.         int h   = bitmap.getHeight();
  148.        
  149.         //앞에서 제작된 y축 대칭 매트릭스를 이미지에 적용한다.
  150.         bitmap  = rtnBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
  151.         return rtnBitmap;
  152.     }  
  153.    
  154.     //private Bitmap setMove(int flag){
  155.     private void setMove(int flag){
  156.         //Bitmap rtnBitmap;
  157.         Matrix matrix = new Matrix();
  158.         switch(flag){
  159.         case 1://left
  160.             Log.i("setMove", "left");
  161.             matrix.postTranslate(-100, 0); //
  162.             break;
  163.         case 2://right
  164.             Log.i("setMove", "right");
  165.             matrix.postTranslate(100, 0); //
  166.             break;
  167.         }
  168.        
  169.         //기존 이미지의 크기(height & width)를 구한다.
  170.         //int w = bitmap.getWidth();
  171.         //int h = bitmap.getHeight();
  172.        
  173.         //앞에서 제작된 y축 대칭 매트릭스를 이미지에 적용한다.
  174.         iv.setImageMatrix(matrix);//이미지에 매트릭스 적용
  175.         //bitmap    = rtnBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
  176.         //return rtnBitmap;
  177.     }
  178.    
  179.    
  180.     //private void setSkew(){
  181.     private Bitmap setSkew(){
  182.         Bitmap rtnBitmap;
  183.         Matrix matrix = new Matrix();
  184.         matrix.postSkew(0.88f, -0.16f); //
  185.        
  186.         //기존 이미지의 크기(height & width)를 구한다.
  187.         int w   = bitmap.getWidth();
  188.         int h   = bitmap.getHeight();
  189.        
  190.         //iv.setImageMatrix(matrix);//이미지에 매트릭스 적용
  191.         bitmap  = rtnBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
  192.         return rtnBitmap;
  193.     }
  194.    
  195. }

res/layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">
  6.    
  7.     <ImageView android:layout_height="wrap_content"
  8.         android:id="@+id/imageViewForMatrix"
  9.         android:src="@drawable/icon"
  10.         android:layout_width="fill_parent"
  11.         >
  12.     </ImageView>
  13.     <LinearLayout
  14.         android:layout_height="wrap_content"
  15.         android:layout_width="match_parent"
  16.         android:id="@+id/linearLayout1"
  17.         android:layout_alignParentBottom="true"
  18.         android:orientation="vertical">
  19.             <LinearLayout
  20.                 android:layout_height="wrap_content"
  21.                 android:layout_width="match_parent"
  22.                 android:id="@+id/linearLayout3">
  23.                 <Button android:text="enLarge"
  24.                     android:id="@+id/upscale"
  25.                     android:layout_width="wrap_content"
  26.                     android:layout_height="wrap_content">
  27.                 </Button>
  28.                 <Button
  29.                     android:text="downScale"
  30.                     android:id="@+id/downscale"
  31.                     android:layout_width="wrap_content"
  32.                     android:layout_height="wrap_content">
  33.                 </Button>
  34.                 <Button
  35.                     android:text="Move Left"
  36.                     android:id="@+id/moveleft"
  37.                     android:layout_width="wrap_content"
  38.                     android:layout_height="wrap_content">
  39.                 </Button>
  40.                 <Button
  41.                     android:text="Move Right"
  42.                     android:id="@+id/moveright"
  43.                     android:layout_width="wrap_content"
  44.                     android:layout_height="wrap_content">
  45.                 </Button>
  46.             </LinearLayout>    
  47.             <LinearLayout
  48.                 android:layout_height="wrap_content"
  49.                 android:layout_width="match_parent"
  50.                 android:id="@+id/linearLayout2"
  51.                 >
  52.                 <Button android:text="reset"
  53.                     android:id="@+id/reset"
  54.                     android:layout_width="wrap_content"
  55.                     android:layout_height="wrap_content">
  56.                 </Button>
  57.                 <Button
  58.                     android:text="revers X"
  59.                     android:id="@+id/reversx"
  60.                     android:layout_width="wrap_content"
  61.                     android:layout_height="wrap_content"
  62.                     android:layout_toRightOf="@id/reset">
  63.                 </Button>
  64.                 <Button
  65.                     android:text="revers Y"
  66.                     android:id="@+id/reversy"
  67.                     android:layout_width="wrap_content"
  68.                     android:layout_height="wrap_content">
  69.                 </Button>
  70.                 <Button
  71.                     android:text="SKEW"
  72.                     android:id="@+id/skew"
  73.                     android:layout_width="wrap_content"
  74.                     android:layout_height="wrap_content">
  75.                 </Button>              
  76.             </LinearLayout>

  77.     </LinearLayout>
  78. </RelativeLayout>






[출처] http://www.shop-wiz.com/document/android/sample5_use_matrix



http://www.flysky.kr/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-multi-touch-pinch-zoom%EA%B3%BC-touch-scroll%EC%9D%B4-%EA%B0%80%EB%8A%A5%ED%95%9C-imageview/



http://blog.naver.com/monk773/90134152702


'프로그래밍 > Android' 카테고리의 다른 글

android jni opencv error  (0) 2013.09.16
android ndk ndk-build.cmd  (3) 2013.09.09
AlertDialog 안에 Layout 넣기  (0) 2013.08.16
setCancelable(boolean flags)  (0) 2013.08.16
getWindowManager()  (0) 2013.07.27