Answers:
试试这个,
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
这将返回您要访问的可绘制对象的ID ...然后您可以通过执行以下操作在imageview中设置图像
imageview.setImageResource(id);
Drawable image = ImageOperations(context,ed.toString(),"image.jpg");
ImageView imgView = new ImageView(context);
imgView = (ImageView)findViewById(R.id.image1);
imgView.setImageDrawable(image);
要么
setImageDrawable(getResources().getDrawable(R.drawable.icon));
我个人更喜欢使用这种方法setImageResource()
。
ImageView myImageView = (ImageView)findViewById(R.id.myImage);
myImageView.setImageResource(R.drawable.icon);
资源可绘制名称不会存储为字符串,因此您必须将字符串解析为在生成过程中生成的整数常量。您可以使用Resources
该类将字符串解析为该整数。
Resources res = getResources();
int resourceId = res.getIdentifier(
generatedString, "drawable", getPackageName() );
imageView.setImageResource( resourceId );
这会将您生成的字符串解析为整数,ImageView
可用于加载正确的图像。
或者,您可以使用ID Drawable
手动加载,然后使用该可绘制对象而不是资源ID设置图像。
Drawable drawable = res.getDrawable( resourceId );
imageView.setImageDrawable( drawable );
这至少在Android API 15中有效
ImageView = imgv;
Resources res = getResources(); // need this to fetch the drawable
Drawable draw = res.getDrawable( R.drawable.image_name_in_drawable );
imgv.setImageDrawable(draw);
您可以使用setImageResource(),但是文档指定 “在UI线程上进行位图读取和解码,这可能会导致延迟打...……请考虑使用setImageDrawable()或setImageBitmap()”。如切托所说
如果您不能在不是Activity的类中获得像这样的Resources对象,则必须为getResources()添加getContext()方法。
ImageView image = (ImageView) v.findViewById(R.id.item_image);
int id = getContext().getResources().getIdentifier(imageName, "drawable", getContext().getPackageName());
image.setImageResource(id);
构造一个POJO.java类,并创建“构造函数,getter和setter方法”
class POJO{
public POJO(Drawable proImagePath) {
setProductImagePath(proImagePath);
}
public Drawable getProductImagePath() {
return productImagePath;
}
public void setProductImagePath(Drawable productImagePath) {
this.productImagePath = productImagePath;
}
}
然后通过可绘制图像的资源将适配器设置到CustomAdapter.java
class CustomAdapter extends ArrayAdapter<POJO>{
private ArrayList<POJO> cartList = new ArrayList<POJO>();
public MyCartAdapter(Context context, int resource) {
super(context, resource);
}
public MyCartAdapter(Context context, ArrayList<POJO> cartList) {
super(context, 0, cartList);
this.context = context;
this.cartList = cartList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
*Here you can setup your layout and references.
**/
ImageView productImage = (ImageView) rootView.findViewById(R.id.cart_pro_image);
productImage.setImageDrawable(POJO.getProductImagePath());
}
}
然后通过ActivityClass.java传递引用
public class MyCartActivity extends AppCompatActivity{
POJO pojo;
CustomAdapter customAdapter;
ArrayList<POJO> cartList = new ArrayList<POJO>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
customAdapter = new CustomAdapter(this, cartList);
pojo = new POJO(getResources().getDrawable(R.drawable.help_green));
}
}
我的项目的一部分,一切正常!)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final ModelSystemTraining modelSystemTraining = items.get(position);
int icon = context.getResources().getIdentifier(String.valueOf(modelSystemTraining.getItemIcon()), "drawable", context.getPackageName());
final FragmentViewHolderSystem fragmentViewHolderSystem = (FragmentViewHolderSystem) holder;
final View itemView = fragmentViewHolderSystem.itemView;
// Set Icon
fragmentViewHolderSystem.trainingIconImage.setImageResource(icon);
// Set Title
fragmentViewHolderSystem.title.setText(modelSystemTraining.getItemTitle());
// Set Desc
fragmentViewHolderSystem.description.setText(modelSystemTraining.getItemDescription());
无法在应用程序运行时生成“ R”文件。您可以使用其他替代方法,例如使用if-else
或switch-case
R
再次生成图像