使用 Glide 默认加载 ImageView 中的图像
Load Images In ImageView ByDefault With Glide
我的问题是关于使用 Glide 将图像加载到图像视图中。我知道如何使用 Glide 加载图像。但我想创建一个自定义 ImageView,以便它可以默认使用 Glide 加载图像(以使其更快并默认消除内存问题)。可能吗。如果是那么如何?提前谢谢你。
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
///How to Over Ride Back Ground Attribute Sent by XML / Sent By Java Initialization
Glide.with(context).load(R.drawable.chains).into(this);
}
更新:
非常简单,我想在我的整个项目中使用我的自定义 ImageView 而不是普通的。这样它将默认解决内存问题。在 CustomImageView 中,我想使用 Glide 加载图像。我看起来像
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
///How to Over Ride Back Ground Attribute Sent by XML / Sent By Java Initialization
@Override
public void setBackground(Drawable background) {
super.setBackground(background);
Glide.with(this.getContext()).load(background).into(this);
}
}
如何管理 CustomImageView 使其工作方式与 ImageView 完全相同,但使用 Glide 加载图像。
尝试这样的事情:
public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, @Nullable AttributeSet set) {
super(context, set);
int[] attrs = {android.R.attr.background};
TypedArray typedArray = context.obtainStyledAttributes(set, attrs);
Drawable drawable = typedArray.getDrawable(0); // 0 is an index of attrs[]
if (drawable != null) {
setBackground(null);
Glide.with(context).load(typedArray.getResourceId(0, placeholder_resource_id)).into(this); // 0 is an index of attrs[]
}
typedArray.recycle();
}
}
我的问题是关于使用 Glide 将图像加载到图像视图中。我知道如何使用 Glide 加载图像。但我想创建一个自定义 ImageView,以便它可以默认使用 Glide 加载图像(以使其更快并默认消除内存问题)。可能吗。如果是那么如何?提前谢谢你。
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
///How to Over Ride Back Ground Attribute Sent by XML / Sent By Java Initialization
Glide.with(context).load(R.drawable.chains).into(this);
}
更新: 非常简单,我想在我的整个项目中使用我的自定义 ImageView 而不是普通的。这样它将默认解决内存问题。在 CustomImageView 中,我想使用 Glide 加载图像。我看起来像
public class CustomImageView extends ImageView {
public CustomImageView(Context context) {
super(context);
}
///How to Over Ride Back Ground Attribute Sent by XML / Sent By Java Initialization
@Override
public void setBackground(Drawable background) {
super.setBackground(background);
Glide.with(this.getContext()).load(background).into(this);
}
}
如何管理 CustomImageView 使其工作方式与 ImageView 完全相同,但使用 Glide 加载图像。
尝试这样的事情:
public class CustomImageView extends android.support.v7.widget.AppCompatImageView {
public CustomImageView(Context context) {
super(context);
}
public CustomImageView(Context context, @Nullable AttributeSet set) {
super(context, set);
int[] attrs = {android.R.attr.background};
TypedArray typedArray = context.obtainStyledAttributes(set, attrs);
Drawable drawable = typedArray.getDrawable(0); // 0 is an index of attrs[]
if (drawable != null) {
setBackground(null);
Glide.with(context).load(typedArray.getResourceId(0, placeholder_resource_id)).into(this); // 0 is an index of attrs[]
}
typedArray.recycle();
}
}