我正在努力找出在适配器中运行许多Intent
的最佳后台进程。我有一个自定义的GridView
,它将照片从SD card
上传到ImageView
。我需要在所有照片(其中24张)上放置点击监听器,所以我为它们创建了Intent
,当点击时,转到另一个活动来显示完整的照片。
我的代码是有效的,但是运行时间慢了很多。太多了。因此,现在我正试图决定在适配器中最好使用哪一个(如果这是放置单击侦听器的最佳位置?)我会使用AsyncTask
、Thread
还是IntentService
?我想我以前没有尝试过任何一种(除了超级简单的教程),所以我不确定哪种对我的情况最好,因为它更复杂。我唯一要做的就是更新UI
。因此,当我的GridView
中的缩略图被点击时,它们必须转到一个活动,并用GridView
中点击位置的当前照片更新该新活动。所以我基本上需要在新活动的ImageView
中放入一张照片Bitmap
。
我读到AsyncTask
有利于更新UI
,但我只是想确保这是一个正确的决定,然后再进行大量研究。这里有很多经验丰富的开发人员,所以我会重视不同的视角。也许你有一些我以前没有考虑过的事情。我是一个初学者,所以设计图案对我来说仍然很难。下面是我的适配器的工作示例,其中包含所有Intent
。
以下是新活动需要进入的ImageView
的一个片段(总共有24个)。它需要接收Intent
。
ImageView photoView0 = (ImageView)findViewById(R.id.photo_display0);
byte[] byteArray = getIntent().getByteArrayExtra("photo0");
Bitmap bm = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
photoView0.setImageBitmap(bm);
GridViewPhotoAdapter.java
package org.azurespot.cutecollection.phototab;
/**
* Created by mizu on 2/5/15.
*/
// package org.azurespot.cutecollection;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.azurespot.R;
import java.io.ByteArrayOutputStream;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter<PhotoGridItem> {
public Context context;
private int resourceId;
Bitmap bm;
ViewHolder holder = null;
int position;
public GridViewPhotoAdapter(Context context, int layoutResourceId) {
super(context, layoutResourceId);
this.context = context;
this.resourceId = layoutResourceId;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
this.position = position;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(R.id.photo_grid_view);
// stores holder with view
row.setTag(holder);
} else {
holder = (ViewHolder)row.getTag();
}
PhotoGridItem photoGridItem = getItem(position);
if (photoGridItem != null) {
bm = photoGridItem.getImage();
holder.imageView.setImageBitmap(bm);
// positioning the image in the GridView slot
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(270, 270));
}
//Convert to bitmap to byte array, so can pass through intent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
// set my click listeners
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (position) {
case 0:
Intent a = new Intent(context, PhotoActivity0.class);
a.putExtra("photo0", byteArray);
context.startActivity(a);
break;
case 1:
Intent b = new Intent(context, PhotoActivity1.class);
b.putExtra("photo1", byteArray);
context.startActivity(b);
break;
case 2:
Intent c = new Intent(context, PhotoActivity2.class);
c.putExtra("photo2", byteArray);
context.startActivity(c);
break;
case 3:
Intent d = new Intent(context, PhotoActivity3.class);
d.putExtra("photo3", byteArray);
context.startActivity(d);
break;
case 4:
Intent e = new Intent(context, PhotoActivity4.class);
e.putExtra("photo4", byteArray);
context.startActivity(e);
break;
case 5:
Intent f = new Intent(context, PhotoActivity5.class);
f.putExtra("photo5", byteArray);
context.startActivity(f);
break;
case 6:
Intent g = new Intent(context, PhotoActivity6.class);
g.putExtra("photo6", byteArray);
context.startActivity(g);
break;
case 7:
Intent h = new Intent(context, PhotoActivity7.class);
h.putExtra("photo7", byteArray);
context.startActivity(h);
break;
case 8:
Intent i = new Intent(context, PhotoActivity8.class);
i.putExtra("photo8", byteArray);
context.startActivity(i);
break;
case 9:
Intent j = new Intent(context, PhotoActivity9.class);
j.putExtra("photo9", byteArray);
context.startActivity(j);
break;
case 10:
Intent k = new Intent(context, PhotoActivity10.class);
k.putExtra("photo10", byteArray);
context.startActivity(k);
break;
case 11:
Intent l = new Intent(context, PhotoActivity11.class);
l.putExtra("photo11", byteArray);
context.startActivity(l);
break;
case 12:
Intent m = new Intent(context, PhotoActivity12.class);
m.putExtra("photo12", byteArray);
context.startActivity(m);
break;
case 13:
Intent n = new Intent(context, PhotoActivity13.class);
n.putExtra("photo13", byteArray);
context.startActivity(n);
break;
case 14:
Intent o = new Intent(context, PhotoActivity14.class);
o.putExtra("photo14", byteArray);
context.startActivity(o);
break;
case 15:
Intent p = new Intent(context, PhotoActivity15.class);
context.startActivity(p);
break;
case 16:
Intent q = new Intent(context, PhotoActivity16.class);
q.putExtra("photo16", byteArray);
context.startActivity(q);
break;
case 17:
Intent r = new Intent(context, PhotoActivity17.class);
r.putExtra("photo17", byteArray);
context.startActivity(r);
break;
case 18:
Intent s = new Intent(context, PhotoActivity18.class);
s.putExtra("photo18", byteArray);
context.startActivity(s);
break;
case 19:
Intent t = new Intent(context, PhotoActivity19.class);
t.putExtra("photo19", byteArray);
context.startActivity(t);
break;
case 20:
Intent u = new Intent(context, PhotoActivity20.class);
u.putExtra("photo20", byteArray);
context.startActivity(u);
break;
case 21:
Intent v = new Intent(context, PhotoActivity21.class);
v.putExtra("photo21", byteArray);
context.startActivity(v);
break;
case 22:
Intent w = new Intent(context, PhotoActivity22.class);
w.putExtra("photo22", byteArray);
context.startActivity(w);
break;
case 23:
Intent x = new Intent(context, PhotoActivity23.class);
x.putExtra("photo23", byteArray);
context.startActivity(x);
break;
}
}
});
return row;
}
public class ViewHolder{
ImageView imageView;
}
}
您曾经考虑过使用缓存吗?谷歌网页@缓存位图。这个想法是使用所谓的LRU缓存和ASyncTask。请注意,ASyncTask本身并不能提高性能。以下是网页中的一段代码片段,对我来说很好:
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
...
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
final Bitmap bitmap = decodeSampledBitmapFromResource(
getResources(), params[0], 100, 100));
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
return bitmap;
}
...
}
注意呼叫decodeSampledBitmapFromResource
,然后呼叫addBitmapToMemoryCache
,以及该网页上的其他详细信息。嗯,我希望这至少有帮助;)