首页 > 解决方案 > 如何使来自互联网的图像响应 Android 布局?

问题描述

我的布局上有一个 Imageview,我用从网上下载的 .jpg 填充。问题是我试图让图像容器响应屏幕尺寸,因为在较大的手机中,图像看起来很小。

    public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
    }

    public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String imageUrl="https:url";

        new DownloadImageTask((ImageView) findViewById(R.id.imvAd))
                .execute(imageUrl);
    }
    }

我不想给出固定值。

任何关于寻找什么的帮助或建议都会很棒

这是布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".startActivities.LoginActivity"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:background="@color/white"
        android:orientation="vertical">

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

            <ImageView
                android:id="@+id/imvLogo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/logo"
                tools:ignore="ContentDescription" />

            <LinearLayout
                android:id="@+id/lytProgressBarLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone">

                <ProgressBar
                    android:id="@+id/pgbLogin"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:padding="10dp">

                <TextView
                    android:id="@+id/txtwelcome"
                    style="@style/styleTextview1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/welcome" />

                <EditText
                    android:id="@+id/edtPwdworker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="@string/password"
                    android:inputType="numberPassword" />

                <Button
                    android:id="@+id/btnLogin"
                    style="@style/styleButtonConsult"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:text="@string/login" />
            </LinearLayout>

            <TextView
                android:id="@+id/txtInfoRoute"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="10dp"
                android:layout_weight="90"
                android:gravity="center" />

            <TextView
                android:id="@+id/txtPinDeviceLogin"
                style="@style/styleTextviewsubTitlebold"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:gravity="center"
                android:text="@string/pinDevice" />

            <ImageView
                android:id="@+id/imvAd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="10dp"
                tools:ignore="ContentDescription" />
        </LinearLayout>

    </ScrollView>

    <TextView
        android:id="@+id/txtVersionLogin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="95"
        android:gravity="center" />

</LinearLayout>

标签: androidimageviewresponsive-images

解决方案


如果您的 ImageView 是固定的,您可以通过设置ScaleTypeCENTER_CROP或其他相关比例类型来缩放图像,以使图像按您的ImageView方式缩放。

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop" />

也不要使用wrap_content,您可以使用它match_parent来适应布局。或使用0dp( in ConstraintLayout) 并将 ImageView 约束在其他两个视图之间。

但是如果您希望 ImageView 的大小相对于屏幕的大小发生变化,您可以使用 aConstraintlayout作为父级并layout_constraintDimensionRatio像这样使用。

<androidx.constraintlayout.widget.ConstraintLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1" />
</androidx.constraintlayout.widget.ConstraintLayout>

除此之外,我建议使用图像加载器库,例如Glide。它比自己加载图像更有效,您也可以直接从那里设置比例类型。


推荐阅读