欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android中缩放图片的一个方法,

来源: 开发者 投稿于  被查看 43573 次 评论:7

Android中缩放图片的一个方法,


<无详细内容>

1.[Java]代码

public static Drawable resizeImage(Bitmap bitmap, int w, int h) {

                // load the origial Bitmap
                Bitmap BitmapOrg = bitmap;

                int width = BitmapOrg.getWidth();
                int height = BitmapOrg.getHeight();
                int newWidth = w;
                int newHeight = h;

                // calculate the scale
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;

                // create a matrix for the manipulation
                Matrix matrix = new Matrix();
                // resize the Bitmap
                matrix.postScale(scaleWidth, scaleHeight);
                // if you want to rotate the Bitmap
                // matrix.postRotate(45);

                // recreate the new Bitmap
                Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                                height, matrix, true);

                // make a Drawable from Bitmap to allow to set the Bitmap
                // to the ImageView, ImageButton or what ever
                return new BitmapDrawable(resizedBitmap);

        }

用户评论