Reputation: 609
I created a custom View, with two Bitmaps. When I use the Bitmap outside of my View, in an ImageView, the image is OK. I have to call setDensity(240)
(hdpi device), but it's ok.
However, in the custom View, when I draw the same Bitmap on a canvas, in the onDraw(Canvas)
overridden method, it's scaled again, the same way when I don't call setDensity(240)
.
Via debugging, I ensured that the density of the canvas and bitmap is 240, after the call.
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);
}
The working ImageView, outside of my 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);
The first one is showing the image scaled up, just like the second one, without the setDensity call. The images were created at 72dpi. These images are given, I cannot change them. Am I missing one thing, or this is how Canvas works? The device used for testing is a DROID2.
Upvotes: 3
Views: 7293
Reputation: 609
I solved the problem. The code snippet that caused the trouble was in the manifest file.
We changed <supports-screens android:anyDensity="false"/>
to true. True is the default, but unfortunately I changed that while I was trying different approaches. With the new value, and the setDensity()
methods, it's working.
Upvotes: 3
Reputation: 22512
Bitmap density isn't a screen density it will be drawn on but an "original" density of bitmap. This means in case bitmap and screen densities match, bitmap wouldn't be scaled. You should set bitmap density to 160 if you want image to be same size in dips as it is in pixels. Hope this will help.
Also I think you shouldn't modify canvas density.
Upvotes: 1