0

请看这段代码片段:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingBottom="35dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp" >

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingRight="5dp"
            android:scaleType="fitXY"
            android:src="@drawable/1" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:scaleType="fitXY"
            android:src="@drawable/2" />

        </LinearLayout>

    </LinearLayout>

我在我的 Android 应用程序中使用此代码,主要基于图像。我对正确缩放有疑问。当我使用 scaleType="fitXY" (上面的代码)时,所有尺寸的一切看起来都很好,但不是图像的高度。宽度被正确拉伸,但高度没有。我尝试使用不会拉伸图像并保持比例正常的 centerInside。但是图像的大小存在问题——它们在某些设备上太小了。我的问题是 - 如何使基于图像的应用程序在所有设备上看起来都一样(或非常接近)?

样品:

http://i.stack.imgur.com/cjS5f.jpg 宽度和高度还可以,但是边距搞砸了,fitCenter http://i.stack.imgur.com/4BUTr.jpg 宽度还可以 - 但高度不行,适合XY

4

3 回答 3

0

如果要保留原始图像的高/宽比,可以在运行时进行。由于您想要您的 ImageButton android:layout_width="fill_parent" android:layout_height="wrap_content",您可以调整以下代码:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;  // just compute size, don't create bitmap
BitmapFactory.decodeStream(stream, null, options); // stream is the InputStream representing the image you want to display
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
imageViewWidth = imageView.getWidth(); // imageView is the View container for the image you want to display
imageViewHeight = imageView.getHeight();
// Compute the sample size
double sampleSize = computePowerOfTwoInSampleSize(options, imageViewWidth, imageViewHeight);
if(sampleSize<1) { 
    options.inSampleSize = 1; 
}
else { 
    options.inSampleSize = (int) sampleSize; 
}
options.inJustDecodeBounds = false; // compute size and create bitmap  
bitmap = BitmapFactory.decodeStream(stream, null, options);
imageView.setImageBitmap(bitmap);
try { 
    stream.close(); 
} 
catch (IOException e) { 
    e.printStackTrace(); 
}

其中方法computePowerOfTwoInSampleSize是这样的(考虑到调整到 2 的幂比其他值更有效):

private double computePowerOfTwoInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    int height = options.outHeight;
    int width = options.outWidth;
    double inSampleSize = 1;        
    // compute the up-sample coefficient (power of 2)
    if(height < reqHeight && width < reqWidth) {
        while(height < reqHeight && width < reqWidth) {
            height = height*2;
            width = width*2;
            if(height < reqHeight && width < reqWidth) {
                inSampleSize = inSampleSize/2;
            }
        }
    }
    // compute the down-sample coefficient
    else {
        while(height > reqHeight || width > reqWidth) { 
            // Calculate ratios of height and width to requested height and width (power of 2)
            height = Math.round((float)height/2);
            width = Math.round((float)width/2); 
            inSampleSize = inSampleSize*2;
        }
    }
    return inSampleSize;
}
于 2014-06-03T13:28:09.820 回答
0

您应该将它的 scaleType 设置为 Matrix 并将其缩放到最近的一侧(顶部和底部或左右)保持纵横比。

于 2014-06-04T14:51:49.163 回答
0

尝试更换

android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="fitXY"

经过

android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scaleType="centerInside"

由于您似乎正在尝试水平填充屏幕,这将尽可能多地在屏幕上拉伸图像而不会扭曲它们

如果您有一种可以裁剪的背景并且只想水平放置,请尝试

android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scaleType="center"
于 2014-06-03T12:24:06.733 回答