【问题标题】:Android image scaling to support multiple resolutionsAndroid图像缩放以支持多种分辨率
【发布时间】:2012-09-12 06:17:26
【问题描述】:

我已将我的游戏编码为 320x480,我认为支持多种分辨率的最简单方法是缩放最终图像。您对此有何看法?这样做会不会有 cpu 效率?

我将所有图像都放在 mdpi 文件夹中,我会将其在屏幕上未缩放地绘制到缓冲区上,然后将其缩放以适应屏幕。所有用户输入也将被缩放。

我有这两个问题: - 如何在没有android自动缩放的情况下绘制位图 -如何缩放位图?

【问题讨论】:

    标签: android image scaling


    【解决方案1】:

    我在我的项目中多次偶然发现同样的问题,每次由于缺乏时间(和懒惰),我都会对不太理想的解决方案感到满意。但最近我找到了一些时间来解决这个特殊问题。这是我的解决方案,希望对您也有帮助。

    Bitmap scaleWithAspectRatio(Bitmap image)
            {
                int imaheVerticalAspectRatio,imageHorizontalAspectRatio;
                float bestFitScalingFactor=0;
                float percesionValue=(float) 0.2;
    
                //getAspect Ratio of Image
                int imageHeight=(int) (Math.ceil((double) image.getHeight()/100)*100);
                int imageWidth=(int) (Math.ceil((double) image.getWidth()/100)*100);
                int GCD=BigInteger.valueOf(imageHeight).gcd(BigInteger.valueOf(imageWidth)).intValue();
                imaheVerticalAspectRatio=imageHeight/GCD;
                imageHorizontalAspectRatio=imageWidth/GCD;
                Log.i("scaleDownLargeImageWIthAspectRatio","Image Dimensions(W:H): "+imageWidth+":"+imageHeight);
                Log.i("scaleDownLargeImageWIthAspectRatio","Image AspectRatio(W:H): "+imageHorizontalAspectRatio+":"+imaheVerticalAspectRatio);
    
                //getContainer Dimensions
                int displayWidth = getWindowManager().getDefaultDisplay().getWidth();
                int displayHeight = getWindowManager().getDefaultDisplay().getHeight();
               //I wanted to show the image to fit the entire device, as a best case. So my ccontainer dimensions were displayWidth & displayHeight. For your case, you will need to fetch container dimensions at run time or you can pass static values to these two parameters 
    
                int leftMargin = 0;
                int rightMargin = 0;
                int topMargin = 0;
                int bottomMargin = 0;
                int containerWidth = displayWidth - (leftMargin + rightMargin);
                int containerHeight = displayHeight - (topMargin + bottomMargin);
                Log.i("scaleDownLargeImageWIthAspectRatio","Container dimensions(W:H): "+containerWidth+":"+containerHeight);
    
                //iterate to get bestFitScaleFactor per constraints
                while((imageHorizontalAspectRatio*bestFitScalingFactor <= containerWidth) && 
                        (imaheVerticalAspectRatio*bestFitScalingFactor<= containerHeight))
                {
                    bestFitScalingFactor+=percesionValue;
                }
    
                //return bestFit bitmap
                int bestFitHeight=(int) (imaheVerticalAspectRatio*bestFitScalingFactor);
                int bestFitWidth=(int) (imageHorizontalAspectRatio*bestFitScalingFactor);
                Log.i("scaleDownLargeImageWIthAspectRatio","bestFitScalingFactor: "+bestFitScalingFactor);
                Log.i("scaleDownLargeImageWIthAspectRatio","bestFitOutPutDimesions(W:H): "+bestFitWidth+":"+bestFitHeight);
                image=Bitmap.createScaledBitmap(image, bestFitWidth,bestFitHeight, true);
    
                //Position the bitmap centre of the container
                int leftPadding=(containerWidth-image.getWidth())/2;
                int topPadding=(containerHeight-image.getHeight())/2;
                Bitmap backDrop=Bitmap.createBitmap(containerWidth, containerHeight, Bitmap.Config.RGB_565);
                Canvas can = new Canvas(backDrop);
                can.drawBitmap(image, leftPadding, topPadding, null);
    
                return backDrop;
            }
    

    【讨论】:

    • 我认为您的(int) (Math.ceil((double) image.getHeight()/100)*100); 行中有错误。你在这里的方式总是会四舍五入到下一个因子 100,这将导致许多图像无法保持正确的纵横比。
    • 你实现并运行了这篇文章吗?它给你带来了什么结果?这次总结背后有一个原因,那就是获得一个 GCD。考虑将 1542 和 1477 的 GCD 转换为 1500 和 1400 的 GCD。
    • 当我按原样尝试此代码时,它倾向于创建方形图像而不是保持适当的纵横比。更改这些行后,它似乎解决了问题。一般来说,它不太适合我的特殊需求,所以我不再使用它,但这是我尝试时的经验。
    • @Dave 你能分享一下你现在用什么来缩放你的图像吗?可能是您在此处分享的参考资料,可能会对我和其他一些人有所帮助。
    【解决方案2】:

    我正在开发一款游戏,我的目标是 1280x800,并且我制作了所有图像以匹配该分辨率。第一次启动游戏时,我会缩小所有图像以匹配当前分辨率,并使用缩放后的图像来实际绘制游戏。如果您使用的是 SurfaceView,那么您在绘制图像时应该不会遇到缩放图像的问题。只是为了 100% 确保我将图像保存在原始目录中并从那里加载它们。

    要对位图进行初始缩放,您只需在解码位图时使用BitmapFactory.Options 来告诉它它的目标密度以及实际显示的密度。

    【讨论】:

    • 我以前从未想过播放原始文件夹中的图像。这可能会派上用场。
    猜你喜欢
    • 1970-01-01
    • 2012-08-24
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多