加载联系人照片在ListView性能 [英] Load contact photo in a listview performance

查看:106
本文介绍了加载联系人照片在ListView性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示联系人的照片在ListView。我面临性能问题(滚动不顺畅),当有大量的联系人。我使用ArrayAdapter的getView方法来指定联系人的照片到ImageView的。当不使用时图像然后滚动平滑

但默认联系人应用程序的滚动非常流畅。所以在我的应用程序中的性能问题。

如何才能提高性能?请建议。

 私有类MyArrayListAdapter扩展ArrayAdapter< ContactsBook>实现OnItemClickListener {
    私人的ArrayList< ContactsBook> mContacts;
    私人语境mContext;


    公共MyArrayListAdapter(上下文的背景下,INT textViewResourceId,ArrayList的< ContactsBook>联系人){
        超(背景下,textViewResourceId,联系方式);
        mContacts =联系人;
        mContext =背景;
    }
    @覆盖
    公共查看getView(INT位置,查看converview,ViewGroup中父){
        查看查看= converview;
        ViewHolder viewHolder =新ViewHolder();
        如果(查看== NULL){
            LayoutInflater充气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            鉴于= inflater.inflate(R.layout.phone_row,NULL);
            viewHolder.tvName =(TextView中)view.findViewById(R.id.tvContact);
            viewHolder.tvPhoneNo =(TextView中)view.findViewById(R.id.tvPhoneNo);
            viewHolder.qcBadge =(QuickContactBadge)view.findViewById(R.id.qContact);
            view.setTag(viewHolder);
        }
        其他
            viewHolder =(ViewHolder)view.getTag();
        ContactsBook CB = mContacts.get(位置);
        如果(CB!= NULL){
            乌里contactPhotoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,cb.getContactIndex());
            BitmapDownloaderTask bdTask =新BitmapDownloaderTask(viewHolder.qcBadge);
            bdTask.execute(contactPhotoUri.toString());
            viewHolder.qcBadge.assignContactUri(con​​tactPhotoUri);
            viewHolder.qcBadge.setImageBitmap(framePhoto(BitmapFactory.de codeResource(getResources(),R.drawable.ic_contact_list_picture)));
            viewHolder.tvName.setText(getContactDisplayName(cb.getContactIndex()));
            viewHolder.tvPhoneNo.setText(getContactPhoneNo(cb.getContactIndex()));
        }
        返回查看;

    }
    类BitmapDownloaderTask扩展的AsyncTask<字符串,太虚,位图> {
        私人字符串URL;
        私人最终的WeakReference< ImageView的> imageViewReference;


        公共BitmapDownloaderTask(ImageView的ImageView的){
            imageViewReference =新的WeakReference< ImageView的>(ImageView的);
        }

        / **
         *实际下载方法。
         * /
        @覆盖
        受保护的位图doInBackground(字符串... PARAMS){
            网址= PARAMS [0];
            乌里conUri = Uri.parse(URL);
            InputStream的photoInputStream = Contacts.openContactPhotoInputStream(mContext.getContentResolver(),conUri);
            如果(photoInputStream == NULL)
                返回framePhoto(BitmapFactory.de codeResource(mContext.getResources(),R.drawable.ic_contact_list_picture));
            位图的照片= framePhoto(getPhoto(mContext.getContentResolver(),conUri));

            返回的照片;
        }
    / **
         *一旦图像被下载,它关联到的ImageView
         * /
        @覆盖
        保护无效onPostExecute(位图位图){
            如果(isCancelled()){
                位= NULL;
            }
            如果(imageViewReference!= NULL){
                ImageView的ImageView的= imageViewReference.get();
                imageView.setImageBitmap(位);
            }
        }
    }
 

解决方案

在创建的视图getView()你应该把一个通用的图标代替照片,然后启动AsyncTask的加载图像中的背景和返回getView()尽可能快。然后,当图像准备就绪(AsyncTask的后台任务完成后),你在加载的图像视图代替通用图标。

这样的图像可能不会立即显现,但滚动将是顺利的。

I want to display contact's photo in a ListView. I am facing performance problem(Scrolling is not smooth) when there is a large number of contacts. I am using ArrayAdapter's getView method to assign contact photo to ImageView. When image is not used then scrolling is smooth.

But the default contact application scrolling is very smooth. So there is a performance problem in my application.

How can I improve the performance? Please suggest.

private class MyArrayListAdapter extends ArrayAdapter<ContactsBook> implements OnItemClickListener{
    private ArrayList<ContactsBook> mContacts;
    private Context mContext;


    public MyArrayListAdapter(Context context, int textViewResourceId, ArrayList<ContactsBook> contacts) {
        super(context, textViewResourceId, contacts);
        mContacts= contacts;
        mContext=context;
    }
    @Override
    public View getView(int position, View converview, ViewGroup parent){
        View view=converview;
        ViewHolder viewHolder=new ViewHolder();
        if(view==null){
            LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.phone_row, null);
            viewHolder.tvName=(TextView)view.findViewById(R.id.tvContact);
            viewHolder.tvPhoneNo=(TextView)view.findViewById(R.id.tvPhoneNo);
            viewHolder.qcBadge=(QuickContactBadge)view.findViewById(R.id.qContact);
            view.setTag(viewHolder);
        }
        else
            viewHolder=(ViewHolder) view.getTag();
        ContactsBook cb=mContacts.get(position);
        if(cb!=null){
            Uri contactPhotoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,cb.getContactIndex());
            BitmapDownloaderTask bdTask=new BitmapDownloaderTask(viewHolder.qcBadge);
            bdTask.execute(contactPhotoUri.toString());
            viewHolder.qcBadge.assignContactUri(contactPhotoUri);
            viewHolder.qcBadge.setImageBitmap(framePhoto(BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_list_picture)));
            viewHolder.tvName.setText(getContactDisplayName(cb.getContactIndex()));
            viewHolder.tvPhoneNo.setText(getContactPhoneNo(cb.getContactIndex()));
        }
        return view;

    }
    class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        private String url;
        private final WeakReference<ImageView> imageViewReference;


        public BitmapDownloaderTask(ImageView imageView) {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        /**
         * Actual download method.
         */
        @Override
        protected Bitmap doInBackground(String... params) {
            url = params[0];
            Uri conUri=Uri.parse(url);
            InputStream photoInputStream=Contacts.openContactPhotoInputStream(mContext.getContentResolver(), conUri);
            if(photoInputStream==null)
                return framePhoto(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_contact_list_picture));
            Bitmap photo=framePhoto(getPhoto(mContext.getContentResolver(), conUri));

            return photo;
        }
    /**
         * Once the image is downloaded, associates it to the imageView
         */
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }
            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                imageView.setImageBitmap(bitmap);
            }
        }
    }

解决方案

When creating a view in getView() you should place a generic icon in place of photo, then start an AsyncTask to load the image in the background and return from getView() as fast as possible. Then when image is ready (AsyncTask background task is done) you replace the generic icon in the view with the loaded image.

This way images might not show immediately, but the scrolling will be smooth.

这篇关于加载联系人照片在ListView性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆