我正在开发一个启动器应用程序,在那里我创建了一个主屏幕,在我的主屏幕上有一个应用程序抽屉按钮。当我按下应用程序抽屉按钮时,打开我的应用程序活动需要很长时间!我不知道为什么会这样,我尝试了像Nexus 5这样的高端设备,但没有变化。有解决方案吗这是我的代码:
从HomeScreen到Apps活动代码:
drawer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, Apps.class);
startActivity(myIntent);
overridePendingTransition( R.anim.left, R.anim.rtl);
}
});
AppsActivity.java:
public class Apps extends Activity {
SharedPreferences colors_app, color_label;
GridView mGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.apps);
loadApps();
mGrid = (GridView) findViewById(R.id.app_grid);
AppsAdapter adapter = new AppsAdapter(this);
mGrid.setAdapter(adapter);
colors_app = getSharedPreferences("MyColor", 1);
int colorcode2 = colors_app.getInt("color_code", 0);
if (colorcode2 != 0) {
Apps.this.findViewById(R.id.apps_back).setBackgroundColor(
colorcode2);
}
mGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent,
View convertView, int position, long id) {
// TODO Auto-generated method stub
ResolveInfo cleckedResolveInfo2 = (ResolveInfo) parent
.getItemAtPosition(position);
ActivityInfo clickedActivityInfo2 = cleckedResolveInfo2.activityInfo;
Uri packageURI = Uri.parse("package:"
+ clickedActivityInfo2.applicationInfo.packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
packageURI);
startActivity(uninstallIntent);
return true;
}
});
mGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View convertView,
int position, long id) {
ResolveInfo cleckedResolveInfo = (ResolveInfo) parent
.getItemAtPosition(position);
ActivityInfo clickedActivityInfo = cleckedResolveInfo.activityInfo;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(
clickedActivityInfo.applicationInfo.packageName,
clickedActivityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
}
});
}
private List<ResolveInfo> mApps;
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
public class AppsAdapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
public AppsAdapter(Context c) {
context = c;
layoutInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
if (convertView == null) {
grid = new View(context);
grid = layoutInflater.inflate(R.layout.grid_custom, null);
} else {
grid = (View) convertView;
}
ResolveInfo info = mApps.get(position);
ImageView icon = (ImageView) grid.findViewById(R.id.icon);
icon.setImageDrawable(info.activityInfo
.loadIcon(getPackageManager()));
TextView label = (TextView) grid.findViewById(R.id.label);
Typeface tf_label = Typeface.createFromAsset(getAssets(),
"fonts/RobotoC-Regular.ttf");
label.setTypeface(tf_label);
label.setText(info.activityInfo.loadLabel(getPackageManager())
.toString());
color_label = getSharedPreferences("LabelColor", 1);
int color_lab = color_label.getInt("color_code_label", 0);
if (color_lab != 0) {
label.setTextColor(color_lab);
}
return grid;
}
public final int getCount() {
return mApps.size();
}
public final Object getItem(int position) {
return mApps.get(position);
}
public final long getItemId(int position) {
return position;
}
}
}
Nobodys得到了答案?
答案 0 :(得分:0)
分析代码的性能可以让您了解瓶颈的位置。
我建议它将在getView()
函数中:
(1)已知对loadLabel
的函数调用可能很慢。
PackageInfo LoadLabel slow performance
(2)为适配器中的每个项目调用Typeface.createFromAsset
不是一个好主意。