异步加载联系人列表中的图像

时间:2013-01-10 05:42:28

标签: android listview android-listview android-asynctask android-contacts

嗨我找不到加载异步模式联系人图片的方法,我的代码在高端手机中似乎没问题,但内存和处理器较少的手机很慢 这是我的活动中的代码,我使用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);
    }

}

我找不到股票安卓联系人列表,有人可以帮帮我吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

正如@Janmejoy所说,懒人装载机是更好的选择,至少我是这么认为的。因为几个星期前我有同样的issue。我所做的是使用Lazy loader和ViewHolder。首先,我没有加载小图像的任何问题。但是,我发现较大的图像似乎没有正确缩放,并且表现得很糟糕。然后我对Lazy loader进行了一些更改,以便它可以正确地缩放图像。我的缩放代码基于this

在ImageLoader类中,我定义了一个变量DisplayMetrics disMetrics;,在构造函数中我做了这个:disMetrics = new DisplayMetrics();

下一步是在decodeFile(File file)方法中进行一些更改:

//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1=new FileInputStream(f);
        BitmapFactory.decodeStream(stream1,null,o);
        stream1.close();

                   // my custom scaling
        int tw = disMetrics.widthPixels;
        int th = disMetrics.heightPixels;
        int targetWidth = tw <= 0 ? Integer.MAX_VALUE : tw;
        int targetHeight = th <= 0 ? Integer.MAX_VALUE : th;

        int scale = 0;
        while ((o.outWidth >> scale) > targetWidth || (o.outHeight >> scale) > targetHeight) {
            scale++;
        }
                 // native scaling
        //Find the correct scale value. It should be the power of 2.
        //final int REQUIRED_SIZE=70;
        //int width_tmp=o.outWidth, height_tmp=o.outHeight;
        //int scale=1;
        //while(true){
        //  if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
        //      break;
        //  width_tmp/=2;
        //  height_tmp/=2;
        //  scale*=2;
        //}

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        o2.inSampleSize = 1 << scale;
        FileInputStream stream2=new FileInputStream(f);
        Bitmap bitmap=BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

最终,我得到了我想要的结果。 我希望它对你也有所帮助。

干杯。