如何使用 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.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.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 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 周围有引号,其中 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);
以下步骤用于将图像从 URL 加载到 imageView 中:-
create a new Activity like this and load image from given 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://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);
}
}
我刚刚在我的应用程序中将 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.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.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 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 周围有引号,其中 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);
以下步骤用于将图像从 URL 加载到 imageView 中:-
create a new Activity like this and load image from given 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://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);
}
}