Android Bitmap zoomIn/zoomOut/rotate ——图片的缩小,放大 和旋转

本文介绍了一个在Android应用中处理图片的方法,包括图片的加载、尺寸调整及旋转等操作。通过使用BitmapFactory、Matrix类实现了图片的缩放与旋转,并展示了如何在ImageView中显示处理后的图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码 
  1. public void onCreate(Bundle icicle) {  
  2.     super.onCreate(icicle);  
  3.     LinearLayout linLayout = new LinearLayout(this);  
  4.   
  5.     // 加载需要操作的图片,这里是eoeAndroid的logo图片  
  6.     Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  
  7.             R.drawable.icon);  
  8.   
  9.     // 获取这个图片的宽和高  
  10.     int width = bitmapOrg.getWidth();  
  11.     int height = bitmapOrg.getHeight();  
  12.   
  13.     // 定义预转换成的图片的宽度和高度  
  14.     int newWidth = 200;  
  15.     int newHeight = 200;  
  16.   
  17.     // 计算缩放率,新尺寸除原始尺寸  
  18.     float scaleWidth = ((float) newWidth) / width;  
  19.     float scaleHeight = ((float) newHeight) / height;  
  20.   
  21.     // 创建操作图片用的matrix对象  
  22.     Matrix matrix = new Matrix();  
  23.   
  24.     // 缩放图片动作  
  25.     matrix.postScale(scaleWidth, scaleHeight);  
  26.   
  27.     // 旋转图片 动作  
  28.     matrix.postRotate(45);  
  29.   
  30.     // 创建新的图片  
  31.     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 00, width,  
  32.             height, matrix, true);  
  33.   
  34.     // 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中  
  35.     BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);  
  36.   
  37.     // 创建一个ImageView  
  38.     ImageView imageView = new ImageView(this);  
  39.   
  40.     // 设置ImageView的图片为上面转换的图片  
  41.     imageView.setImageDrawable(bmd);  
  42.   
  43.     // 将图片居中显示  
  44.     imageView.setScaleType(ScaleType.CENTER);  
  45.   
  46.     // 将ImageView添加到布局模板中  
  47.     linLayout.addView(imageView, new LinearLayout.LayoutParams(  
  48.             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  49.   
  50.     // 设置为本activity的模板  
  51.     setContentView(linLayout);  
  52. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值