如何在Android中将Drawable转换为int,反之亦然
我想将Drawable转换为int,反之亦然。基本上,我想将Arraylist对象保存为sharedpreference。为此,我实现了Gson到Srtring的转换方法。如果我在这里使用Drawable而不是int,那么Gson字符串转换会花费很多时间。所以我想使用int而不是Drawable如何在Android中将Drawable转换为int,反之亦然,android,drawable,android-sharedpreferences,Android,Drawable,Android Sharedpreferences,我想将Drawable转换为int,反之亦然。基本上,我想将Arraylist对象保存为sharedpreference。为此,我实现了Gson到Srtring的转换方法。如果我在这里使用Drawable而不是int,那么Gson字符串转换会花费很多时间。所以我想使用int而不是Drawable private List<AppInfo> apps = null; public void setIcon(int icon) { this.icon = icon; }
private List<AppInfo> apps = null;
public void setIcon(int icon) {
this.icon = icon;
}
apps.get(position).setIcon(mContext.getResources().getDrawable(apps.get(position).getIcon()));
下面是将自定义对象的ArrayList转换为字符串的源代码,以便我可以将其保存到SharedReference
Gson gson = new Gson();
apps.get(number).setColor(picker.getColor());
String JsonAppsdata = gson.toJson(apps);
System.out.println("Storing="+JsonAppsdata);
utility.StoreData(getApplicationContext(), JsonAppsdata);
Int->可绘制:
Drawable icon=getResources().getDrawable(42,getTheme())代码>
可绘制->Int:
(我假设您正在填充列表,然后重新加载它们。(或者,更好的是,依赖现有的图像缓存解决方案,如)
UPD:
没有直接的方法将Drawable
转换为int
,但是在这种特殊情况下,可以从PackageManager
获取int
,而不是Drawable
:
ApplicationInfo applicationInfo = mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),1);
int icon= applicationInfo.icon;
这就是我们如何获取应用程序图标并为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));
}
唯一对我有效的解决方案是@KonstantinLoginov提供的,但不是使用:
val drawable=context.getPackageManager().getApplicationInfo(applicationInfo)
我通过了程序包名称:
val drawable=context.getPackageManager().getApplicationIcon(packageName)
这是一个字符串,可以很容易地传递。您的资源中是否有所有图标?或者您是从服务器下载的?如何填充列表应用程序?第一次我是从res/drawable中填充它的,然后如果有某个(用户)要使用某个系统内应用程序图标更改该图标,则我需要获取系统安装应用程序图标。简而言之,有时我需要从res/drawable获取图标,有时我需要通过此mContext.getPackageManager().getApplicationCon(apps.get(position.getPname())从系统中获取安装应用程序图标那么,是什么阻止您将这个mContext.getPackageManager().getApplicationInfo()int保存到您的AppInfo中呢?让我们来看看。我面临的问题是,当ApplicationInfo ApplicationInfo=mContext.getPackageManager().getApplicationInfo(apps.get(position)时,资源id找不到异常.getPname(),1);int icon=applicationInfo.icon;apps.get(position).setIcon(applicationInfo.icon);//返回时int->DrawableDrawable icon=getResources().getDrawable(apps.get(i).getIcon();ImageView ImageView=(ImageView)rootView.findViewById(imageArray[i]);ImageView.setImageDrawable(图标);
如何对drawableLeft执行相同操作?我正在将drawableLeft与文本视图一起使用,并使用getCompoundDrawables方法检索其值。那么如何将其转换为int。
ApplicationInfo applicationInfo = mContext.getPackageManager().getApplicationInfo(apps.get(position).getPname(),1);
int icon= applicationInfo.icon;
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));
}