Android图片缩放

private Drawable zoomDrawable(Drawable drawable, int w, int h) { int width = drawable.getIntrinsicWidth(); int height= drawable.getIntrinsicHeight(); // drawable转换成bitmap Bitmap oldbmp = drawableToBitmap(drawable); // 创建操作图片用的Matrix对象 Matrix matrix = new Matrix(); // 计算缩放比例 float scaleWidth = ((float)w / width); float scaleHeight = ((float)h / height); // 设置缩放比例 matrix.postScale(scaleWidth, scaleHeight); // 建立新的bitmap,其内容是对原bitmap的缩放后的图 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); // 把bitmap转换成drawable并返回 return new BitmapDrawable(newbmp); } /* drawable 转换成bitmap */ private Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); // 取drawable的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立对应bitmap Bitmap bitmap = Bitmap.createBitmap(width, height, config); // 建立对应bitmap的画布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, width, height); // 把drawable内容画到画布中 drawable.draw(canvas); return bitmap; }

//此方法来自网络

你可能感兴趣的:(android,网络,float,Matrix)