繁体   English   中英

如何使用 Glide v4.0.0RC1 从 Url 将图像加载到 ImageView

[英]How to load Image into ImageView from Url using Glide v4.0.0RC1

我刚刚在我的应用程序中将 Glide 库从 v3 更新到 v4。 但是现在我无法从 url 加载图像。 以前它在 v3 上运行良好。

这是我的 Glide 代码:

Glide.with(context).load(galleryList.get(itemPosition).getImage()).thumbnail(Glide.with(context).load(R.drawable.balls)).apply(options).into(holder.kolamImage);

v4有什么变化? 我浏览了文档,但仍然没有帮助。

如果您使用的是Glide v4.0.0-RC1,那么您需要使用RequestOptions来添加占位符、错误图像和其他选项。 这是一个工作示例

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher_round)
                    .error(R.mipmap.ic_launcher_round);



 Glide.with(this).load(image_url).apply(options).into(imageView);
Glide.with(this)
        .load("url here") // image url
        .placeholder(R.drawable.placeholder) // any placeholder to load at start
        .error(R.drawable.imagenotfound)  // any image in case of error
        .override(200, 200) // resizing
        .centerCrop()     
        .into(imageView);  // imageview object

Glide v4 增加了RequestOptions的特性来添加占位符、错误图像和自定义图像。

RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.your_placeholder_image)
                    .error(R.drawable.your_error_image);

Glide.with(this).load(image_url).apply(options).into(imageView);

以下步骤用于从 URL 将图像加载到 imageView 中:-

创建一个像这样的新活动并从给定的 url 加载图像。

活动_main.xml

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

    <ImageView
        android:id="@+id/myOfferImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" />

</LinearLayout>

主活动.java

public class MainActivity extends AppCompatActivity {


    ImageView myOfferImageView;
    String url = "";

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


        url = "https://image.url"


        myOfferImageView = findViewById(R.id.myOfferImage);
        Glide.with(this).load(url)
                .placeholder(R.drawable.ic_launcher_background)
                .error(R.drawable.ic_launcher_background)
                .into(myOfferImageView);
    }


}

确保您的 url 周围有引号,ivProfileImage 是您的图像视图。

Glide.with(mContext)
    .asBitmap()
    .load("https://i2.wp.com/www.siasat.com/wp-content/uploads/2018/03/Rosamund-Pike.jpeg?fit=600%2C421&ssl=1")
    .into(ivProfileImage);

暂无
暂无

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

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