我刚刚在我的应用程序中将Glide库从v3更新到v4。 但是现在我无法从网址加载图片。以前它与v3一起工作正常。
这是我的Glide代码:
Glide.with(context).load(galleryList.get(itemPosition).getImage()).thumbnail(Glide.with(context).load(R.drawable.balls)).apply(options).into(holder.kolamImage);
v4有什么变化?我浏览了文档,但仍然没有帮助。
答案 0 :(得分:13)
如果您使用 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);
答案 1 :(得分:2)
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
答案 2 :(得分:2)
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);
答案 3 :(得分:1)
以下步骤用于将图像从URL加载到imageView中:-
像这样创建一个新的Activity并从给定的URL加载图像。
activity_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>
MainActivity.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://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRbk0YDyfWSiZJNMehf3KvJrbLPvVyUTyM2nt0TGwCGjJB06-WDNg"
myOfferImageView = findViewById(R.id.myOfferImage);
Glide.with(this).load(url)
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background)
.into(myOfferImageView);
}
}
答案 4 :(得分:0)
确保您的网址周围带有引号,并且ivProfileImage是您的imageview。
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);