将图像动态设置为自定义图像视图 class android
set image dynamically to the custom imageview class android
我想将 url 加载到 ImageView 中并且 url 是动态的,所以我创建了自定义 Imageview class 这样的
public class CustomLocalCurrencyImageViewWhiteColor extends AppCompatImageView {
public CustomLocalCurrencyImageViewWhiteColor(Context context) {
super(context);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs) {
super(context, attrs);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
}
。当我按以下方式使用它时它工作正常
<com.kiran.example.ebitcoin.customview.CustomLocalCurrencyImageViewWhiteColor
android:id="@+id/img"
android:layout_width="@dimen/imageSizeTooSmallBTCLogo"
android:layout_height="@dimen/imageSizeTooSmallBTCLogo"
android:layout_gravity="center_vertical" />
现在,我也想动态更改此自定义图像的图像资源,所以我尝试了
CustomLocalCurrencyImageViewWhiteColor img = findViewById(R.id.img);
img.setImageResource(R.drawable.ic_bitcoin_24_white);
但不幸的是,一切都没有改变。如何动态更改自定义图像视图 class 的图像?有人找到了这个问题的答案吗?
在扩展 AppCompatImageView
时,您可以执行以下操作:
Drawable drawable = AppCompatDrawableManager.get().getDrawable(getApplicationContext(), R.drawable.ic_bitcoin_24_white);
img.setImageResource(drawable);
好吧,你已经开始使用 Glide,所以尝试这样做:
在 CustomLocalCurrencyImageViewWhiteColor 中创建 setImageWithGlide 方法
public void setImageWithGlide(Context context, String imagePath) {
Glide.with(context).load(imagePath).into(this)
}
public void setImageWithGlide(Context context, Integer resId) {
Glide.with(context).load(resId).into(this)
}
你可以这样使用它:
img.setImageWithGlide(imagePath);
或者如果你使用 resourceId
img.setImageWithGlide(R.drawable.image);
终于,我得到了我自己的问题的答案。我按照this,按照下面的方式完成了代码。
public class CustomLocalCurrencyImageViewWhiteColor extends AppCompatImageView {
Context context;
public CustomLocalCurrencyImageViewWhiteColor(Context context) {
super(context);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs) {
super(context, attrs);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public void setImageWithGlide(Context context, Integer resId) {
this.context = context;
Glide.with(context).load("").placeholder(resId).into(this);
}
}
要动态更改图像,
CustomLocalCurrencyImageViewWhiteColor img = findViewById(R.id.img);
img.setImageWithGlide(this, R.drawable.image);
我想将 url 加载到 ImageView 中并且 url 是动态的,所以我创建了自定义 Imageview class 这样的
public class CustomLocalCurrencyImageViewWhiteColor extends AppCompatImageView {
public CustomLocalCurrencyImageViewWhiteColor(Context context) {
super(context);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs) {
super(context, attrs);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
}
。当我按以下方式使用它时它工作正常
<com.kiran.example.ebitcoin.customview.CustomLocalCurrencyImageViewWhiteColor
android:id="@+id/img"
android:layout_width="@dimen/imageSizeTooSmallBTCLogo"
android:layout_height="@dimen/imageSizeTooSmallBTCLogo"
android:layout_gravity="center_vertical" />
现在,我也想动态更改此自定义图像的图像资源,所以我尝试了
CustomLocalCurrencyImageViewWhiteColor img = findViewById(R.id.img);
img.setImageResource(R.drawable.ic_bitcoin_24_white);
但不幸的是,一切都没有改变。如何动态更改自定义图像视图 class 的图像?有人找到了这个问题的答案吗?
在扩展 AppCompatImageView
时,您可以执行以下操作:
Drawable drawable = AppCompatDrawableManager.get().getDrawable(getApplicationContext(), R.drawable.ic_bitcoin_24_white);
img.setImageResource(drawable);
好吧,你已经开始使用 Glide,所以尝试这样做:
在 CustomLocalCurrencyImageViewWhiteColor 中创建 setImageWithGlide 方法
public void setImageWithGlide(Context context, String imagePath) {
Glide.with(context).load(imagePath).into(this)
}
public void setImageWithGlide(Context context, Integer resId) {
Glide.with(context).load(resId).into(this)
}
你可以这样使用它:
img.setImageWithGlide(imagePath);
或者如果你使用 resourceId
img.setImageWithGlide(R.drawable.image);
终于,我得到了我自己的问题的答案。我按照this,按照下面的方式完成了代码。
public class CustomLocalCurrencyImageViewWhiteColor extends AppCompatImageView {
Context context;
public CustomLocalCurrencyImageViewWhiteColor(Context context) {
super(context);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs) {
super(context, attrs);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public CustomLocalCurrencyImageViewWhiteColor(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Glide.with(context).load(SessionManager.getCountryWiseDataObject(context).getCurrencyImageWhite()).into(this);
}
public void setImageWithGlide(Context context, Integer resId) {
this.context = context;
Glide.with(context).load("").placeholder(resId).into(this);
}
}
要动态更改图像,
CustomLocalCurrencyImageViewWhiteColor img = findViewById(R.id.img);
img.setImageWithGlide(this, R.drawable.image);