android图片的缩放 .

简介:
import android.app.Activity;   
  1. import android.graphics.Bitmap;   
  2. import android.graphics.BitmapFactory;   
  3. import android.graphics.Matrix;   
  4. import android.graphics.drawable.BitmapDrawable;   
  5. import android.os.Bundle;   
  6. import android.view.ViewGroup.LayoutParams;   
  7. import android.widget.ImageView;   
  8. import android.widget.LinearLayout;   
  9. import android.widget.ImageView.ScaleType;   
  10.   
  11. public class bitmaptest extends Activity {   
  12. public void onCreate(Bundle icicle) {   
  13.         super.onCreate(icicle);   
  14.         setTitle("eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl");   
  15.         LinearLayout linLayout = new LinearLayout(this);   
  16.   
  17.         // 加载需要操作的图片,这里是eoeAndroid的logo图片   
  18.         Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),   
  19.                R.drawable.eoe_android);   
  20.   
  21.         //获取这个图片的宽和高   
  22.         int width = bitmapOrg.getWidth();   
  23.         int height = bitmapOrg.getHeight();   
  24.   
  25.         //定义预转换成的图片的宽度和高度   
  26.         int newWidth = 200;   
  27.         int newHeight = 200;   
  28.   
  29.         //计算缩放率,新尺寸除原始尺寸   
  30.         float scaleWidth = ((float) newWidth) / width;   
  31.         float scaleHeight = ((float) newHeight) / height;   
  32.   
  33.         // 创建操作图片用的matrix对象   
  34.         Matrix matrix = new Matrix();   
  35.   
  36.         // 缩放图片动作   
  37.         matrix.postScale(scaleWidth, scaleHeight);   
  38.   
  39.         //旋转图片 动作   
  40.         matrix.postRotate(45);   
  41.   
  42.         // 创建新的图片   
  43.         Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 00,   
  44.                           width, height, matrix, true);   
  45.   
  46.         //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中   
  47.         BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);   
  48.   
  49.         //创建一个ImageView   
  50.         ImageView imageView = new ImageView(this);   
  51.   
  52.         // 设置ImageView的图片为上面转换的图片   
  53.         imageView.setImageDrawable(bmd);   
  54.   
  55.         //将图片居中显示   
  56.         imageView.setScaleType(ScaleType.CENTER);   
  57.   
  58.         //将ImageView添加到布局模板中   
  59.         linLayout.addView(imageView,   
  60.           new LinearLayout.LayoutParams(   
  61.                       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT   
  62.                 )   
  63.         );   
  64.   
  65.         // 设置为本activity的模板   
  66.         setContentView(linLayout);   
  67.     }   
  68. }   
相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
45 0
|
4月前
|
XML Java Android开发
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
178 0
|
7月前
|
存储 编解码 Android开发
Android关于图片方向问题
Android关于图片方向问题
41 0
|
4月前
|
XML JSON Java
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
70 0
|
23天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
22 1
|
6月前
|
SQL 人工智能 移动开发
Android etc1tool之png图片转换pkm 和 zipalign简介
etc1tool 是一种命令行实用程序,可用于将 PNG 图片编码为 ETC1 压缩标准格式(PKM),并将 ETC1 压缩图片解码回 PNG。
|
8月前
|
Java Android开发
Android 保存资源图片到相册最新写法适用于Android10.0及以上
Android 保存资源图片到相册最新写法适用于Android10.0及以上
585 0
|
8月前
|
SQL 数据库 Android开发
Android 访问系统相册选中图片,并返回该图片的路径
Android 访问系统相册选中图片,并返回该图片的路径
99 0
|
4月前
|
API Android开发
[Android]图片加载库Glide
[Android]图片加载库Glide
55 0
|
4月前
|
Android开发
[Android]制作9-Patch图片
[Android]制作9-Patch图片
42 0