图片自动调整大小 [英] Images automatically resized

查看:104
本文介绍了图片自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xamarin进行android编程,我有几张 32x32 像素尺寸的图像,需要在 canvas 上显示.我一直在使用带有 xhdpi 屏幕的Android SDK仿真器测试代码,图像似乎会自动调整大小以使其尺寸加倍.即它们变为 64像素宽x 64像素高.然后,我用 mdpi 屏幕创建了另一个AVD,图像似乎是正确的大小.

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.

我已经在 android文档和其他文档中了解了与密度无关的像素网站,但似乎无法理解为什么图片尺寸会自动调整大小.幕后是否有隐式调整大小?如果是这样,为什么Android开发人员不会在其文档中提及它?我还需要设置什么吗?以下是代码的相关部分(正在调整大小的图像是 jp1 jp2 等):

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();
        }

推荐答案

我已经在android文档和其他网站上阅读了有关密度独立像素的信息,但是似乎无法理解为什么图像尺寸会自动调整大小.

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.

问题出在[BitmapFactory.DecodeResource(Resources res,int id)](

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.

解决方案:为了避免出现此问题,您应该使用此方法的其他版本:[decodeResource(资源res,int id,BitmapFactory.Options opts)](

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);

这篇关于图片自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆