android开发分享dynamic使用setImageDrawable在ImageView中设置图像

我从数据库dynamic生成一个string,它具有相同名称的图像在drawable文件夹中。

现在我想dynamic地使用setImageDrawable(R.id.StringGenerated)ImageView设置该值。

有什么build议么..

    尝试这个,

     int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null); 

    这将返回您要访问的drawable的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); 

    资源可绘制名称不作为string存储,因此您必须将该stringparsing为在构build过程中生成的整数常量。 您可以使用Resources类将stringparsing为该整数。

     Resources res = getResources(); int resourceId = res.getIdentifier( generatedString, "drawable", getPackageName() ); imageView.setImageResource( resourceId ); 

    这将您的生成的stringparsing成ImageView可以用来加载正确的图像的整数。

    或者,您可以使用id手动加载Drawable ,然后使用该drawable而不是资源ID设置图像。

     Drawable drawable = res.getDrawable( resourceId ); imageView.setImageDrawable( drawable ); 

    像这个答案一样简单:

     Drawable myDrawable = getResources().getDrawable(R.drawable.pic); imageview.setImageDrawable(myDrawable); 

    这至less在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()。 正如chetto所述

    如果您不能在不是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); 

    你也可以使用像这样的东西:

    imageView.setImageDrawable(ActivityCompat.getDrawable(getContext(), R.drawable.generatedID));

    或使用毕加索:

    Picasso.with(getContext()).load(R.drawable.generatedId).into(imageView);

    所有发布的答案今天不适用。 例如,getDrawable()已被弃用。 这是一个更新的答案,欢呼!

     ContextCompat.getDrawable(mContext, drawable) 

    从logging的方法

    公共静态最终android.graphics.drawable.Drawable getDrawable(@NotNull android.content.Context上下文,
    @ android.support.annotation.DrawableRes int id

    我和你有同样的问题,我做了以下的解决方法:

     **IMAGEVIEW**.setImageResource(getActivity() .getResources() .getIdentifier("**IMG**", "drawable", getActivity() .getPackageName())); 

    构造一个POJO.java类并创build“构造函数,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”文件。 你可以使用一些其他的select,例如使用if-elseswitch-case

      以上就是android开发分享dynamic使用setImageDrawable在ImageView中设置图像相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/516482.html

      (0)
      上一篇 2020年12月3日
      下一篇 2020年12月3日

      精彩推荐