Android ImageView Set loadScaledPicture(String image_path, ImageView target)

Here you can find the source of loadScaledPicture(String image_path, ImageView target)

Description

load Scaled Picture

License

Open Source License

Declaration

public static void loadScaledPicture(String image_path, ImageView target) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.widget.ImageView;

public class Main {
    public static void loadScaledPicture(String image_path, ImageView target) {
        // Get the dimensions of the View
        int targetW = target.getWidth();
        int targetH = target.getHeight();

        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(image_path, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

        //Log.i("loadScaledPicture", String.format("imageview is %dx%d. image is %dx%d. Scale factor is %d", targetW, targetH, photoW, photoH, scaleFactor));
        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;//ww w .ja  va  2 s .  c o  m

        Bitmap bitmap = BitmapFactory.decodeFile(image_path, bmOptions);
        target.setImageBitmap(bitmap);
    }
}

Related

  1. swapImages(ImageView src, ImageView dst)
  2. loadAnimation(ImageView imageView, Animation animation, int animationResource)