Загрузить фотографию контакта в представлении списка

Я хочу отображать фотографию контакта в ListView. У меня проблема с производительностью (прокрутка не плавная) при большом количестве контактов. Я использую метод getView в ArrayAdapter для назначения фотографии контакта ImageView. Когда изображение не используется, прокрутка выполняется плавно.

Но прокрутка приложения контактов по умолчанию очень плавная. Итак, в моем приложении есть проблема с производительностью.

Как я могу улучшить производительность? Пожалуйста, предложите.

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);
            }
        }
    }
6
задан Ashok Kumar M 12 March 2011 в 04:41
поделиться