Android 图片缩放

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

  1. import android.app.Activity;   
  2. import android.graphics.Bitmap;   
  3. import android.graphics.BitmapFactory;   
  4. import android.graphics.Matrix;   
  5. import android.graphics.drawable.BitmapDrawable;   
  6. import android.os.Bundle;   
  7. import android.view.ViewGroup.LayoutParams;   
  8. import android.widget.ImageView;   
  9. import android.widget.LinearLayout;   
  10. import android.widget.ImageView.ScaleType;   
  11.   
  12. public class bitmaptest extends Activity {   
  13. public void onCreate(Bundle icicle) {   
  14.         super.onCreate(icicle);   
  15.         setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl");   
  16.         LinearLayout linLayout = new LinearLayout(this);   
  17.   
  18.         // 加载需要操作的图片,这里是eoeAndroid的logo图片   
  19.         Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),   
  20.                R.drawable.eoe_android);   
  21.   
  22.         //获取这个图片的宽和高   
  23.         int width = bitmapOrg.getWidth();   
  24.         int height = bitmapOrg.getHeight();   
  25.   
  26.         //定义预转换成的图片的宽度和高度   
  27.         int newWidth = 200;   
  28.         int newHeight = 200;   
  29.   
  30.         //计算缩放率,新尺寸除原始尺寸   
  31.         float scaleWidth = ((float) newWidth) / width;   
  32.         float scaleHeight = ((float) newHeight) / height;   
  33.   
  34.         // 创建操作图片用的matrix对象   
  35.         Matrix matrix = new Matrix();   
  36.   
  37.         // 缩放图片动作   
  38.         matrix.postScale(scaleWidth, scaleHeight);   
  39.   
  40.         //旋转图片 动作   
  41.         matrix.postRotate(45);   
  42.   
  43.         // 创建新的图片   
  44.         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 00,   
  45.                           width, height, matrix, true);   
  46.   
  47.         //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中   
  48.         BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);   
  49.   
  50.         //创建一个ImageView   
  51.         ImageView imageView = new ImageView(this);   
  52.   
  53.         // 设置ImageView的图片为上面转换的图片   
  54.         imageView.setImageDrawable(bmd);   
  55.   
  56.         //将图片居中显示   
  57.         imageView.setScaleType(ScaleType.CENTER);   
  58.   
  59.         //将ImageView添加到布局模板中   
  60.         linLayout.addView(imageView,   
  61.           new LinearLayout.LayoutParams(   
  62.                       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT   
  63.                 )   
  64.         );   
  65.   
  66.         // 设置为本activity的模板   
  67.         setContentView(linLayout);   
  68.     }   
  69. }   
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ImageView.ScaleType; public class bitmaptest extends Activity { public void onCreate(Bundle icicle) {         super.onCreate(icicle);         setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl");         LinearLayout linLayout = new LinearLayout(this);         // 加载需要操作的图片,这里是eoeAndroid的logo图片         Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),                R.drawable.eoe_android);         //获取这个图片的宽和高         int width = bitmapOrg.getWidth();         int height = bitmapOrg.getHeight();         //定义预转换成的图片的宽度和高度         int newWidth = 200;         int newHeight = 200;         //计算缩放率,新尺寸除原始尺寸         float scaleWidth = ((float) newWidth) / width;         float scaleHeight = ((float) newHeight) / height;         // 创建操作图片用的matrix对象         Matrix matrix = new Matrix();         // 缩放图片动作         matrix.postScale(scaleWidth, scaleHeight);         //旋转图片 动作         matrix.postRotate(45);         // 创建新的图片         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,                           width, height, matrix, true);         //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中         BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);         //创建一个ImageView         ImageView imageView = new ImageView(this);         // 设置ImageView的图片为上面转换的图片         imageView.setImageDrawable(bmd);         //将图片居中显示         imageView.setScaleType(ScaleType.CENTER);         //将ImageView添加到布局模板中         linLayout.addView(imageView,           new LinearLayout.LayoutParams(                       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT                 )         );         // 设置为本activity的模板         setContentView(linLayout);     } }

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值