5

我有一个带有这个 getView 的适配器:

public View getView(int position, View convertView, ViewGroup parent) {
    Log.d("getView gv", position+"");

    NewsLine holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.grid_entry, parent, false);
        holder = new NewsLine();

        holder.iv= (ImageView) convertView.findViewById(R.id.photo);

        convertView.setTag(holder);
    } else {
        holder = (NewsLine) convertView.getTag();
    }

    NewsItem item=news.ITEMS.get(position);
    //---------

    if(item.imgurl!=null && item.imgurl.compareToIgnoreCase("null")!=0) 
    {
        holder.iv.setVisibility(View.VISIBLE);     
        mMemoryCache.loadBitmap(item.imgurl, holder.iv,position);
    }
    else
        holder.iv.setVisibility(View.INVISIBLE);
    //-------------




    return convertView;
}

我有两个问题:

  1. 对于位置 0,getView 会被多次调用(如果位图在 LruCache 中丢失,则使用 AsyncTask 下载位图)。我有一个动画(从 0 到 1 的 alpha),该位置会重新启动几次。

  2. 因为我正在回收视图,有时您可以在几分之一秒内看到旧的 imageView 内容。

//----

这是缓存类(仅堆):

public class SetImgAT extends LruCache<String, Bitmap> {
private static SetImgAT instance;   
private Animation FadeInAnimation;

private SetImgAT(int size, Context context) {
    super(size);
    FadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fadein);
}

public static synchronized SetImgAT getInstance(int size, Context context) {
    if (instance == null) {
        instance = new SetImgAT(size, context);
    }
    return instance;
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return (value.getRowBytes() * value.getHeight());
}

public void loadBitmap(String url, ImageView imageView,int pos) {
    Bitmap bitmap = instance.get(url.hashCode() + "");
    if (bitmap != null) {
        Log.d("ImageCache", "hit - "+url.hashCode()+"pos:"+pos);
        imageView.setImageBitmap(bitmap);

        imageView.invalidate();
    } else {
        Log.d("ImageCache", "miss");
        BitmapWorkerTask task = new BitmapWorkerTask(imageView);
        task.execute(url);
    }
}

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
    ImageView mImageView;

    public BitmapWorkerTask(ImageView imageView) {
        mImageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(String... url) {
        Bitmap Picture = null;


        if (url[0] != null && url[0].compareToIgnoreCase("null") != 0) {
            Log.d("GetBMP from", url[0]);

            URL img_value = null;
            try {
                img_value = new URL(url[0]);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                Picture = BitmapFactory.decodeStream(img_value
                        .openConnection().getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (Picture == null) {
                Log.d("deb", "no bitmap");
            } else {
                Log.d("got deb", "got bitmap to "+url[0].hashCode());                   
                instance.put(url[0].hashCode()+"", Picture);
            }

        }
        return Picture;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        // super.onPostExecute(result);
        if (result != null) {
            Log.d("deb", "set bitmap");
            mImageView.setImageBitmap(result);
            //mImageView.startAnimation(FadeInAnimation);
        }

    }
}

//----------------

}

谢谢!:)

4

1 回答 1

0

在来回滚动或随意调用 notifyDataSetChanged() 时,我看到了类似的行为。

作为对您现在正在做的事情的增强,我建议改用毕加索,因为它很好地处理了这种情况,除了动画的淡入淡出。

getView() 中的一个衬里:

Picasso.with(context).load(urlToLoad).into(imageView);

见: http ://square.github.io/picasso/

于 2014-04-28T21:23:38.713 回答