在Android中如何将Drawable转换为int,以及反向转换

14

我想将Drawable转换为int,然后再进行反向转换。基本上,我想将ArrayList对象保存到sharedPrefrence中。为此,我已经实现了一个Gson转换方法来将其转换为字符串。如果我在这里使用Drawable而不是int,那么Gson字符串转换会花费很长时间。所以我想使用int代替Drawable。

 private List<AppInfo> apps = null;

   public void setIcon(int icon) {
    this.icon = icon;
}

   apps.get(position).setIcon(mContext.getResources().getDrawable(apps.get(position).getIcon()));

这里的AppInfo是什么

    public AppInfo(String appname, String pname, String versionName, int versionCode, int icon, int color) {
    this.appname = appname;
    this.pname = pname;
    this.versionName = versionName;
    this.versionCode = versionCode;
    this.icon = icon;
    this.color = color;
}

这里是将自定义对象的ArrayList转换为字符串的源代码,以便我可以将其保存到SharedPreference中。

            Gson gson = new Gson();
            apps.get(number).setColor(picker.getColor());
            String JsonAppsdata = gson.toJson(apps);
            System.out.println("Storing="+JsonAppsdata);
            utility.StoreData(getApplicationContext(), JsonAppsdata);

你的资源中是否包含所有图标?还是从服务器下载它们? - Konstantin Loginov
你如何填充List<AppInfo> apps? - Konstantin Loginov
首先,我从res/drawable中填充它,然后如果有人(用户)想要将该图标更改为系统应用程序图标中的某个图标,则需要获取系统安装应用程序图标。简而言之,有时我需要从res/drawable中获取,有时我需要从系统中安装的应用程序图标中获取,通过mContext.getPackageManager().getApplicationIcon(apps.get(position).getPname())实现。 - M.ArslanKhan
那么,是什么阻止你将mContext.getPackageManager().getApplicationIcon() int保存到你的AppInfo中呢? - Konstantin Loginov
1
让我们在聊天中继续这个讨论 - M.ArslanKhan
显示剩余3条评论
3个回答

8

Int -> Drawable:

Drawable icon = getResources().getDrawable(42, getTheme());

Drawable -> Int:

(我假设您正在使用已经在应用程序的res / drawable文件夹中的应用程序图标来填充List<AppInfo> apps)

一旦您将R.drawable.app1设置为ImageView,您还可以给它一个tag以便稍后在ImageView中识别资源:

    ImageView appIcon1ImageView = (ImageView)findViewById(R.id.app_icon_1);
    appIcon1ImageView.setImageDrawable(getDrawable(R.drawable.app1));
    appIcon1ImageView.setTag(R.drawable.app1);

    ......
    // Once you need to identify which resource is in ImageView
    int drawableId = Integer.parseInt(appIcon1ImageView.getTag().toString());

如果您的图标来自服务器,唯一的方法是将它们存储到磁盘,然后重新加载它们。(或者更好的是,依赖于已经存在的图片缓存解决方案,如picasso)
更新: 没有直接将Drawable转换为int的方法,但在这种特定情况下,可以从PackageManager获取int,而不是Drawable
ApplicationInfo applicationInfo = mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),1);
int icon= applicationInfo.icon;

我现在遇到的问题是资源 id 未找到异常,代码如下:ApplicationInfo applicationInfo=mContext.getPackageManager(). getApplicationInfo(apps.get(position).getPname(),1); int icon= applicationInfo.icon; apps.get(position).setIcon(applicationInfo.icon); //转回来时要用 int->Drawable Drawable icon = getResources().getDrawable(apps.get(i).getIcon()); ImageView imageView = (ImageView) rootView.findViewById(imageArray[i]); imageView.setImageDrawable(icon); - M.ArslanKhan
我如何对drawableLeft执行相同的操作?我正在使用TextView和drawableLeft,并使用getCompoundDrawables方法检索其值。那么我该如何将其转换为int? - Tirth Patel

1
这是我们如何获取应用程序图标并将其设置为ImageView的方法。
                   applicationInfo=mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),PackageManager.GET_META_DATA);
                   int icon= applicationInfo.icon;
                   Rsources resources=mContext.getPackageManager().getResourcesForApplication(applicationInfo);

                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        holder.itemIcon.setImageDrawable(resources.getDrawable(icon,null));
                    }else
                    {
                         holder.itemIcon.setImageDrawable(resources.getDrawable(icon));

                    }

0
唯一对我有效的解决方案是由@KonstantinLoginov在聊天中提供的,但我使用的不是: < p > < code > val drawable = context.getPackageManager().getApplicationIcon(applicationInfo) , 我传递了packageName: < p > < code > val drawable = context.getPackageManager().getApplicationIcon(packageName),它是字符串,可以轻松地传递。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接