Shrink Bitmap and Cropped Bitmap in android

by Thursday, August 07, 2014 0 comments
Shrink Bitmap:

 public Bitmap ShrinkBitmap(String file, int width, int height){  
  BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
      bmpFactoryOptions.inJustDecodeBounds = true;  
      Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);  
      int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);  
      int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);  
      if (heightRatio > 1 || widthRatio > 1)  
      {  
       if (heightRatio > widthRatio)  
       {  
       bmpFactoryOptions.inSampleSize = heightRatio;  
       } else {  
       bmpFactoryOptions.inSampleSize = widthRatio;  
       }  
      }  
      bmpFactoryOptions.inJustDecodeBounds = false;  
      bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);  
  return bitmap;  
 }  

Cropped Bitmap:

 public Bitmap getCroppedBitmap(Bitmap bitmap) {  
      Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
                bitmap.getHeight(), Config.ARGB_8888);  
      Canvas canvas = new Canvas(output);  
      final int color = 0xff424242;  
      final Paint paint = new Paint();  
      final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
      paint.setAntiAlias(true);  
      canvas.drawARGB(0, 0, 0, 0);  
      paint.setColor(color);  
      // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
      canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,  
                bitmap.getWidth() / 2, paint);  
      paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
      canvas.drawBitmap(bitmap, rect, rect, paint);  
      //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);  
      //return _bmp;  
      return output;  
 }  

Unknown

Androider

Welcome to Android-Action Blog. I’m a normal guy, who is passionate about Mobile Coding. Here I am writing about Android. Happy learning

0 comments:

Post a Comment