android– 毕加索在AsyncTask中生成的图像加载

android– 毕加索在AsyncTask中生成的图像加载,第1张

概述所以我试图使用PicassoLibrary进行图像下载和缓存.为了让contactUri传递给Picasso,我需要向ContactsContentProvider提出查询.因为我不想阻止主UI线程获取contactId,所以我把它放在AsyncTask中.一旦我得到那个contactId,我就会在AsyncTask的onPostExecute()方法中调用Picasso.

所以我试图使用Picasso Library进行图像下载和缓存.为了让contactUri传递给Picasso,我需要向Contacts Content ProvIDer提出查询.因为我不想阻止主UI线程获取contactID,所以我把它放在AsyncTask中.一旦我得到那个contactID,我就会在AsyncTask的onPostExecute()方法中调用Picasso.

但是,我注意到当我快速滚动ListVIEw时出现的闪烁.在我看来,VIEwHolder存在问题,因为回收视图在设置适当的图像之前显示上一个图像.反正有没有避免这个?

public class ConversationThreadsCursorAdapter extends SimpleCursorAdapter {    // region Constants    private static final int RECIPIENT_IDS_ColUMN_INDEX = 3;    private static final int ID2_ColUMN_INDEX = 0;    private static final int ADDRESS_ColUMN_INDEX = 1;    // endregion    // region Variables    private final String DEBUG_TAG = getClass().getSimplename().toString();    private Context mContext;    protected Drawable mDefaultPicDrawable;    protected ContentResolver mContentResolver;    protected linearLayout.LayoutParams mContactPicLayoutParams;    // endregion    // region Constructors    public ConversationThreadsCursorAdapter(Context context, int layout,    Cursor c, String[] from, int[] to, int flags) {        super(context, layout, c, from, to, flags);        mContext = context;        mDefaultPicDrawable = mContext.getResources().getDrawable(        R.drawable.ic_contact_picture);        mContactPicLayoutParams = new linearLayout.LayoutParams(        mDefaultPicDrawable.getIntrinsicWIDth(),        mDefaultPicDrawable.getIntrinsicHeight());    }    // endregion    public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {        VIEwHolder vIEwHolder = null;        if (convertVIEw == null) {            convertVIEw = mLayoutInflater.inflate(R.layout.simple_message, null);            // Creates a VIEwHolder and store references to the children            // vIEws we want to bind data to.            vIEwHolder = setupVIEwHolder(convertVIEw);            convertVIEw.setTag(vIEwHolder);        } else {            // Get the VIEwHolder back to get fast access to the TextVIEw            // and the ImageVIEw.            vIEwHolder = (VIEwHolder) convertVIEw.getTag();            vIEwHolder.task.cancel(true);        }        mCursor = getCursor();        mCursor.movetoposition(position);        vIEwHolder.position = position;        String recipIEnt_IDs = mCursor.getString(RECIPIENT_IDS_ColUMN_INDEX);        String[] recipIEnts = recipIEnt_IDs.split(" ");        vIEwHolder.task = new AddressFetcherTask(vIEwHolder, position);        vIEwHolder.task.execute(recipIEnts);         return convertVIEw;    }    // region Helper Methods    private VIEwHolder bindUIElements(VIEw convertVIEw) {        VIEwHolder vIEwHolder = new VIEwHolder();        vIEwHolder.contactBadge = (QuickContactBadge) convertVIEw.findVIEwByID(R.ID.contact_pic);        return vIEwHolder;    }    private VIEwHolder setupVIEwHolder(VIEw convertVIEw) {        VIEwHolder vIEwHolder = bindUIElements(convertVIEw);        vIEwHolder.contactBadge.setLayoutParams(mContactPicLayoutParams);        return vIEwHolder;    }    // endregion    // region Inner Classes    private class VIEwHolder {        QuickContactBadge contactBadge;        int position;    }    private class AddressFetcherTask extends AsyncTask < String[], VoID, Integer > {        private VIEwHolder mVIEwHolder;        private int mposition;        public AddressFetcherTask(VIEwHolder vIEwHolder, int position) {            mVIEwHolder = vIEwHolder;            mposition = position;        }        @OverrIDe        protected Integer doInBackground(String[]...recipIEnts) {            String recipIEnt = recipIEnts[0][0];            Log.d(DEBUG_TAG, "recipIEnt is " + recipIEnt);            Cursor c = mContentResolver.query(            Uri.parse("content://mms-sms/canonical-addresses"), null, "_ID = " + recipIEnt, null, null);            String _ID = "";            String address = "";            while (c.movetoNext()) {                _ID = c.getString(ID2_ColUMN_INDEX);                address = c.getString(ADDRESS_ColUMN_INDEX);            }            c.close();            int contactID;            if (address != null) {                contactID = ContactsUtils.getContactID(mContext, address, "address");            } else {                contactID = Integer.valueOf(address);            }            return contactID;        }        @OverrIDe        protected voID onPostExecute(Integer contactID) {            if (mVIEwHolder.position == mposition) {                Picasso.with(mContext)                    .load(getContactUri(contactID))                    .placeholder(R.drawable.ic_contact_picture)                    .into(mVIEwHolder.contactBadge);            }        }    }    // endregion}

解决方法:

只需在getVIEw中将imagevIEw设置为null,它就应该删除你正在经历的大部分内容.

另一个微小的小角落情况是,当你的asynctask到达postExecute时,视图可能仍然存在,但它可能已被分配了一个不同的联系人来加载(它已被回收).

您需要在视图中放置某种标记,然后在postexecute中设置它时检查它是否仍然相同.

要删除淡入,您需要从getvIEw中删除asynctask.您需要能够在getvIEw中调用picasso,这意味着在到达getvIEw之前准备好数据.

下面,不太确定它是否会编译,我已经在文本编辑器中完成了.

但是低音我正在缓存mCachedContactIDs中的结果,如果需要一个新表,只需重新加载整个表.我通常发现这很强大.但你也可以调用我已经注释掉的毕加索代码

public class ConversationThreadsCursorAdapter extends SimpleCursorAdapter {// region Constantsprivate static final int RECIPIENT_IDS_ColUMN_INDEX = 3;private static final int ID2_ColUMN_INDEX = 0;private static final int ADDRESS_ColUMN_INDEX = 1;private HashMap<String, Integer> mCachedContactIDs = new HashMap<String, Integer>();// endregion// region Variablesprivate final String DEBUG_TAG = getClass().getSimplename().toString(); private Context mContext;protected Drawable mDefaultPicDrawable;protected ContentResolver mContentResolver;protected linearLayout.LayoutParams mContactPicLayoutParams;// endregion// region Constructorspublic ConversationThreadsCursorAdapter(Context context, int layout,Cursor c, String[] from, int[] to, int flags) {    super(context, layout, c, from, to, flags);    mContext = context;    mDefaultPicDrawable = mContext.getResources().getDrawable(    R.drawable.ic_contact_picture);    mContactPicLayoutParams = new linearLayout.LayoutParams(    mDefaultPicDrawable.getIntrinsicWIDth(),    mDefaultPicDrawable.getIntrinsicHeight());}// endregionpublic VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) {    VIEwHolder vIEwHolder = null;    if (convertVIEw == null) {        convertVIEw = mLayoutInflater.inflate(R.layout.simple_message, null);            // Creates a VIEwHolder and store references to the children        // vIEws we want to bind data to.        vIEwHolder = setupVIEwHolder(convertVIEw);          convertVIEw.setTag(vIEwHolder);    } else {        // Get the VIEwHolder back to get fast access to the TextVIEw        // and the ImageVIEw.        vIEwHolder = (VIEwHolder) convertVIEw.getTag();        vIEwHolder.task.cancel(true);        vIEwHolder.contactBadge.setimageDrawable(mDefaultPicDrawable);    }    mCursor = getCursor();    mCursor.movetoposition(position);    vIEwHolder.position = position;    String recipIEnt_IDs = mCursor.getString(RECIPIENT_IDS_ColUMN_INDEX);    String[] recipIEnts = recipIEnt_IDs.split(" ");    String recipIEnt = recipIEnts[0];    if(mCachedContactIDs.get(recipIEnt) != null){        Picasso.with(mContext)            .load(getContactUri(mCachedContactIDs.get(recipIEnt)))            .placeholder(R.drawable.ic_contact_picture)            .into(mVIEwHolder.contactBadge);                     } else {        vIEwHolder.task = new AddressFetcherTask(vIEwHolder, position);        vIEwHolder.task.execute(recipIEnts);     }    return convertVIEw;    }// region Helper Methodsprivate VIEwHolder bindUIElements(VIEw convertVIEw) {    VIEwHolder vIEwHolder = new VIEwHolder();        vIEwHolder.contactBadge = (QuickContactBadge) convertVIEw.findVIEwByID(R.ID.contact_pic);    return vIEwHolder;}private VIEwHolder setupVIEwHolder(VIEw convertVIEw) {    VIEwHolder vIEwHolder = bindUIElements(convertVIEw);        vIEwHolder.contactBadge.setLayoutParams(mContactPicLayoutParams);    return vIEwHolder;}// endregion// region Inner Classesprivate class VIEwHolder {    QuickContactBadge contactBadge;    int position;    AddressFetcherTask task;}private class AddressFetcherTask extends AsyncTask < String[], VoID, Integer > {    private VIEwHolder mVIEwHolder;    private int mposition;    private String mRecipIEnt;    public AddressFetcherTask(VIEwHolder vIEwHolder, int position) {        mVIEwHolder = vIEwHolder;        mposition = position;    }    @OverrIDe    protected Integer doInBackground(String[]...recipIEnts) {        mRecipIEnt = recipIEnts[0][0];        Log.d(DEBUG_TAG, "recipIEnt is " + recipIEnt);        Cursor c = mContentResolver.query(        Uri.parse("content://mms-sms/canonical-addresses"), null, "_ID = " + mRecipIEnt, null, null);        String _ID = "";        String address = "";        while (c.movetoNext()) {            _ID = c.getString(ID2_ColUMN_INDEX);            address = c.getString(ADDRESS_ColUMN_INDEX);        }        c.close();        int contactID;        if (address != null) {            contactID = ContactsUtils.getContactID(mContext, address, "address");        } else {            contactID = Integer.valueOf(address);        }        return contactID;    }    @OverrIDe    protected voID onPostExecute(Integer contactID) {                                   if (mVIEwHolder.position == mposition) {            mCachedContactIDs.put(mRecipIEnt, contactID);            Picasso.with(mContext)                .load(getContactUri(mCachedContactIDs.get(recipIEnt)))                .placeholder(R.drawable.ic_contact_picture)                .into(mVIEwHolder.contactBadge);        }                   }}  // endregion}

或者,如果所有这些让你感到困扰的是毕加索的淡出,那么在请求中添加noFade().

总结

以上是内存溢出为你收集整理的android – 毕加索在AsyncTask中生成的图像加载全部内容,希望文章能够帮你解决android – 毕加索在AsyncTask中生成的图像加载所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103403.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存