반응형

아래 링크에서 발췌한 코드를 이용하여

 

특정 레이아웃보다

1. 가로가 큰 이미지는 가로만 fit하게

2. 세로가 큰 이미지는 가로만 fit하게

3. 작은 이미지는 그대로

 

를 만들어 보고자 한다.

 

import android.graphics.Matrix
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
}

https://stackoverflow.com/questions/7815689/how-do-you-obtain-a-drawable-object-from-a-resource-id-in-android-package

 

How do you obtain a Drawable object from a resource id in android package?

I need to get a Drawable object to display on an image button. Is there a way to use the code below (or something like it) to get an object from the android.R.drawable.* package? for example if

stackoverflow.com

 

 

 

 

 

** 이미지는 직접 연습용으로 다운받아 진행해보시길 바랍니다. **

 

< MainActivity.java >

package com.example.testpage;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    LinearLayout imageViewLayout;
    ImageView imageView;
    Button wideBtn, longBtn, largeBtn, smallBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageViewLayout = findViewById(R.id.image_view_layout);
        imageView = findViewById(R.id.image_view);

        wideBtn = findViewById(R.id.wide_image_btn);
        longBtn = findViewById(R.id.long_image_btn);
        largeBtn = findViewById(R.id.large_image_btn);
        smallBtn = findViewById(R.id.small_image_btn);

        wideBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable drawable = getResources().getDrawable(R.drawable.img_1280_720);
                Bitmap bitmap =  ((BitmapDrawable)drawable).getBitmap();
                imageView.setImageBitmap(getResizedBitmap(bitmap, imageViewLayout.getWidth(), imageViewLayout.getHeight()));
            }
        });
        longBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable drawable = getResources().getDrawable(R.drawable.img_508_1019);
                Bitmap bitmap =  ((BitmapDrawable)drawable).getBitmap();
                imageView.setImageBitmap(getResizedBitmap(bitmap, imageViewLayout.getWidth(), imageViewLayout.getHeight()));
            }
        });
        largeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable drawable = getResources().getDrawable(R.drawable.img_3000_1688);
                Bitmap bitmap =  ((BitmapDrawable)drawable).getBitmap();
                imageView.setImageBitmap(getResizedBitmap(bitmap, imageViewLayout.getWidth(), imageViewLayout.getHeight()));
            }
        });
        smallBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Drawable drawable = getResources().getDrawable(R.drawable.android_software_developer);
                Bitmap bitmap =  ((BitmapDrawable)drawable).getBitmap();
                imageView.setImageBitmap(getResizedBitmap(bitmap, imageViewLayout.getWidth(), imageViewLayout.getHeight()));
            }
        });
    }

    public Bitmap getResizedBitmap(Bitmap bm, int layoutWidth, int layoutHeight) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        Toast.makeText(getApplicationContext(), "layout width : " + layoutWidth + " layout height : " + layoutHeight + " width : " + width + " height : "  + height, Toast.LENGTH_SHORT).show();
        float scaleWidth = ((float) layoutWidth) / width;
        float scaleHeight = ((float) layoutHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();

        // RESIZE THE BIT MAP
        if(width > layoutWidth && height > layoutHeight) {
            // 레이아웃보다 큰 이미지인 경우
            matrix.postScale(scaleWidth, scaleHeight);
        } else if(width > layoutWidth) {
            // 가로만 레이아웃보다 큰 경우
            matrix.postScale(scaleWidth, scaleWidth);
        } else if(height > layoutHeight) {
            // 세로만 레이아웃보다 큰 경우
            matrix.postScale(scaleHeight, scaleHeight);
        } else {
            // 레이아웃보다 작은 이미지인 경우
            return bm;
        }

        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
                bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }
}

 

 

< activity_main.xml >

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

    <LinearLayout
        android:id="@+id/image_view_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center">
        <Button
            android:id="@+id/wide_image_btn"
            android:text="wide"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/long_image_btn"
            android:text="long"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/large_image_btn"
            android:text="latge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/small_image_btn"
            android:text="small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

 

 

 

 

반응형