gpt4 book ai didi

android - 如何在 gridView 中更快地加载应用程序的图像(图标)?

转载 作者:行者123 更新时间:2023-11-29 14:18:49 25 4
gpt4 key购买 nike

我正在显示安装在 gridView 中的所有应用程序。当加载大量应用程序时,比方说 30 个或更多,图标将显示在默认的 Android 图标上,然后几秒钟后更新为正确的图标。我想知道我可以对我的代码进行改进以使图标图像显示更快。

加载以下内容:new LoadIconsTask().execute(mApps.toArray(new AppsInstalled[]{}));

这是我的工作。

private class LoadIconsTask extends AsyncTask<AppsInstalled, Void, Void>{

@Override
protected Void doInBackground(AppsInstalled... params) {
// TODO Auto-generated method stub
Map<String, Drawable> icons = new HashMap<String, Drawable>();
PackageManager manager = getApplicationContext().getPackageManager();

// match package name with icon, set Adapter with loaded Map
for (AppsInstalled app : params) {
String pkgName = app.getAppUniqueId();
Drawable ico = null;
try {
Intent i = manager.getLaunchIntentForPackage(pkgName);
if (i != null) {
ico = manager.getActivityIcon(i);
}
} catch (NameNotFoundException e) {
Log.e(TAG, "Unable to find icon match based on package: " + pkgName
+ " : " + e.getMessage());
}
icons.put(app.getAppUniqueId(), ico);
}
mAdapter.setIcons(icons);
return null;
}

在我使用 loadIconsTask() 之前填充我的应用程序列表

private List<App> loadInstalledApps(boolean includeSysApps) {
List<App> apps = new ArrayList<App>();

// the package manager contains the information about all installed apps
PackageManager packageManager = getPackageManager();

List<PackageInfo> packs = packageManager.getInstalledPackages(0); // PackageManager.GET_META_DATA

for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
// skip system apps if they shall not be included
if ((!includeSysApps)
&& ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1)) {
continue;
}
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description = p.applicationInfo
.loadDescription(packageManager);
app.setDescription(description != null ? description.toString()
: "");
apps.add(app);
}
return apps;
}

关于我的适配器类,它是标准的。我的 getView() 如下所示:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

AppViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);

// creates a ViewHolder and stores a reference to the children view
// we want to bind data to
holder = new AppViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.apptitle);
holder.mIcon = (ImageView) convertView.findViewById(R.id.appicon);
convertView.setTag(holder);
} else {
// reuse/overwrite the view passed assuming that it is castable!
holder = (AppViewHolder) convertView.getTag();
}

App app = mApps.get(position);

holder.setTitle(app.getTitle());
if (mIcons == null || mIcons.get(app.getPackageName()) == null) {
holder.setIcon(mStdImg);
} else {
holder.setIcon(mIcons.get(app.getPackageName()));
}

return convertView;
}

有没有更好的方法?我能否以某种方式将图标的图像存储在数据结构中,并且当我返回此 Activity 时可以跳过 loadIconsTask?那可能吗?提前谢谢你。

最佳答案

您可以使用 Picasso带有自定义 RequestHandler 的库以在后台加载图标。

  1. 首先创建一个 RequestHandler,它将处理需要加载应用程序图标的特定情况。

    public class AppIconRequestHandler extends RequestHandler {

    /** Uri scheme for app icons */
    public static final String SCHEME_APP_ICON = "app-icon";

    private PackageManager mPackageManager;

    public AppIconRequestHandler(Context context) {
    mPackageManager = context.getPackageManager();
    }

    /**
    * Create an Uri that can be handled by this RequestHandler based on the package name
    */
    public static Uri getUri(String packageName) {
    return Uri.fromParts(SCHEME_APP_ICON, packageName, null);
    }

    @Override
    public boolean canHandleRequest(Request data) {
    // only handle Uris matching our scheme
    return (SCHEME_APP_ICON.equals(data.uri.getScheme()));
    }

    @Override
    public Result load(Request request, int networkPolicy) throws IOException {
    String packageName = request.uri.getSchemeSpecificPart();
    Drawable drawable;
    try {
    drawable = mPackageManager.getApplicationIcon(packageName);
    } catch (PackageManager.NameNotFoundException ignored) {
    return null;
    }

    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    return new Result(bitmap, Picasso.LoadedFrom.DISK);
    }
    }
  2. 在您的适配器中,创建一个 Picasso 实例并添加您的 RequestHandler

    // field variable
    private Picasso mPicasso;

    // in constructor
    Picasso.Builder builder = new Picasso.Builder(context);
    builder.addRequestHandler(new AppIconRequestHandler(context));
    mPicasso = builder.build();
  3. 在您的适配器的 getView() 中使用 Picasso 加载图标。

    mPicasso.load(AppIconRequestHandler.getUri(app.packageName)).into(holder.mIcon);

关于android - 如何在 gridView 中更快地加载应用程序的图像(图标)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com