May 25, 2012

Reduce bitmap by setting inSampleSize of BitmapFactory.Options

if inSampleSize of BitmapFactory.Options set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1.

Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. ~ in my testing, both 1/2 and 1/3 have the same size bitmap generated.

Also, powers of 2 are often faster/easier for the decoder to honor.

This example demonstrate how to create a resized bitmap by user setting.

Reduce bitmap by setting inSampleSize of BitmapFactory.Options


package com.AndroidResizeBigPicture;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class AndroidResizeBigPictureActivity extends Activity {
 
 SeekBar resizeBar;
 TextView resizeLevel;
 ImageView imageView;
 
 Bitmap bitmap_Source;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        resizeBar = (SeekBar)findViewById(R.id.resizebar);
     resizeLevel = (TextView)findViewById(R.id.resizelevel);
     imageView = (ImageView)findViewById(R.id.imageview);
     
     resizeBar.setOnSeekBarChangeListener(resizeBarChangeListener);
     
     bitmap_Source = BitmapFactory.decodeResource(getResources(), R.drawable.testpicture);
    }
    
    OnSeekBarChangeListener resizeBarChangeListener
    = new OnSeekBarChangeListener(){

  @Override
  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
   // TODO Auto-generated method stub
   int ratio = (int)java.lang.Math.pow(2, progress);
   imageView.setImageBitmap(resize(ratio));
   resizeLevel.setText("ratio: 1/" + ratio);
  }

  @Override
  public void onStartTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onStopTrackingTouch(SeekBar seekBar) {
   // TODO Auto-generated method stub
   
  }};
  
 private Bitmap resize(int ratio){
  BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inSampleSize = ratio;        
        bmpFactoryOptions.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(getResources(), R.drawable.testpicture, bmpFactoryOptions);
 }
  
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <SeekBar
        android:id="@+id/resizebar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="4"
  android:progress="0"
  android:layout_margin="10dp"/>
    <TextView
        android:id="@+id/resizelevel"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="ratio: 1/1"/>
    <HorizontalScrollView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content">
     <ScrollView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
         <LinearLayout
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:orientation="vertical" >
             <ImageView
                 android:id="@+id/imageview"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:scaleType="center"
                 android:src="@drawable/testpicture"/>       
      </LinearLayout>
     </ScrollView>
 </HorizontalScrollView>
 
</LinearLayout>


Notice: You have to place your testpicture.jpg in /res/drawable/ folder.


Next:
- Auto resize to fit View


No comments:

Post a Comment

Infolinks In Text Ads