Thursday, February 10, 2011

drawable bitmmap resizing

  1. public class bitmaptest extends Activity {
  2.     @Override
  3.     public void onCreate(Bundle icicle) {
  4.         super.onCreate(icicle);
  5.         LinearLayout linLayout = new LinearLayout(this);
  6.        
  7.         // load the origial BitMap (500 x 500 px)
  8.         Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
  9.                R.drawable.android);
  10.        
  11.         int width = bitmapOrg.width();
  12.         int height = bitmapOrg.height();
  13.         int newWidth = 200;
  14.         int newHeight = 200;
  15.        
  16.         // calculate the scale - in this case = 0.4f
  17.         float scaleWidth = ((float) newWidth) / width;
  18.         float scaleHeight = ((float) newHeight) / height;
  19.        
  20.         // createa matrix for the manipulation
  21.         Matrix matrix = new Matrix();
  22.         // resize the bit map
  23.         matrix.postScale(scaleWidth, scaleHeight);
  24.         // rotate the Bitmap
  25.         matrix.postRotate(45);
  26.         // recreate the new Bitmap
  27.         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
  28.                           width, height, matrix, true);
  29.    
  30.         // make a Drawable from Bitmap to allow to set the BitMap
  31.         // to the ImageView, ImageButton or what ever
  32.         BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
  33.        
  34.         ImageView imageView = new ImageView(this);
  35.        
  36.         // set the Drawable on the ImageView
  37.         imageView.setImageDrawable(bmd);
  38.      
  39.         // center the Image
  40.         imageView.setScaleType(ScaleType.CENTER);
  41.        
  42.         // add ImageView to the Layout
  43.         linLayout.addView(imageView,
  44.                 new LinearLayout.LayoutParams(
  45.                       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
  46.                 )
  47.         );
  48.        
  49.         // set LinearLayout as ContentView
  50.         setContentView(linLayout);
  51.     }
  52. }
     Ref Website:http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
     

No comments:

Post a Comment