简体   繁体   English

URL 使用 Glide 时图像未显示在 Android Studio 中

[英]URL image not showing up in Android Studio when using Glide

My activity is made up of 12 ImageViews.我的活动由 12 个 ImageView 组成。 When the user clicks on one of the grid, I want to replace the default rectangular X image with an URL image.当用户单击其中一个网格时,我想用 URL 图像替换默认的矩形 X 图像。 I have been trying to use Glide to set the image.我一直在尝试使用 Glide 来设置图像。 However, I kept having issues with the alignment of the pictures.但是,我一直对图片的 alignment 有疑问。

Before setting URL image using glide在使用 glide 设置 URL 图像之前

在此处输入图像描述

After setting URL image using glide使用滑行设置 URL 图像后

在此处输入图像描述

This is my code in the corresponding layout page.这是我在相应布局页面中的代码。

    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/gameScoreDynamic"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="@+id/gameScoreDynamic"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:src="@drawable/recx"></ImageView>

            <ImageView
                android:id="@+id/image2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:src="@drawable/recx"></ImageView>

            <ImageView
                android:id="@+id/image3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:src="@drawable/recx"></ImageView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="@+id/gameScoreDynamic"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/image4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

            <ImageView
                android:id="@+id/image5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

            <ImageView
                android:id="@+id/image6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="@+id/gameScoreDynamic"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/image7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

            <ImageView
                android:id="@+id/image8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

            <ImageView
                android:id="@+id/image9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="@+id/gameScoreDynamic"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/image10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

            <ImageView
                android:id="@+id/image11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

            <ImageView
                android:id="@+id/image12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/recx"
                android:scaleType="centerInside"
                android:adjustViewBounds="true"
                android:layout_weight="1">
            </ImageView>

        </LinearLayout>

    </LinearLayout>

This is my code in the activity page.这是我在活动页面中的代码。

public class GamePage extends AppCompatActivity implements View.OnClickListener {

....
....
....

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_page);

    ImageView img1 = findViewById(R.id.image1);
    img1.setOnClickListener(this);
    gridIDs.add(img1.getId());
    Glide.with(this)
            .load("https://cdn.stocksnap.io/img-thumbs/280h/tree-desert_JMTQNOJWU8.jpg")
            .into(img1);
}

I would like to replace the 12 rectangular boxes with X with URL images.我想用 URL 图像替换 X 的 12 个矩形框。 Have tried to fiddle with the ImageView layout settings but to no avail.试图摆弄 ImageView 布局设置但无济于事。 Please help!请帮忙!

The problem is that you are using wrap_content for the height and width of the ImageViews .问题是您正在使用wrap_content作为ImageViews的高度和宽度。 When Glide loads the new image, the view expands to accommodate the size of the new image.当 Glide 加载新图像时,视图会扩展以适应新图像的大小。

There is likely to be a way to specify different behavior with Glide, but another approach would be to set the size of the ImageViews programmatically to a fixed size based upon the intrinsic size of "drawable/recx".可能有一种方法可以指定 Glide 的不同行为,但另一种方法是根据“drawable/recx”的固有大小,以编程方式将ImageViews的大小设置为固定大小。 In the activity's onCreate() :在活动的onCreate()中:

ImageView iv = findViewById(R.id.image7);
Drawable d = iv.getDrawable();
ViewGroup.LayoutParams lp = iv.getLayoutParams();
lp.width = d.getIntrinsicWidth();
lp.height = d.getIntrinsicHeight();

You could also just set the size in the XML.您也可以只在 XML 中设置大小。

Your XML is a mixture of different layout attributes, so it looks like you have tried several things in XML. Another thought is to use 0dp along with weights or other means to adjust the size of the ImageViews rather than use wrap_content .您的 XML 是不同布局属性的混合体,因此看起来您在 XML 中尝试了几种方法。另一个想法是使用0dp以及权重或其他方法来调整ImageViews的大小,而不是使用wrap_content This may be a better way since it would not need any additional code.这可能是更好的方法,因为它不需要任何额外的代码。 (It it works, that is. You will have to try it out.) (它有效,也就是说。你必须尝试一下。)

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

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