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.
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 thesetDensity()methods, it’s working.