【问题标题】:How to convert Drawable into int and vice versa in Android如何在Android中将Drawable转换为int,反之亦然
【发布时间】:2016-04-01 14:13:06
【问题描述】:

我想将 Drawable 转换为 int ,然后反之亦然。基本上我想将 Arraylist 对象保存到 sharedPrefrence强>。为此,我实施了 Gson 到 Srtring 的转换方法。如果我在这里使用 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 转换为字符串的来源,以便我可以将其保存到 SharedPrefrence。

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

【问题讨论】:

  • 您的资源中有所有图标吗?还是从服务器下载?
  • 你如何填充 List 应用程序?
  • 第一次我从 res/drawable 填充它,然后如果有人(用户)想用系统应用程序图标中的某个人更改该图标,那么我需要获取系统安装应用程序图标。简而言之,有时我需要从 res/drawable 中获取,有时我需要通过此 mContext.getPackageManager().getApplicationIcon(apps.get(position).getPname()) 从系统中安装应用程序的图标中获取图标
  • 那么是什么阻止您将此 mContext.getPackageManager().getApplicationIcon() int 保存到您的 AppInfo 中?

标签: android drawable android-sharedpreferences


【解决方案1】:

Int -> 可绘制:

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

可绘制 -> 整数:

(我假设您正在使用图标已在您的应用程序的res/drawable 文件夹中的应用程序填充List&lt;AppInfo&gt; 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());

如果您的图标来自服务器 - 唯一的方法是 to store them to disk 然后重新加载它们。 (或者,更好的是,依赖现有的图像缓存解决方案,如 picasso

统一更新: 没有将Drawable 转换为int 的直接方法,但在这种特殊情况下,可以从PackageManager 获取int,而不是Drawable

ApplicationInfo applicationInfo = mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),1);
int icon= applicationInfo.icon;

【讨论】:

  • 我面临的问题是 ApplicationInfo applicationInfo=mContext.getPackageManager() 时找不到资源 ID 异常。 getApplicationInfo(apps.get(position).getPname(),1);诠释图标=应用信息.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);
  • 如何为 drawableLeft 做同样的事情?我将 drawableLeft 与 TextView 一起使用,并使用 getCompoundDrawables 方法检索它的值。那么如何将其转换为 int。
【解决方案2】:

这就是我们获取应用程序图标并将其设置为图像视图的方式。

                   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));

                    }

【讨论】:

    【解决方案3】:

    唯一对我有用的解决方案是@KonstantinLoginov 的chat,但不是使用:

    val drawable = context.getPackageManager().getApplicationIcon(applicationInfo),

    我传入了包名:

    val drawable = context.getPackageManager().getApplicationIcon(packageName) 是字符串,可以轻松传递。

    【讨论】:

      猜你喜欢
      • 2021-06-10
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 2015-02-05
      • 2020-10-24
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多