0

我看到了很多关于同一主题的问题,并尝试使用

android:adjustViewBounds="true"
android:scaleType="fitCenter"

我的图像仍然按原样显示。如果我改变

android:scaleType="fitCenter"

工作 fitXY正常。但根据文件,这个不保持纵横比。那么我怎样才能更改代码以按预期工作?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/card"
        android:layout_marginTop="25dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/card" >

        <ImageView
            android:id="@+id/idImage"
            android:layout_width="fill_parent"
            android:layout_height="110dp"
            android:layout_margin="10dp"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/783454" />

    </LinearLayout>

</LinearLayout>

![在此处输入图像描述][1] ![在此处输入图像描述][2] 我在哪里缺少这个概念?

4

1 回答 1

0

请看这段代码,这就是我如何缩放位图以正确适应屏幕。也许它会有所帮助,并为您提供有关您的任务的想法。

private void loadImage() {
    ImageView imageView = (ImageView)findViewById(R.id.imageView);

    Bitmap imageBitmap = ... load original image bitmap;  

    Bitmap scaledBitmap = imageBitmap; 

    // Scaling

    int imgSrcHeight = imageBitmap.getHeight();
    int imgSrcWidth = imageBitmap.getWidth();

    int scaledHeight = 0;
    int scaledWidth = 0;

    int ctnrHeight = imageView.getMeasuredHeight();
    int ctnrWidth = imageView.getMeasuredWidth();

    int mHeight = imgSrcHeight - ctnrHeight;
    int mWidth = imgSrcWidth - ctnrWidth;

    if(mHeight > 0 && mWidth > 0)
    {
        if(mHeight > mWidth)
        {
            // scale to fit height
            if(mHeight > 0)
            {
                scaledHeight = ctnrHeight;

                // if height < 0 it means it's already inside of content
                int coefOverhight = (ctnrHeight * 100)/imgSrcHeight;
                scaledWidth = (int)(imgSrcWidth * ((coefOverhight)/100.0)); 
            }
        }
        else
        {
            // scale to fit width
            if(mWidth > 0)
            {
                scaledWidth = ctnrWidth;
                int coefOverwidth = (ctnrWidth * 100)/imgSrcWidth;
                scaledHeight = (int)(imgSrcHeight * ((coefOverwidth)/100.0));
            }
        }
    }
    else
    {
        scaledHeight = imgSrcHeight;
        scaledWidth = imgSrcWidth;
    }

    scaledBitmap = Bitmap.createScaledBitmap(imageBitmap, scaledWidth, scaledHeight, true);


    imageView.setImageBitmap(scaledBitmap);

}

于 2012-07-30T16:32:02.883 回答