我在屏幕上显示一些TexViews。它们包含文本以及可绘制的图像。我的问题是如何根据下面的屏幕分辨率缩放可绘制对象是我当前正在运行的代码:
Drawable dr = getResources().getDrawable(R.drawable.image);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
Drawable d = new BitmapDrawable(this.getResources(),Bitmap.createScaledBitmap(bitmap, 35, 35, true));
textView.setCompoundDrawablesWithIntrinsicBounds(d, null,null,null);
在这里,我使用了比率35,但是当我们在更大的屏幕上运行该应用程序时,图像看起来很小。
有没有建议缩放图像以适合不同的屏幕尺寸?
首先,获得密度标度:
DisplayMetrics dm = new DisplayMetrics();
mainContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
float densityScale = dm.density;
然后在使用之前通过densityScale值缩放所需的大小:
float scaledWidth = 35 * densityScale;
float scaledHeight = 35 * densityScale;
Drawable dr = getResources().getDrawable(R.drawable.image);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
Drawable d = new BitmapDrawable(this.getResources(),Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true));
textView.setCompoundDrawablesWithIntrinsicBounds(d, null,null,null);
假定35是160dpi的正确尺寸。否则,您需要将35更改为适合160dpi的值。
有关支持多种分辨率/密度的更多信息,请参见此处。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句