我想知道是否可以以任何方式更改我的代码以加快加载时间。当用户想要更改主屏幕上的图标时,他们单击“选择图像”并选择我的活动。
这就是延迟发生的时候。加载大约需要 7 到 8 秒。现在我确实加载了 1,000 个图标,但在我添加多个网格视图之前,它只花了大约 2 到 3 秒。我添加了多个网格视图,因此我可以分割我的图标,我想保留它。
这是加载 icons2 类时的 logcat。我删除了许多 GC_FOR_ALLOC 条目,因为它对于这篇文章来说太大了。
我查看了 DDMS,并将活动的分配跟踪器与其他快速且非常接近的主题进行了比较。我在想我在icons2
课堂上做的工作比我需要做的要多,或者填充这么多网格视图不是一个好习惯?
06-13 14:14:09.334: D/dalvikvm(1779): GC_CONCURRENT freed 426K, 4% free 13815K/14352K, paused 10ms+0ms, total 15ms
06-13 14:14:09.525: W/Trace(1779): Unexpected value from nativeGetEnabledTags: 0
06-13 14:14:09.535: W/Trace(1779): Unexpected value from nativeGetEnabledTags: 0
06-13 14:14:09.535: W/Trace(1779): Unexpected value from nativeGetEnabledTags: 0
06-13 14:14:09.557: W/Trace(1779): Unexpected value from nativeGetEnabledTags: 0
06-13 14:14:09.595: W/Trace(1779): Unexpected value from nativeGetEnabledTags: 0
06-13 14:14:09.605: D/dalvikvm(1779): GC_CONCURRENT freed 1587K, 11% free 14168K/15868K, paused 6ms+3ms, total 11ms
06-13 14:14:09.655: D/dalvikvm(1779): GC_FOR_ALLOC freed 732K, 12% free 14424K/16280K, paused 5ms, total 13ms
06-13 14:14:09.685: D/dalvikvm(1779): GC_FOR_ALLOC freed 650K, 11% free 14804K/16572K, paused 3ms, total 3ms
06-13 14:14:09.705: D/dalvikvm(1779): GC_FOR_ALLOC freed 731K, 12% free 15060K/16944K, paused 2ms, total 2ms
06-13 14:14:09.737: D/dalvikvm(1779): GC_FOR_ALLOC freed 570K, 11% free 15435K/17152K, paused 14ms, total 14ms
06-13 14:14:09.905: D/dalvikvm(1779): GC_FOR_ALLOC freed 650K, 11% free 15733K/17564K, paused 117ms, total 117ms
06-13 14:14:10.044: D/dalvikvm(1779): GC_FOR_ALLOC freed 650K, 11% free 16032K/17856K, paused 129ms, total 129ms
06-13 14:14:10.104: D/dalvikvm(1779): GC_FOR_ALLOC freed 650K, 11% free 16331K/18188K, paused 18ms, total 18ms
06-13 14:14:10.144: D/dalvikvm(1779): GC_FOR_ALLOC freed 570K, 10% free 16675K/18480K, paused 4ms, total 5ms
06-13 14:14:10.214: D/dalvikvm(1779): GC_FOR_ALLOC freed 650K, 11% free 16893K/18772K, paused 18ms, total 18ms
06-13 14:14:10.224: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 17154K/18980K, paused 2ms, total 3ms
06-13 14:14:10.265: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 17416K/19268K, paused 17ms, total 17ms
06-13 14:14:10.285: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 17677K/19516K, paused 2ms, total 3ms
06-13 14:14:10.304: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 17939K/19804K, paused 3ms, total 3ms
06-13 14:14:10.344: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 18200K/20052K, paused 22ms, total 22ms
06-13 14:14:10.364: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 18462K/20340K, paused 3ms, total 4ms
06-13 14:14:10.374: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 18724K/20588K, paused 3ms, total 3ms
06-13 14:14:10.414: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 10% free 18986K/20876K, paused 18ms, total 19ms
06-13 14:14:10.444: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 9% free 19247K/21124K, paused 18ms, total 18ms
06-13 14:14:10.464: D/dalvikvm(1779): GC_FOR_ALLOC freed 487K, 9% free 19553K/21372K, paused 3ms, total 3ms
06-13 14:14:10.474: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 9% free 19733K/21660K, paused 3ms, total 3ms
06-13 14:14:10.514: D/dalvikvm(1779): GC_FOR_ALLOC freed 489K, 9% free 20040K/21868K, paused 18ms, total 18ms
06-13 14:14:10.524: D/dalvikvm(1779): GC_FOR_ALLOC freed 569K, 9% free 20221K/22156K, paused 2ms, total 2ms
06-13 14:14:10.564: D/dalvikvm(1779): GC_FOR_ALLOC freed 488K, 9% free 20445K/22320K, paused 17ms, total 18ms
06-13 14:14:10.576: D/dalvikvm(1779): GC_FOR_ALLOC freed 487K, 9% free 20669K/22528K, paused 3ms, total 3ms
06-13 14:14:10.614: D/dalvikvm(1779): GC_FOR_ALLOC freed 488K, 9% free 20893K/22776K, paused 19ms, total 19ms
06-13 14:14:10.624: D/dalvikvm(1779): GC_FOR_ALLOC freed 487K, 9% free 21117K/22984K, paused 3ms, total 3ms
06-13 14:14:10.666: D/dalvikvm(1779): GC_FOR_ALLOC freed 491K, 9% free 21345K/23232K, paused 20ms, total 20ms
06-13 14:14:10.685: D/dalvikvm(1779): GC_FOR_ALLOC freed 487K, 9% free 21569K/23480K, paused 6ms, total 7ms
06-13 14:14:10.714: D/dalvikvm(1779): GC_FOR_ALLOC freed 488K, 8% free 21793K/23688K, paused 18ms, total 18ms
06-13 14:14:10.744: D/dalvikvm(1779): GC_FOR_ALLOC freed 487K, 9% free 22017K/23936K, paused 16ms, total 17ms
06-13 14:14:10.756: D/dalvikvm(1779): GC_FOR_ALLOC freed 406K, 8% free 22285K/24144K, paused 0ms, total 0ms
06-13 14:14:10.794: D/dalvikvm(1779): GC_FOR_ALLOC freed 488K, 8% free 22509K/24436K, paused 17ms, total 17ms
06-13 14:14:10.804: D/dalvikvm(1779): GC_FOR_ALLOC freed 487K, 8% free 22652K/24600K, paused 6ms, total 6ms
06-13 14:14:10.847: D/dalvikvm(1779): GC_FOR_ALLOC freed 407K, 8% free 22920K/24808K, paused 22ms, total 22ms
06-13 14:14:10.865: D/dalvikvm(1779): GC_FOR_ALLOC freed 487K, 8% free 23063K/25056K, paused 7ms, total 7ms
06-13 14:14:10.895: D/dalvikvm(1779): GC_FOR_ALLOC freed 407K, 8% free 23249K/25140K, paused 18ms, total 18ms
06-13 14:14:10.924: D/dalvikvm(1779): GC_FOR_ALLOC freed 406K, 8% free 23436K/25348K, paused 17ms, total 17ms
06-13 14:14:10.937: D/dalvikvm(1779): GC_FOR_ALLOC freed 407K, 8% free 23623K/25556K, paused 13ms, total 13ms
06-13 14:14:11.065: D/dalvikvm(1779): GC_FOR_ALLOC freed 406K, 8% free 23810K/25764K, paused 117ms, total 117ms
06-13 14:14:11.075: D/dalvikvm(1779): GC_FOR_ALLOC freed 406K, 8% free 23997K/25932K, paused 5ms, total 5ms
06-13 14:14:11.085: D/dalvikvm(1779): GC_FOR_ALLOC freed 407K, 8% free 24183K/26140K, paused 3ms, total 3ms
06-13 14:14:11.105: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 24414K/26308K, paused 4ms, total 4ms
06-13 14:14:11.335: D/dalvikvm(1779): GC_FOR_ALLOC freed 407K, 8% free 24520K/26516K, paused 217ms, total 217ms
06-13 14:14:11.345: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 24669K/26600K, paused 13ms, total 13ms
06-13 14:14:11.355: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 24899K/26808K, paused 2ms, total 3ms
06-13 14:14:11.477: D/dalvikvm(1779): GC_FOR_ALLOC freed 407K, 8% free 25005K/27016K, paused 103ms, total 103ms
06-13 14:14:11.485: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 25154K/27100K, paused 3ms, total 3ms
06-13 14:14:11.614: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 25304K/27264K, paused 116ms, total 116ms
06-13 14:14:11.624: D/dalvikvm(1779): GC_FOR_ALLOC freed 326K, 8% free 25453K/27388K, paused 3ms, total 3ms
06-13 14:14:11.634: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 25603K/27552K, paused 5ms, total 6ms
06-13 14:14:11.654: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 25752K/27716K, paused 7ms, total 8ms
06-13 14:14:11.684: D/dalvikvm(1779): GC_FOR_ALLOC freed 326K, 8% free 25901K/27880K, paused 17ms, total 18ms
06-13 14:14:11.696: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 7% free 26051K/28004K, paused 3ms, total 3ms
06-13 14:14:11.716: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 7% free 26200K/28168K, paused 7ms, total 8ms
06-13 14:14:11.744: D/dalvikvm(1779): GC_FOR_ALLOC freed 326K, 7% free 26350K/28332K, paused 19ms, total 20ms
06-13 14:14:11.754: D/dalvikvm(1779): GC_FOR_ALLOC freed 325K, 8% free 26499K/28496K, paused 4ms, total 4ms
06-13 14:14:20.944: I/Choreographer(1779): Skipped 1161 frames! The application may be doing too much work on its main thread.
这是我的icons2类
package bigdx.clean.multi;
import java.util.ArrayList;
import bigdx.clean.multi.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class Icons2 extends Activity {
public Uri CONTENT_URI;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
setContentView(R.layout.icon_picker);
ExpandableHeightGridView g=(ExpandableHeightGridView) findViewById(R.id.icon_grid_folders);
ExpandableHeightGridView g2=(ExpandableHeightGridView) findViewById(R.id.icon_grid_apps);
ExpandableHeightGridView g3=(ExpandableHeightGridView) findViewById(R.id.icon_grid_google);
ExpandableHeightGridView g4=(ExpandableHeightGridView) findViewById(R.id.icon_grid_system);
ExpandableHeightGridView g5=(ExpandableHeightGridView) findViewById(R.id.icon_grid_games);
ExpandableHeightGridView g6=(ExpandableHeightGridView) findViewById(R.id.icon_grid_text);
g.setExpanded(true);
g2.setExpanded(true);
g3.setExpanded(true);
g4.setExpanded(true);
g5.setExpanded(true);
g6.setExpanded(true);
g.setNumColumns(GridView.AUTO_FIT);
g2.setNumColumns(GridView.AUTO_FIT);
g3.setNumColumns(GridView.AUTO_FIT);
g4.setNumColumns(GridView.AUTO_FIT);
g5.setNumColumns(GridView.AUTO_FIT);
g6.setNumColumns(GridView.AUTO_FIT);
g.setColumnWidth(iconSize);
g2.setColumnWidth(iconSize);
g3.setColumnWidth(iconSize);
g4.setColumnWidth(iconSize);
g5.setColumnWidth(iconSize);
g6.setColumnWidth(iconSize);
g.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
g2.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
g3.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
g4.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
g5.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
g6.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
g.setVerticalSpacing(iconSize/3);
g2.setVerticalSpacing(iconSize/3);
g3.setVerticalSpacing(iconSize/3);
g4.setVerticalSpacing(iconSize/3);
g5.setVerticalSpacing(iconSize/3);
g6.setVerticalSpacing(iconSize/3);
g.setAdapter(new IconsAdapter(this, iconSize, g.getId()));
g2.setAdapter(new IconsAdapter(this, iconSize, g2.getId()));
g3.setAdapter(new IconsAdapter(this, iconSize, g3.getId()));
g4.setAdapter(new IconsAdapter(this, iconSize, g4.getId()));
g5.setAdapter(new IconsAdapter(this, iconSize, g5.getId()));
g6.setAdapter(new IconsAdapter(this, iconSize, g6.getId()));
g.setOnItemClickListener(new AdapterView.OnItemClickListener(){
private Uri CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String str = adapterView.getItemAtPosition(position).toString();
setResult(-1, new Intent(null, Uri.withAppendedPath(this.CONTENT_URI, str)));
finish();
}
});
g2.setOnItemClickListener(new AdapterView.OnItemClickListener(){
private Uri CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String str = adapterView.getItemAtPosition(position).toString();
setResult(-1, new Intent(null, Uri.withAppendedPath(this.CONTENT_URI, str)));
finish();
}
});
g3.setOnItemClickListener(new AdapterView.OnItemClickListener(){
private Uri CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String str = adapterView.getItemAtPosition(position).toString();
setResult(-1, new Intent(null, Uri.withAppendedPath(this.CONTENT_URI, str)));
finish();
}
});
g4.setOnItemClickListener(new AdapterView.OnItemClickListener(){
private Uri CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String str = adapterView.getItemAtPosition(position).toString();
setResult(-1, new Intent(null, Uri.withAppendedPath(this.CONTENT_URI, str)));
finish();
}
});
g5.setOnItemClickListener(new AdapterView.OnItemClickListener(){
private Uri CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String str = adapterView.getItemAtPosition(position).toString();
setResult(-1, new Intent(null, Uri.withAppendedPath(this.CONTENT_URI, str)));
finish();
}
});
g6.setOnItemClickListener(new AdapterView.OnItemClickListener(){
private Uri CONTENT_URI = Uri.parse("content://" + IconsProvider.class.getCanonicalName());
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String str = adapterView.getItemAtPosition(position).toString();
setResult(-1, new Intent(null, Uri.withAppendedPath(this.CONTENT_URI, str)));
finish();
}
});
}
class IconsAdapter extends BaseAdapter{
private Context mContext;
private int mIconSize;
private ArrayList<Integer> mThumbs;
private int mG;
public IconsAdapter(Context mContext, int iconsize, int g) {
this.mContext = mContext;
this.mIconSize = iconsize;
this.mG = g;
loadIcons();
}
@Override
public int getCount() {
return mThumbs.size();
}
@Override
public Object getItem(int position) {
return mThumbs.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbs.get(position));
return imageView;
}
private void loadIcons() {
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
int folders = R.id.icon_grid_folders;
int apps = R.id.icon_grid_apps;
int system = R.id.icon_grid_system;
int google = R.id.icon_grid_google;
int text = R.id.icon_grid_text;
int games = R.id.icon_grid_games;
if (mG == folders) {
mThumbs = new ArrayList<Integer>();
addIcons(resources, packageName, R.array.icon_pack_folders);
}
if (mG == apps) {
mThumbs = new ArrayList<Integer>();
addIcons(resources, packageName, R.array.icon_pack_apps);
}
if (mG == games) {
mThumbs = new ArrayList<Integer>();
addIcons(resources, packageName, R.array.icon_pack_games);
}
if (mG == text) {
mThumbs = new ArrayList<Integer>();
addIcons(resources, packageName, R.array.icon_pack_text);
}
if (mG == google) {
mThumbs = new ArrayList<Integer>();
addIcons(resources, packageName, R.array.icon_pack_google);
}
if (mG == system) {
mThumbs = new ArrayList<Integer>();
addIcons(resources, packageName, R.array.icon_pack_system);
}
}
private void addIcons(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra,"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
}
}
}
}
}
}