我创建了一个具有两个位图的自定义视图。当我在视图外使用位图时,在ImageView中,图像正常。我必须致电setDensity(240)
(hdpi设备),但没关系。 但是,在自定义视图中,当我在画布上绘制相同的位图时,在onDraw(Canvas)
重写的方法中,它会再次缩放,当我不调用setDensity(240)
时,将以同样的方式缩放。 通过调试,我确保在调用之后画布和位图的密度是240。在画布上绘制位图得到缩放
代码:
@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的,我的视野之外。
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。
请问您能解释一下160的值是怎么来的?正如您在代码中看到的那样,我将Bitmaps密度设置为与屏幕密度相匹配。 – gmate 2011-02-16 14:26:16
160dpi是默认的Android密度。如果您将位图密度设置为160并且屏幕密度为240,则位图将被放大。如果将位图密度设置为240,则不会在240dpi设备上缩放,但会在160dpi设备上缩小比例。 – 2011-02-16 14:40:01