在android平台缩放和旋转图片

  1. package com.j2medev;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Matrix;
  6. import android.graphics.drawable.BitmapDrawable;
  7. import android.os.Bundle;
  8. import android.view.ViewGroup.LayoutParams;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11. import android.widget.ImageView.ScaleType;
  12. public class ImageTest extends Activity {
  13. /** Called when the activity is first created. */
  14. @Override
  15. public void onCreate(Bundle icicle) {
  16.   super.onCreate(icicle);
  17.   setContentView(R.layout.main);
  18.   LinearLayout linLayout = new LinearLayout(this);
  19.   // load the origial BitMap (500 x 500 px)
  20.   Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
  21.     R.drawable.android_118);
  22.   int width = bitmapOrg.getWidth();
  23.   int height = bitmapOrg.getHeight();
  24.   int newWidth = 200;
  25.   int newHeight = 200;
  26.   // calculate the scale - in this case = 0.4f
  27.   float scaleWidth = ((float) newWidth) / width;
  28.   float scaleHeight = ((float) newHeight) / height;
  29.   // createa matrix for the manipulation
  30.   Matrix matrix = new Matrix();
  31.   // resize the bit map
  32.   matrix.postScale(scaleWidth, scaleHeight);
  33.   // rotate the Bitmap
  34.   matrix.postRotate(45);
  35.   // recreate the new Bitmap
  36.   Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
  37.     height, matrix, true);
  38.   // make a Drawable from Bitmap to allow to set the BitMap
  39.   // to the ImageView, ImageButton or what ever
  40.   BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
  41.   ImageView imageView = new ImageView(this);
  42.   // set the Drawable on the ImageView
  43.   imageView.setImageDrawable(bmd);
  44.   // center the Image
  45.   imageView.setScaleType(ScaleType.CENTER);
  46.   // add ImageView to the Layout
  47.   linLayout.addView(imageView, new LinearLayout.LayoutParams(
  48.     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  49.   // set LinearLayout as ContentView
  50.   setContentView(linLayout);
  51. }
  52. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值