Android 图片缩放
//创建一直合适的空白,大小100*100的图片RGB_565会使透明图片变黑建议用ARGB_8888 Bitmap newBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565); //初始化画布 Canvas canvas = new Canvas(newBitmap); //将图片缩小到合适位置 canvas.drawBitmap(bitmap, null, new RectF(0, 0, 100, 100), null); //通过(BitmapDrawable)转换成BitmapDrawable因为Drawable是BitmapDrawable的父类所以直接转换为Drawable BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),newBitmap); //防止oom回收图片 bitmap.recycle();
int width = bitmap.getWidth(); int height = origin.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(ratio, ratio); Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false); origin.recycle();