首页 文章

ImageView:填充水平维持宽高比

提问于
浏览
7

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

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

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

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

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

将奖励50分给工作答案

enter image description here

3 回答

  • 0

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

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

    然后你还设置这个设置:

    android:adjustViewBounds="true"
    

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

  • 3

    它以小图片获得了's doable without custom classes. This is the result I':
    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>
    

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

  • 1

    试试这个:

    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);
        }
    
    }
    

    Note:

    别忘了拨打 fitXUniformly(parentWidth); . 在这里, parentWidth 将是 CustomImageView 的父级的宽度 .

    我希望它会有所帮助!!

相关问题