我遇到了一个让我抓狂的问题。
我有 4 个函数可以使用 View 、位图、屏幕等返回的密度 (1.5) 和宽度/高度值在整个 UI 中正确调整图像大小。
这在 SG II 上非常有效。但是在报告密度为 0.75 的 HTC Wildfire 上......一些图像在屏幕上视觉上太小了大约 25%......但是当我决定将密度覆盖为 1.0 时,这些图像突然适合而其他图像变得酥脆,但尺寸不再正确...这让我发疯...
我只能得出结论,我在某些地方比较苹果和梨(像素和 DIP),但我无法让我的测试与我阅读的内容相符,所以为了对某些事情有 100% 的把握:
...
我会在这里得到像素吗?或倾角? :
int bitmapWidth = bitmap.getWidth();
假设 underneat 是一个包含位图的 ImageView 。像素还是 DIP?
int widthParent = view.getWidth();
下面实际上是 DIP,对吧?
getContext().getResources().getDisplayMetrics().widthPixels;
...
这是我的代码不起作用(这段代码给出的位图在视觉上可能占用 2/3s 的宽度,而它应该占用 100%)如果您检查注释,像素值显然是正确的,但最终结果不正确:
vto.addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mainLogo.getViewTreeObserver().removeGlobalOnLayoutListener(this);
mainLogo.setScaleType(ImageView.ScaleType.CENTER_INSIDE); // mainLogo = ImageView
SharedCode.sharedUtilScaleImage_Width(mainLogo, false);
}
}
);
// ...
public static void sharedUtilScaleImage_Width(ImageView view, boolean tryBackground)
{
Drawable drawing = null;
boolean useBackground = false;
if (drawing == null) {
drawing = view.getDrawable();
}
if (drawing == null) {
if (tryBackground) {
drawing = view.getBackground();
useBackground = true;
}
}
if (drawing == null) {
return;
}
if (!(drawing instanceof BitmapDrawable)) {
return;
}
Bitmap bitmap = ((BitmapDrawable)drawing).getBitmap();
if (bitmap == null) {
return;
}
//--
int bitmapWidth = bitmap.getWidth(); // returns 770 (which is correct checking on disk)
int bitmapHeight = bitmap.getHeight();
// float density = 1;
// density = MicApp.getContext().getResources().getDisplayMetrics().density; // 1.5
float widthScreen = MicApp.getContext().getResources().getDisplayMetrics().widthPixels; // returns 480
int widthParent = view.getWidth(); // returns 480, should be same as screen
//--
float xScale = ( (float) widthParent / (float) bitmapWidth);
Matrix matrix = new Matrix();
matrix.postScale(xScale, xScale);
//--
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
int bitmapWidth2 = scaledBitmap.getWidth(); // 480
int bitmapHeight2 = scaledBitmap.getHeight();
//--
BitmapDrawable result = new BitmapDrawable(scaledBitmap);
//--
if (useBackground) {
LayoutParams layoutparams = new LayoutParams(bitmapWidth, bitmapHeight);
view.setLayoutParams(layoutparams);
view.setBackgroundDrawable(result);
}
else {
view.setImageDrawable(result);
}
}
最佳答案
解码图像和显示图像之间有几个步骤。这些步骤通常用于以正确的比例显示来自资源的图像。如果您只将图像放在 /res/drawable-mdpi/
而不是其他文件夹中,它仍然可以在具有其他密度的设备上以正确的大小显示。
如果您想在 x-hdpi
(320dpi) 设备上加载适用于 mdpi
(160dpi) 的图像,并希望它以相同的尺寸显示实际上,您需要将其像素大小加倍。例如。 100x100 像素的图像需要以 200x200 像素显示。
涉及缩放的地方是
- 解码图像(参见例如
BitmapFactory.Options#inDensity
)。BitmapFactory
已经可以将 100x100 的 png 图像解码为 200x200 的图像。 位图
自身的密度。这是每个位图的属性,用于存储图像的密度。如果BitmapFactory
会将 100x100 mdpi 图像解码为 200x200,那么密度基本上会说“我是 x-hdpi”。您还可以在解码时禁用缩放,并获得具有 mdpi 密度的 100x100 图像。显示该图像需要绘制缩放为 2 倍的图像。BitmapDrawable
的目标密度,通常是设备的密度。如果BitmapDrawable
必须绘制密度不匹配的Bitmap
,它将绘制缩放版本。
来自BitmapDrawable
的代码,mTargetDensity
应该是设备密度。
private void computeBitmapSize() {
mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity);
mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity);
}
和Bitmap
的相关部分,mDensity
是位图的密度
public int getScaledWidth(int targetDensity) {
return scaleFromDensity(getWidth(), mDensity, targetDensity);
}
static public int scaleFromDensity(int size, int sdensity, int tdensity) {
if (sdensity == DENSITY_NONE || sdensity == tdensity) {
return size;
}
// Scale by tdensity / sdensity, rounding up.
return ((size * tdensity) + (sdensity >> 1)) / sdensity;
}
- 缩放选项,例如
ImageView
。如果您想在ImageView
中显示一个ImageDrawable
表示(由于缩放或实际上)200x200 px 的图像,您需要在屏幕上显示该ImageDrawable
再次缩放图像。
使用 ImageView.ScaleType.CENTER_INSIDE
将以 200x200 像素绘制图像,因为 CENTER_INSIDE
仅在图像大于 View 时缩放图像。 FIT_CENTER
会将其放大到 300x300。只是 CENTER
根本无法缩放。它只是居中。
CENTER_INSIDE
+ 上面的 100x100 mdpi 图像仍会将其绘制为 200x200px,因为所有缩放因子都存储在 BitmapDrawable
/Bitmap
中。
BitmapFactory
> Bitmap
> BitmapDrawable
> ImageView
链中还有其他几个地方,如 BitmapDrawable(Bitmap bitmap)
构造函数说
This constructor was deprecated in API level 4. Use BitmapDrawable(Resources, Bitmap) to ensure that the drawable has correctly set its target density.
这可能会以不希望的方式影响图像缩放。
-> 防止所有缩放
- 不缩放解码
- 确保您的位图密度是当前设备密度或只是
Bitmap#setDensity(Bitmap.DENSITY_NONE)
- 在通过
ImageView.ScaleType.CENTER
绘制时不要应用缩放。
关于Android 和像素与设备指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18856511/