我有一个应用程序,嵌入式可绘制48x48像素,71,12像素/英寸 我通过流将相同的图像加载到Web服务器,然后加载该流
return new BitmapDrawable(getActivity().getResources(), new ByteArrayInputStream(imageThumbnail));
显示的结果是:
如何让BitmapDrawable与其他drawable一样缩放?
答案 0 :(得分:16)
你可以触发android bitmapfactory自动缩放位图,代码为:
BitmapFactory.Options options = new BitmapFactory.Options();
DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
options.inScreenDensity = metrics.densityDpi;
options.inTargetDensity = metrics.densityDpi;
options.inDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bm = BitmapFactory.decodeStream(in, null, options);
in.close();
BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bm);
答案 1 :(得分:11)
做这样的事情:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Bitmap bitmapOrg = new BitmapDrawable(getResources(), new ByteArrayInputStream(imageThumbnail)).getBitmap();
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
或者
也许尝试一种不同的方法...尝试在XML布局中设置图像的高度和宽度,我猜你现在有了带有wrap_content高度和宽度的ImageView,尝试将高度和宽度设置为48dip
答案 2 :(得分:-3)
获取屏幕的当前密度并将drawable设置为该密度
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi
myDrawable.setDensity(densityDpi);