繁体   English   中英

如何将图像的宽度设置为 imageView android

[英]How can I set the width of the image to imageView android

我怎样才能完成

根据图像,我如何将图像设置为 imageview 并且背景颜色为灰色。

这是我试过的代码

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:layout_gravity="center"
        android:background="@color/colorAshLight"
        android:padding="11dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            android:src="@drawable/dunkincoffee"
            />
    </LinearLayout>

但我得到的结果是:[![在此处输入图片描述][3]][3]

[![在此处输入图片描述][4]][4]

请试试这个。 您需要将android:scaleType="centerCrop"设置为 ImageView

更新您的代码如下。

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:layout_gravity="center"
            android:background="@color/colorAshLight"
            android:padding="11dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            android:src="@drawable/dunkincoffee"
            />

</LinearLayout>

使用这个 class,这将自动调整 imageView 的边界及其图像宽度:

1.在你的项目中添加这个class:

package com.example.myapplication;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ManageHeightByWidthImageView extends ImageView {

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

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable d = getDrawable();
        if (d != null) {

            int w = MeasureSpec.getSize(widthMeasureSpec);
            int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();

            try {
                setMeasuredDimension(w, h);
            } catch (NullPointerException ignored) {
            }
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

}
 

2. 像下面这样使用第一张图片:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:background="@color/colorAshLight"
        android:padding="10dp">

        <!--
                        Set the width value you want in place of "match_parent", for this linearLayout
        -->
        <com.example.myapplication.ManageHeightByWidthImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/dunkincoffee" />
    </LinearLayout>

3.对于第二个垂直图像:

        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@color/colorAshLight"
            android:padding="10dp">
            <com.example.myapplication.ManageHeightByWidthImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@drawable/vertical_image" />
        </LinearLayout>

它看起来像这样:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM