我创建了一个带有两个位图的自定义视图。当我在视图之外的 ImageView 中使用位图时,图像正常。我得打电话setDensity(240)
(hdpi 设备),但没关系。
但是,在自定义视图中,当我在画布上绘制相同的位图时,onDraw(Canvas)
重写的方法,它再次缩放,当我不调用时也是如此setDensity(240)
。
通过调试,我确保调用后画布和位图的密度都是240。
Code:
@Override
protected void onDraw(Canvas canvas) {
_ingredient.setDensity(240);
canvas.setDensity(240);
canvas.drawBitmap(_ingredient, new Matrix(), new Paint());
Matrix refMatrix = new Matrix();
refMatrix.postTranslate(0, _ingredient.getHeight()-35);
Paint paint = new Paint();
paint.setAlpha(60);
}
正在工作的 ImageView,在我的 View 之外。
Bitmap tertr = _cocktail.getCocktailBitmapImage();
tertr.setDensity(240);
ImageView in = new ImageView(this);
in.setScaleType(ScaleType.CENTER);
in.setImageBitmap(tertr);
RelativeLayout relativeLayout = RelativeLayout)findViewById(R.id.cocktail_date_image_layout);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
relativeLayout.addView(in, params);
第一个显示放大的图像,就像第二个一样,没有 setDensity 调用。图像以 72dpi 创建。这些图像是给定的,我无法更改它们。我是否错过了一件事,或者这就是 Canvas 的工作原理?用于测试的设备是DROID2。
我解决了这个问题。导致问题的代码片段位于清单文件中。
我们改变了<supports-screens android:anyDensity="false"/>
为真。 True 是默认值,但不幸的是,当我尝试不同的方法时,我改变了它。有了新的价值,setDensity()
方法,有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)