4

我无法弄清楚如何将位图用作背景图像,使其缩放以填充屏幕但保持纵横比。到目前为止,我只找到了使用附加 ImageView 而不是背景图像并使用 android:scaleType="centerCrop" 的解决方案

...
<ImageView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/desc_background"
    android:scaleType="centerCrop"
    android:src="@drawable/background"
/>
...

不过我更愿意通过在窗口上设置活动背景来解决这个问题

 <style ...>
 <item name="android:windowBackground">@drawable/background</item>

正如我被告知(并看到)这要快得多。(@drawable/background 是一个 XML 可绘制对象)

似乎是一项常见的任务,但到目前为止我找不到解决方案。任何建议将不胜感激。

谢谢

马丁

4

2 回答 2

0

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

Bitmap scaleDownLargeImageWithAspectRatio(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;
        }
于 2014-03-25T09:54:36.077 回答
0

您是否尝试将 adjustViewBounds 属性设置为 true ?

http://developer.android.com/reference/android/widget/ImageView.html#attr_android:adjustViewBounds

于 2012-08-31T03:07:41.173 回答