이미지 크기 변경(Image resize)

황제낙엽 2018.08.09 06:23 조회 수 : 1379

ImageView에 있는 그림을 변경해 보도록 하자.

mail.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"
    >
    <ImageView 기존 원본 이미지
        android:id="@+id/ImageView01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
    />
    
    <Button 변경 버튼
        android:text="Resize" 
        android:id="@+id/Button01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
    />
    
    <ImageView 변경된 이미지가 보여질 부분
        android:id="@+id/ImageView02" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
    />
</LinearLayout>

 



ImageResize.java

 
package com.androidstudy.ImageResize;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageResize extends Activity {
    private ImageView ImgvResize = null;
    private ImageView ImgvSource = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ImgvSource = (ImageView)findViewById(R.id.ImageView01);
        ImgvSource.setImageResource(R.drawable.android_productivity);

        ImgvResize = (ImageView)findViewById(R.id.ImageView02);
        ImgvResize.setScaleType(ScaleType.CENTER);
        
        Button btn =(Button)findViewById(R.id.Button01);
        btn.setOnClickListener( new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                ImageResizeClickEvent( v );
            }
        });
    }
    
    /*
     *    Bitmap 이미지 리사이즈
     *    Src : 원본 Bitmap
     *  newHeight : 새로운 높이
     *  newWidth : 새로운 넓이
     *  참고 소스 : http://skyswim42.egloos.com/3477279 ( webview 에서 capture 화면 resizing 하는 source 도 있음 )
     */
    private BitmapDrawable BitmapResizePrc( Bitmap Src, int newHeight, int newWidth)
    {
        BitmapDrawable Result = null;
        
        int width = Src.getWidth(); 
        int height = Src.getHeight(); 

        // calculate the scale - in this case = 0.4f 
        float scaleWidth = ((float) newWidth) / width; 
        float scaleHeight = ((float) newHeight) / height; 
        
        // createa matrix for the manipulation 
        Matrix matrix = new Matrix(); 
        // resize the bit map 
        
        matrix.postScale(scaleWidth, scaleHeight);
        
        // rotate the Bitmap  회전 시키려면 주석 해제!
        //matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(Src, 0, 0, width, height, matrix, true);

        // check
        width = resizedBitmap.getWidth(); 
        height = resizedBitmap.getHeight();
        
        Log.i("ImageResize", "Image Resize Result : " + Boolean.toString((newHeight==height)&&(newWidth==width)) );
        
        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        Result = new BitmapDrawable(resizedBitmap);
        
        return Result;
    }
    
    private void ImageResizeClickEvent( View v )
    {
        
        ImgvSource.buildDrawingCache();
        
        Bitmap bitmapOrg = ImgvSource.getDrawingCache();
        BitmapDrawable bmpResize = this.BitmapResizePrc(bitmapOrg, 100, 120);

        // set the Drawable on the ImageView
        ImgvResize.setImageDrawable(bmpResize);
    }
}
 



결과

 

imgresize_utime.jpg

 

번호 제목 글쓴이 날짜 조회 수
128 PC를 위한 최고의 안드로이드 에뮬레이터 황제낙엽 2023.01.18 3138
127 [Jetpack Compose UI / 상추님] GDG Jetpack compose 코드랩 수료 후기 (feat. 굿즈) 황제낙엽 2022.12.24 2372
126 [Jetpack Compose UI / kyj1991719] compose - Compose 이해 황제낙엽 2022.12.24 4026
125 [Jetpack Compose UI / 꾸준] Compose 가 뭔데...? 황제낙엽 2022.12.24 4036
124 [Jetpack Compose UI / IIVIKI] Compose 학습과 분석 황제낙엽 2022.12.24 4050
123 [Jetpack Compose UI] 스터디 시작하기 황제낙엽 2022.12.24 4001
122 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 바텀네비게이션 뷰, 네비게이션 AAC file 황제낙엽 2022.12.22 3882
121 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 뷰바인딩 file 황제낙엽 2022.12.22 3852
120 [개발하는 정대리] 취준생을 위한 안드로이드 앱만들기 - 라이브데이터 뷰모델 file 황제낙엽 2022.12.21 3828
119 [대왕놀] ImageButton in Compose (IconButton) 황제낙엽 2022.12.20 4107
118 [대왕놀] compose instrumentation test tutorial 황제낙엽 2022.12.20 3987
117 [대왕놀] Switch in Compose 황제낙엽 2022.12.20 4107
116 [대왕놀] EditText in Compose (TextField) 황제낙엽 2022.12.20 4023
115 [대왕놀] margin in Compose 황제낙엽 2022.12.20 4105
114 [대왕놀] modifier in compose 황제낙엽 2022.12.20 4183
113 [대왕놀] Button in Compose file 황제낙엽 2022.12.20 4226
112 [대왕놀] TextView in Compose (Text) 황제낙엽 2022.12.20 4084
111 [대왕놀] ImageView in Compose (Image) 황제낙엽 2022.12.20 4019
110 [대왕놀] LinearLayout in Compose (Row, Column) 황제낙엽 2022.12.20 4116
109 [대왕놀] Introduction to Jetpack compose 황제낙엽 2022.12.20 3965