ImageView:填充水平维持宽高比

时间:2012-03-08 19:07:27

标签: android layout android-layout imageview

我需要ImageView缩放图像,直到水平填充父级。

如果图像视图背景为红色,其余内容(图像下方)为绿色,我正在寻找图片1 显示的结果。如果图像宽度高于屏幕宽度,则自动获得该结果。

但如果是图片2中的小图片,我可以得到的最好结果是图片3 (将ImageView宽度和高度设置为 fill_parent 和scaleType为 FitStart

获得图片4设置height = wrap_content,width = fill_parent,scaleType = CenterCrop。它应该垂直缩放以显示整个图像,但是当scaleType表示它会裁剪它。

即使图像很小,任何获取图片1的想法?

将为工作答案授予50的赏金

enter image description here

3 个答案:

答案 0 :(得分:3)

我找到了一个对我有用的简单解决方案。根据需要设置宽度和高度,例如:

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

然后你也设置了这个设置:

android:adjustViewBounds="true"

" adjustViewBounds"默认为false(我觉得很奇怪),但设置它可以很容易地在imageview中填充图像。

答案 1 :(得分:1)

没有自定义类,它是可行的。这是我用小图片获得的结果: enter image description here

大图: enter image description here

您必须使用RelativeLayout才能使其正常工作,但您可以将其与LinearLayout结合使用,以实现您所需的一切。这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:id="@+id/button_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            >
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:text="button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <ImageView 
            android:src="@drawable/cat"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:layout_above="@id/button_container"/>
    </RelativeLayout>
</ScrollView>

诀窍是你将它设置为填满屏幕,但它必须高于其他布局。这样您就可以实现所需的一切。

答案 2 :(得分:0)

试试这个:

import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class CustomImageView extends android.widget.ImageView {

    public CustomImageView(Context context) {
        super(context);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void fitYUniformly() {
        final Drawable drawable = getDrawable();
        if (drawable == null) return;

        final int dwidth = drawable.getIntrinsicWidth();
        final int dheight = drawable.getIntrinsicHeight();
        if (dwidth == -1 || dheight == -1) return;

        int vheight = this.getHeight();
        float scale = (float) vheight / (float) dheight;

        final int vwidth = (int) (dwidth * scale);
        scale(scale, vwidth, vheight);
    }

    public void fitXUniformly(int parentWidth) {
        final Drawable drawable = getDrawable();
        if (drawable == null) return;

        final int dwidth = drawable.getIntrinsicWidth();
        final int dheight = drawable.getIntrinsicHeight();
        if (dwidth == -1 || dheight == -1) return;

        int vwidth = parentWidth;// here,you need to pass the width of parentview
    //  int vwidth = this.getWidth();           
        float scale = (float) vwidth / (float) dwidth;

        final int vheight = (int) (dheight * scale);
        scale(scale, vwidth, vheight);
    }

    private void scale(float scale, int newWidth, int newHeight) {
        final ViewGroup.LayoutParams params = this.getLayoutParams();
        params.width = newWidth;
        params.height = newHeight;
        this.setLayoutParams(params);
        this.setScaleType(ScaleType.MATRIX);
        final Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        this.setImageMatrix(matrix);
    }

}

注意:

不要忘记致电fitXUniformly(parentWidth);。在这里,parentWidth将是CustomImageView父级的宽度。

我希望它会有所帮助!!