本函数可以作为一个工具函数的使用,源于网络,但是本人感觉没有别要非得写成是Drawable类型的,明明是bitmap操作,没有必要非得写成Drawable类型的,完全可以写成Bitmap类型的,ImageView中有方法完全支持直接获取Bitmap类的文件直接显示的。(保留个人意见)

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);

        }

posted on 2011-05-14 18:17  denniswang  阅读(413)  评论(0编辑  收藏  举报