我的应用在一个地方运行有点太慢了。应用程序的其他4个不同的活动响应速度相当快,但其中一个活动太慢而不舒服。从主活动中按下一个按钮,它将使用gridView开始下一个活动,gridView在每个视图中填充图像和文本。按下按钮后,它需要太长时间,比如出现gridView加载页面之前的3到5秒。
测试由中兴通讯制造的变速缓慢的600mhz单核处理器安卓手机。南非沙漠中卡拉哈里布须曼人等人民使用的低成本手机之一。对于那些不知道的人来说,说它与三星Galaxy S3或iPhone 5不在同一类中是不足为奇的。
我想知道我是否可以对应用程序运行缓慢部分的代码执行某些操作,以便为速度提供帮助。有任何想法吗?我唯一能看到的是广泛使用多个游标和数据库查询可能会减慢速度。我想加载gridView会占用一些严重的jigaboo。
我的代码很乱,但我想知道我能做些什么。如何在单独的线程中执行所有内容提供程序查询?有任何想法吗?这段代码需要一个enima。
以下是放慢应用程序速度的代码部分示例。
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Media._ID };
imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, img, MediaStore.Images.Media.IS_PRIVATE + "='" + 1 +"'",null, MediaStore.Images.Media._ID);
image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
count = imagecursor.getCount();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,int position, long id) {
System.gc();
String[] img = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE };
imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, img, MediaStore.Images.Media.IS_PRIVATE + "='" + 1 +"'",null, MediaStore.Images.Media._ID);
image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
count = imagecursor.getCount();
imagecursor.moveToPosition(position);
// String[] proj = { };
// imagecursor = managedQuery(
// MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,null, null, null);
// image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String i = imagecursor.getString(image_column_index);
// terra = i;
// opens larger pic Intent intent = new Intent(getApplicationContext(), ViewImage.class);
/* intent.putExtra("filename", i);
startActivity(intent); */
// passing bitmap image to another activiy and starting that activity
// textone.setText(i); (i == /mnt/sdcard/DCIM/pic06.png)
Intent intentBitmapStart = new Intent(ImageThumbnailsActivity.this, Editor.class);
intentBitmapStart.putExtra("filename", i);
startActivity(intentBitmapStart);
// Intent myIntent = new Intent(ImageThumbnailsActivity.this, Editor.class);
// ImageThumbnailsActivity.this.startActivity(myIntent);
}
});
}
这是适配器方法的getview的一个部分。
public View getView(int position,View convertView,ViewGroup parent) {
System.gc();
if (convertView == null) {
// Make up a new view
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.image_item, null);
} else {
// Use convertView if it is available
view = convertView;
}
PanoramioItem s = mImageManager.get(position);
ImageView i = (ImageView) view.findViewById(R.id.image);
i.setImageBitmap(s.getBitmap());
i.setBackgroundResource(R.drawable.picture_frame);
TextView t = (TextView) view.findViewById(R.id.title);
t.setText(s.getTitle());
t = (TextView) view.findViewById(R.id.owner);
t.setText(s.getOwner());
return view;
}
View view;
ImageView iv = new ImageView(mContext.getApplicationContext());
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_adapterview, null);
} else {
// Use convertView if it is available
view = convertView;
}
// Toast.makeText(ImageThumbnailsActivity.this, "image_column_index2: " + image_column_index, Toast.LENGTH_SHORT).show();
imagecursor.moveToPosition(position);
int id = imagecursor.getInt(image_column_index);
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""+ id);
String imagePath = getRealPathFromURI(uri);
String[] titleText = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE };
textcursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, titleText, MediaStore.Images.Media.DATA + "='" + imagePath +"'",null, null);
if(textcursor.moveToFirst()){
titleString = (String) textcursor.getString(textcursor.getColumnIndex(MediaStore.Images.Media.TITLE));
textcursor.close();
}
else{
titleString = "";
}
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeFile(imagePath, bfo);
int imageHeight = bfo.outHeight;
int imageWidth = bfo.outWidth;
String imageType = bfo.outMimeType;
BitmapFactory.Options bfo2 = new BitmapFactory.Options();
int reqHeight = 50;
int reqWidth = 50;
if (imageHeight > reqHeight || imageWidth > reqWidth) {
if (imageWidth > imageHeight) {
bfo2.inSampleSize = Math.round((float)imageHeight / (float)reqHeight);
} else {
bfo2.inSampleSize = Math.round((float)imageWidth / (float)reqWidth);
}
}
Bitmap bm2 = BitmapFactory.decodeFile(imagePath, bfo2);
// iv.setImageBitmap(bm2);
ImageView i = (ImageView) view.findViewById(R.id.adapterimageview);
i.setImageBitmap(bm2);
TextView t = (TextView) view.findViewById(R.id.adaptertextview);
t.setText(titleString);
// iv.setImageURI(Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, ""+ id));
view.setLayoutParams(new GridView.LayoutParams(103, 103));
return view;
}