Android Open Source - GhostStories Bitmap Utils






From Project

Back to project page GhostStories.

License

The source code is released under:

GNU General Public License

If you think the Android project GhostStories 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 games.ghoststories.utils;
/*from w w w  . j a v  a  2  s. com*/
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

/**
 * Utility class containing methods to manipulate bitmaps
 */
public abstract class BitmapUtils {     

   /**
    * 
    * @param pOptions
    * @param pReqWidth
    * @param pReqHeight
    * @return
    */
   public static int calculateInSampleSize(
         BitmapFactory.Options pOptions, int pReqWidth, int pReqHeight) {
      // Raw height and width of image
      final int height = pOptions.outHeight;
      final int width = pOptions.outWidth;
      int inSampleSize = 1;

      if (height > pReqHeight || width > pReqWidth) {
         if (width > height) {
            inSampleSize = Math.round((float)height / (float)pReqHeight);
         } else {
            inSampleSize = Math.round((float)width / (float)pReqWidth);
         }
      }
      return inSampleSize;
   }

   /**
    * Decode a bitmap from the given file
    * @param pPath The path to the bitmap to load
    * @param pReqWidth The width of the loaded bitmap
    * @param pReqHeight The height of the loaded bitmap
    * @return The decoded bitmap
    */
   public static Bitmap decodeSampledBitmapFromFile(String pPath,
         int pReqWidth, int pReqHeight) {

      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeFile(pPath, options);     

      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, pReqWidth, pReqHeight);

      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeFile(pPath, options);     
   }

   /**
    * Decode a bitmap from the given resource id
    * @param pRes Reference to the resources
    * @param pResId The id of the bitmap resource to decode
    * @param pReqWidth The width of the loaded bitmap
    * @param pReqHeight The height of the loaded bitmap
    * @return The decoded bitmap
    */
   public static Bitmap decodeSampledBitmapFromResource(Resources pRes, int pResId,
         int pReqWidth, int pReqHeight) {

      // First decode with inJustDecodeBounds=true to check dimensions
      final BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeResource(pRes, pResId, options);

      // Calculate inSampleSize
      options.inSampleSize = calculateInSampleSize(options, pReqWidth, pReqHeight);

      // Decode bitmap with inSampleSize set
      options.inJustDecodeBounds = false;
      return BitmapFactory.decodeResource(pRes, pResId, options);
   }

   /**
    * Rotates the given bitmap the number of degrees. Allocates a new 
    * bitmap.
    * @param pOriginal The bitmap to rotate
    * @param pDegrees The degrees to rotate the bitmap.
    * @return A new rotated bitmap
    */
   public static Bitmap rotateBitmap(Bitmap pOriginal, float pDegrees) {
      Matrix matrix = new Matrix();
      matrix.postRotate(pDegrees);      
      return Bitmap.createBitmap(pOriginal, 0, 0, 
            pOriginal.getWidth(), pOriginal.getHeight(), 
            matrix, true);
   }

   /**
    * Rotates and scales the specified bitmap by the given amount. Allocates
    * a new bitmap.
    * @param pOriginal The original bitmap
    * @param pDegrees The degrees to rotate the bitmap
    * @param pScaleX The scale x factor
    * @param pScaleY The scale y factor
    * @return A new rotated and scaled bitmap
    */
   public static Bitmap rotateAndScaleBitmap(Bitmap pOriginal, float pDegrees,
         float pScaleX, float pScaleY) {
      Matrix matrix = new Matrix();
      matrix.postScale(pScaleX, pScaleY);
      matrix.postRotate(pDegrees);
      return Bitmap.createBitmap(pOriginal, 0, 0, 
            pOriginal.getWidth(), pOriginal.getHeight(), 
            matrix, true);
   }        

   /**
    * Scales the specified bitmap by the given amount.
    * @param pOriginal The original bitmap
    * @param pScaleX The scale x factor
    * @param pScaleY The scale y factor
    * @return A new scaled bitmap
    */
   public static Bitmap scaleBitmap(Bitmap pOriginal, float pScaleX, float pScaleY) {
      Matrix matrix = new Matrix();
      matrix.postScale(pScaleX, pScaleY);
      return Bitmap.createBitmap(pOriginal, 0, 0, 
            pOriginal.getWidth(), pOriginal.getHeight(), 
            matrix, true);
   }      
}




Java Source Code List

com.drawable.shapes.GradientRectangle.java
com.interfaces.IDraggable.java
com.utils.AndroidUtils.java
com.utils.AnimationUtils2.java
com.utils.ImageLoadingTask.java
com.utils.ImageRotationTask.java
com.utils.ImageViewUtils.java
com.views.NumberedImageView.java
com.views.ToggledImageView.java
com.views.layouts.ScaledLinearLayout.java
com.views.layouts.ScaledRelativeLayout.java
com.views.layouts.SquareGridLayout.java
com.views.layouts.SquareLinearLayout.java
com.views.layouts.SquareTableLayout.java
com.views.layouts.ZoomableRelativeLayout.java
com.views.listeners.DragTouchListener.java
games.ghoststories.activities.GameLoadingActivity.java
games.ghoststories.activities.GameScreenActivity.java
games.ghoststories.activities.TitleActivity.java
games.ghoststories.controllers.GhostDeckController.java
games.ghoststories.controllers.HaunterController.java
games.ghoststories.controllers.PlayerBoardCardController.java
games.ghoststories.controllers.VillageTileController.java
games.ghoststories.controllers.combat.CombatAreaController.java
games.ghoststories.controllers.combat.DiceDragListener.java
games.ghoststories.controllers.combat.GhostDragListener.java
games.ghoststories.controllers.combat.TaoTokenDragListener.java
games.ghoststories.data.DragData.java
games.ghoststories.data.GameBoardData.java
games.ghoststories.data.GhostData.java
games.ghoststories.data.GhostDeckData.java
games.ghoststories.data.GhostGraveyardData.java
games.ghoststories.data.GhostStoriesBitmaps.java
games.ghoststories.data.GhostStoriesConstants.java
games.ghoststories.data.GhostStoriesGameManager.java
games.ghoststories.data.PlayerData.java
games.ghoststories.data.TokenSupplyData.java
games.ghoststories.data.interfaces.IGameBoardListener.java
games.ghoststories.data.interfaces.IGamePhaseListener.java
games.ghoststories.data.interfaces.IGameTokenListener.java
games.ghoststories.data.interfaces.IGhostDeckListener.java
games.ghoststories.data.interfaces.IGhostListener.java
games.ghoststories.data.interfaces.ITokenListener.java
games.ghoststories.data.interfaces.IVillageTileListener.java
games.ghoststories.data.village.BuddhistTempleTileData.java
games.ghoststories.data.village.CircleOfPrayerTileData.java
games.ghoststories.data.village.VillageTileDataFactory.java
games.ghoststories.data.village.VillageTileData.java
games.ghoststories.enums.EBoardLocation.java
games.ghoststories.enums.ECardLocation.java
games.ghoststories.enums.EColor.java
games.ghoststories.enums.ECombatPhase.java
games.ghoststories.enums.EDiceSide.java
games.ghoststories.enums.EDice.java
games.ghoststories.enums.EDifficulty.java
games.ghoststories.enums.EDragItem.java
games.ghoststories.enums.EGamePhase.java
games.ghoststories.enums.EGhostAbility.java
games.ghoststories.enums.EHaunterLocation.java
games.ghoststories.enums.EPlayerAbility.java
games.ghoststories.enums.ETileLocation.java
games.ghoststories.enums.EVillageTile.java
games.ghoststories.fragments.AuxAreaFragment.java
games.ghoststories.fragments.GameboardFragment.java
games.ghoststories.utils.BitmapUtils.java
games.ghoststories.utils.GameUtils.java
games.ghoststories.utils.XmlUtils.java
games.ghoststories.views.GameScreen.java
games.ghoststories.views.aux_area.CardInfoView.java
games.ghoststories.views.aux_area.GamePhaseDetailsView.java
games.ghoststories.views.aux_area.GamePhaseView.java
games.ghoststories.views.aux_area.GhostDeckView.java
games.ghoststories.views.aux_area.GhostGraveyardCardView.java
games.ghoststories.views.aux_area.PlayerInfoView.java
games.ghoststories.views.combat.CombatDamageView.java
games.ghoststories.views.combat.CombatDiceAreaView.java
games.ghoststories.views.combat.CombatDiceView.java
games.ghoststories.views.combat.CombatGhostView.java
games.ghoststories.views.combat.CombatInstructionsView.java
games.ghoststories.views.combat.CombatRollView.java
games.ghoststories.views.combat.CombatView.java
games.ghoststories.views.combat.ExtraCombatDiceView.java
games.ghoststories.views.combat.GhostHealthView.java
games.ghoststories.views.common.AbstractNumberedTokenView.java
games.ghoststories.views.common.BuddhaTokenView.java
games.ghoststories.views.common.QiTokenView.java
games.ghoststories.views.common.TaoTokenView.java
games.ghoststories.views.common.YinYangTokenView.java
games.ghoststories.views.gameboard.PlayerAreaView.java
games.ghoststories.views.gameboard.PlayerBoardCardView.java
games.ghoststories.views.gameboard.PlayerBoardView.java
games.ghoststories.views.gameboard.PlayerTokenAreaView.java
games.ghoststories.views.gameboard.VillageTileView.java
games.ghoststories.views.graveyard.GraveyardScrollView.java
games.ghoststories.views.graveyard.GraveyardView.java
games.ghoststories.views.title.TitleButton.java
games.ghoststories.views.title.TitleScreen.java
games.ghoststories.views.village.CircleOfPrayerView.java