Android Open Source - semana-isw2013 Bitmap Hack






From Project

Back to project page semana-isw2013.

License

The source code is released under:

Apache License

If you think the Android project semana-isw2013 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 mobi.itson.semanaisw2013app.utils;
/*from ww w .  j  a  va 2 s.co  m*/
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitmapHack {

  
  /**
   * Method that calculates the In SampleSize
   * @param options Options from BitmapFactory
   * @param reqWidth The require Width
   * @param reqHeight The Require Height
   * @param samplesize The sample size
   * @return inSampleSize The In sample size
   */
  public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight, int samplesize){
    //Define here the Width and Height by outwidth and outheight
    //also the insamplesize
    options.inJustDecodeBounds = true;
    final int width = options.outWidth;
    final int height = options.outHeight;
    int inSampleSize = samplesize;
    
    //if Width or Height are bigger than their reqs, will get the ratios
    if(width > reqWidth || height > reqHeight){
      final int widthRatio = Math.round( (float) width / (float) reqWidth);
      final int heightRatio = Math.round( (float) height / (float) reqHeight);
      
      //the lowest ratio have to be the insamplesize (in this case de height)
      return inSampleSize = (heightRatio < widthRatio) ? heightRatio : widthRatio;
    }
    return inSampleSize;
  }
  
  
  /**
   * Method that decode a bitmap from Resource calculating a insamplesize
   * @param res Resources
   * @param resId id's Resources
   * @param reqWidth require width
   * @param reqHeight requiere height
   * @param inSampleSize InsampleSize
   * @return Decoded resource
   */
  public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight, int inSampleSize){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight, inSampleSize);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
  }
  
  /**
   * Method that decode a bitmap from Resource
   * @param res Resources
   * @param resId id's Resources
   * @param reqWidth require width
   * @param reqHeight requiere height
   * @return Decoded resource
   */
  public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight){
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight, 2);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);  
  }
}




Java Source Code List

mobi.itson.semanaisw2013app.AboutUsActivity.java
mobi.itson.semanaisw2013app.ConferenceActivity.java
mobi.itson.semanaisw2013app.Conference.java
mobi.itson.semanaisw2013app.ConferencesPagerFragment.java
mobi.itson.semanaisw2013app.GalleryActivity.java
mobi.itson.semanaisw2013app.ImagePagerActivity.java
mobi.itson.semanaisw2013app.MainActivity.java
mobi.itson.semanaisw2013app.ScheduleActivity.java
mobi.itson.semanaisw2013app.SplashActivity.java
mobi.itson.semanaisw2013app.WebView_Register_Actvity.java
mobi.itson.semanaisw2013app.WorkshopActivity.java
mobi.itson.semanaisw2013app.WorkshopPagerFragment.java
mobi.itson.semanaisw2013app.Workshop.java
mobi.itson.semanaisw2013app.utils.ActivityUtils.java
mobi.itson.semanaisw2013app.utils.BitmapHack.java
mobi.itson.semanaisw2013app.utils.Constants.java
mobi.itson.semanaisw2013app.utils.HttpHelper.java
mobi.itson.semanaisw2013app.utils.SemanaUpload.java
mobi.itson.semanaisw2013app.utils.TouchHighlightImageButton.java
mobi.itson.semanaisw2013app.utils.UploadImage.java