2

In my Android application an Asyntask is used for loading images from web in a list view.I want to show the center portion of the image and I have tried imageloader for loading images but the problem is image clarity is very poor but I have used it for loading thumbnails because thumbnail is very small.So that I have tried an Asyntask which is given below.The problem with this method is image view shows other images that means image view in first list item will show the next list item's image and after some time it will display the correct image.How can I solve this issue.Or suggest a method for lading images with good clarity.Please help me.Thanks in advance

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    ProgressBar mProgressBar;
    String url;

    public DownloadImageTask(ImageView bmImage, ProgressBar progressBar) {
        this.bmImage = bmImage;
        this.mProgressBar = progressBar;
    }

    @Override
    protected void onPreExecute() {
        mProgressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        url = urldisplay;
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            // Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        Utilities.addBitmapToMemoryCache(url, result);
        bmImage.setImageBitmap(result);
        mProgressBar.setVisibility(View.GONE);
        // result.recycle();
    }
}

}

public View getView(final int position, View convertView, ViewGroup parent) {
    vi = convertView;
    int type = getItemViewType(position);
    // Utilities.message_player = new MediaPlayer();
    if (vi == null) {
        inflater = LayoutInflater.from(mcontext);
        if (type == ITEM_TYPE_ONE)
            vi = inflater.inflate(R.layout.message_group_list_item, null);
        else
            vi = inflater.inflate(R.layout.listlastrow, null);
    }
    if (type == ITEM_TYPE_ONE) {
        if (fontType == true) {

        }
        ((ImageView) vi.findViewById(R.id.imageMessage)).setTag(position+"i");

Bitmap bmp = Utilities.getBitmapFromMemCache(feedsImage);
                    if(bmp == null){
                    DownloadImageTask dwnloadImgTask = new DownloadImageTask(((ImageView) vi.findViewWithTag(position+"i")),
                            ((ProgressBar) vi.findViewById(R.id.progressBar1)));
                    dwnloadImgTask.execute(feedsImage);
                    }else{
                        ((ImageView) vi.findViewById(R.id.imageMessage)).setImageBitmap(bmp);
                    }

}
}
4

1 回答 1

2

当您从缓存设置位图或下载新图像时,您会丢失一些东西:

您的父视图 ( vi ) 可能是以前创建的视图,该视图被回收以避免重新创建每个视图(即convertView的用途)。

这个 convertView 上面已经有一个图像,因为它是一个已加载图像的回收项目。

所以你必须重置图像视图内容,以防你有一个 convertView :

    ((ImageView) vi.findViewById(R.id.imageMessage)).setTag(position+"i");

    Bitmap bmp = Utilities.getBitmapFromMemCache(feedsImage);
    if(bmp == null){
        //HERE I add a line to reset the bitmap of the imageview
        ((ImageView) vi.findViewById(R.id.imageMessage)).setImageBitmap(null);

        DownloadImageTask dwnloadImgTask = new DownloadImageTask(((ImageView)    vi.findViewWithTag(position+"i")), ((ProgressBar) vi.findViewById(R.id.progressBar1)));
        dwnloadImgTask.execute(feedsImage);
    }else{
        ((ImageView) vi.findViewById(R.id.imageMessage)).setImageBitmap(bmp);
    }

我已设置null删除位图,但您可能想设置一个占位符位图(例如带有重新加载图标的灰色图片或类似的东西......)

希望有帮助。

于 2013-07-18T10:59:43.270 回答