Issue
Programming android using Xamarin, i have a couple of images in 32x32 pixels dimensions that i need to display on canvas
. I have been testing the code using Android SDK Emulator with an xhdpi
screen and the images seem to automatically resize to double their dimensions. i.e. they become 64 pixels wide by 64 pixels tall. I then created another AVD with mdpi
screen and the images seem to be the correct size.
I have read about density independent pixels on the android documentation and other websites, but can't seem to understand why the image dimensions would resize automatically. Is there an implicit resizing taking place behind the scenes? If so, why would the android developers not mention it in their documentation? Is there anything else I need to set up? Below is a relevant part of the code (my images being resized are jp1, jp2, etc):
Paint bkgBmpPaint = new Paint(); Color myColor = new Color();
Paint myTextPaint = new Paint();
private int a = 0, b = 0, bw = 0;
private Bitmap partialBitmap = null;
DisplayMetrics dm = new DisplayMetrics();
private Bitmap jp1,jp2,jp3,jp4;
public MyCanvasPath(Context context) : base(context) //constructor
{
partialBitmap = Bitmap.CreateBitmap(Resources.Configuration.ScreenWidthDp, Resources.Configuration.ScreenHeightDp,Bitmap.Config.Argb8888);
float scale = Resources.DisplayMetrics.Density;
jp1 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.jp1);
jp2 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.jp2);
jp3 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.jp3);
jp4 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.jp4);
Canvas myCanvas = new Canvas(partialBitmap); b = myCanvas.Height; a = myCanvas.Width; bw = jp1.Width;
IList<Bitmap> pImgList = new List<Bitmap> {
jp1, jp2, jp3, jp4 };
imgCount = pImgList.Count;
for (int x = 0; x < imgCount; x++)
{
myCanvas.DrawBitmap(pBmpList.ElementAt(x), bw * x, 0, null);
}}
protected override void OnDraw(Canvas screenCanvas)
{
screenCanvas.DrawBitmap(partialBitmap, 0, 0, null);
partialBitmap.Recycle();
}
Solution
I have read about density independent pixels on the android documentation and other websites, but can't seem to understand why the image dimensions would resize automatically.
The problem lies in [BitmapFactory.DecodeResource(Resources res,int id)](https://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources, int)). This method is density dependent. By default, it will use the density of your device/emulator. You are testing it with two emulators with different densities. So this method creates bitmap with different dimensions.
Solution:
To avoid the problem, you should use other version of this method: [decodeResource(Resources res, int id, BitmapFactory.Options opts)](https://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources, int, android.graphics.BitmapFactory.Options)) with opts.Indensity
set to a fixed value, for example:
var option = new BitmapFactory.Options();
option.InDensity = 320;
jp1 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon,option);
jp2 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon,option);
jp3 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon,option);
jp4 = BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon,option);
Answered By - Elvis Xia - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.