【发布时间】:2012-12-24 11:06:25
【问题描述】:
您好,我找不到在异步模式下加载联系人图片的方法,我的代码在高端手机中似乎没问题,但内存和处理器较少的手机速度很慢
这是我在活动中的代码,我使用onScrollchanged 来避免在滚动条上检索联系人图片
mAdapter = new DialerContactsAdapter(contactos,getActivity());
listaContactos.setAdapter(mAdapter);
listaContactos.setFastScrollEnabled(true);
listaContactos.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case OnScrollListener.SCROLL_STATE_IDLE:
mAdapter.mBusy = false;
mAdapter.notifyDataSetChanged();
break;
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
mAdapter.mBusy = false;
mAdapter.notifyDataSetChanged();
break;
case OnScrollListener.SCROLL_STATE_FLING:
mAdapter.mBusy = true;
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
这是我的自定义适配器,我“总是从数据库加载图像并调整大小,我保存到 LruCache 类中
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder =null;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.dialer_contact_list, null);
holder = new ViewHolder();
holder.Picture = (ImageView) v.findViewById(R.id.imgContactDialer);
holder.Desc =(TextView) v.findViewById(R.id.txtContactNameDialer);
holder.phone =(TextView)v.findViewById(R.id.txtContactNumberDialer);
v.setTag(holder);
}else
{
holder = (ViewHolder)v.getTag();
}
final ViewHolder holder2 = holder;
final ContactInfo ci = (ContactInfo) getItem(position);
holder2.Picture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent contact = new Intent(context, ShowSingleContactFragment.class);
contact.putExtra("id", ci.get_id());
contact.putExtra("name", ci.getDisplayName());
context.startActivity(contact);
}
});
//genera PlaceHolder
Bitmap cacheImg = cache.getBitmapFromMemCache(ci.get_id());
if(cacheImg==null)
{
if(!mBusy)
{
new Thread(new Runnable() {
public void run() {
final Bitmap photo= Contacts.loadContactPhoto(ci.get_id(),context.getBaseContext(),context.getResources().getInteger(R.integer.contact_picture_size));
cache.addBitmapToMemoryCache(ci.get_id(), photo);
holder2.Picture.post(new Runnable() {
public void run() {
holder2.Picture.setImageBitmap(photo);
}
});
}
}).start();
}else
{
holder.Picture.setImageResource(R.drawable.ic_contact_picture);
}
}else
{
holder2.Picture.setImageBitmap(cacheImg);
}
}
这是我的 BitmapCache 类
public class BitmapCache {
public LruCache<String, Bitmap> mMemoryCache;
public BitmapCache(Context mContext)
{
final int memClass = ((ActivityManager) mContext.getSystemService(
Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new LruCache <String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items.
return getByteCount(bitmap);
}
};
}
@SuppressLint("NewApi")
private int getByteCount(Bitmap bitmap)
{
if(Utils.isHoneyComb())
{
return bitmap.getByteCount();
}else
{
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
}
我找不到股票 android 联系人列表,有人可以帮我吗?非常感谢。
【问题讨论】:
-
使用这个...demo 来延迟加载图片...github.com/thest1/LazyList
-
这会将图像存储在缓存中,因此每次加载所有图像都不需要时间,并且它还保存了低处理器和低内存手机中的内存不足错误..
-
尝试使用惰性适配器加载图像.. 这是链接...它可能对您有所帮助.. thinkandroid.wordpress.com/2012/06/13/…
-
我尝试了lazyLoad和终极加载器,但它们速度慢且不稳定:s我开始认为我手机上的文件读取不太好:s(Htc one x)但atm我当前的代码似乎工作更好:(在我的设备上运行良好,但在其他设备上运行速度较慢:(
标签: android listview android-listview android-asynctask android-contacts