이미지 크기 변경(Image resize)

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

sitelink1 https://m.blog.naver.com/utime/150090784436 
sitelink2  
sitelink3  

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

 

번호 제목 글쓴이 날짜 조회 수
48 [안드로이드 웹뷰] 파일 시스템으로 부터 HTML 로딩 황제낙엽 2018.08.21 87
47 내부 저장소 접근 함수 API 와 실제 저장소 경로 황제낙엽 2018.08.21 85
46 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-06 file 황제낙엽 2018.08.19 991
45 안드로이드 파일 객체 생성자 황제낙엽 2018.08.19 90
44 파일 입출력(내장 메모리, 외장메모리) 황제낙엽 2018.08.19 720
43 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-05 황제낙엽 2018.08.19 132
42 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-04 file 황제낙엽 2018.08.12 1711
41 뷰 캡처하여 이미지 파일로 저장하기(SD카드로 화면 캡처)-03 file 황제낙엽 2018.08.12 150
40 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-02 황제낙엽 2018.08.12 133
39 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-01 황제낙엽 2018.08.12 86
38 뷰 캡처하여 이미지 파일로 저장하기(화면 캡처)-00 황제낙엽 2018.08.12 109
37 [HttpURLConnection] 서버와 세션 유지 황제낙엽 2018.08.12 58
36 [HttpURLConnection] 세션 관리 황제낙엽 2018.08.12 27
» 이미지 크기 변경(Image resize) file 황제낙엽 2018.08.09 108
34 Emulator: audio: Failed to create voice `adc' 황제낙엽 2018.08.06 1007
33 install_failed_invalid_apk file 황제낙엽 2018.08.06 49
32 Image to byte Array (바로 사용가능한 JPEG 파일) 황제낙엽 2018.07.24 612
31 안드로이드 스튜디오(Android Studio) 최적화 file 황제낙엽 2018.02.07 433
30 Android Studio for beginners, Part 4: Advanced tools and plugins (2) file 황제낙엽 2018.02.02 14
29 Android Studio for beginners, Part 4: Advanced tools and plugins (1) file 황제낙엽 2018.02.02 26