Android Open Source - singly-android Bitmap Utils






From Project

Back to project page singly-android.

License

The source code is released under:

MIT License

If you think the Android project singly-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.singly.android.util;
/*  www .  jav  a2s  . c  o  m*/
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * Utility methods for working with Bitmap instances.
 */
public class BitmapUtils {

  /**
   * Determines the best sample rate for the Bitmap for the given width and 
   * height.  Then decodes the Bitmap using that sample rate.  Note that the 
   * Bitmap isn't returned at the width and height, it is returned using a
   * sampling rate that is appropriate for the desired width and height as 
   * Android will automatically scale a Bitmap to a given component area.
   * 
   * Decoding happens twice this method.  Once for getting the scale and once
   * for decoding at scale.  Because of this it is best to serialize the Bitmap
   * once it is decoded at scale to prevent double decoding on each load.
   * 
   * @param bytes The image bytes to be loaded into a Bitmap.
   * @param width The desired max width.
   * @param height The desired max height.
   * 
   * @return
   */
  public static Bitmap decodeAndScaleImage(byte[] bytes, int width, int height) {

    try {

      // decode the image file
      BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
      scaleOptions.inJustDecodeBounds = true;
      BitmapFactory.decodeByteArray(bytes, 0, bytes.length, scaleOptions);

      // find the correct scale value as a power of 2.
      int scale = 1;
      while (scaleOptions.outWidth / scale / 2 >= width
        && scaleOptions.outHeight / scale / 2 >= height) {
        scale *= 2;
      }

      // decode with the sample size
      BitmapFactory.Options outOptions = new BitmapFactory.Options();
      outOptions.inSampleSize = scale;
      return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, outOptions);
    }
    catch (Exception e) {
    }

    return null;
  }

}




Java Source Code List

com.singly.android.client.AsyncApiResponseHandler.java
com.singly.android.client.AuthenticationActivity.java
com.singly.android.client.AuthenticationWebViewListener.java
com.singly.android.client.BaseAuthenticationWebViewClient.java
com.singly.android.client.FacebookAuthenticationActivity.java
com.singly.android.client.SinglyClient.java
com.singly.android.component.AbstractCachingBlockLoadedListAdapter.java
com.singly.android.component.AuthenticatedServicesActivity.java
com.singly.android.component.AuthenticatedServicesAdapter.java
com.singly.android.component.AuthenticatedServicesFragment.java
com.singly.android.component.DeviceOwnerActivity.java
com.singly.android.component.Friend.java
com.singly.android.component.FriendsListActivity.java
com.singly.android.component.FriendsListAdapter.java
com.singly.android.component.FriendsListFragment.java
com.singly.android.component.FriendsListRowClickListener.java
com.singly.android.component.SinglyService.java
com.singly.android.component.TableOfContentsFragment.java
com.singly.android.component.TableOfContentsTouchListener.java
com.singly.android.examples.MainActivity.java
com.singly.android.util.BitmapUtils.java
com.singly.android.util.ImageCacheListener.java
com.singly.android.util.ImageInfo.java
com.singly.android.util.JSON.java
com.singly.android.util.RemoteImageCache.java
com.singly.android.util.SinglyUtils.java
com.singly.android.util.URLUtils.java