CustomListView:滚动ListView时图像正在改变

时间:2015-03-04 09:06:44

标签: android listview android-listview android-asynctask


我知道这个问题被多次询问..
但是我无法得到任何正确的解决方案。在我的应用程序中,我使用AsyncTask来加载图像。
我使用AsyncTask因为我想要平滑滚动listview ..并且在实现AsyncTask之后我的listView顺利滚动..
但现在问题是图像在滚动后正在改变.. 。
我该怎么办?
MyAdapter班级

package com.example.adapter;
public class DisplayAllFeedAdapter extends ArrayAdapter<DisplayAllFeedItem> {
    private final List<DisplayAllFeedItem> list;
    private final Activity context;
    ExifInterface exif;

    public DisplayAllFeedAdapter(Activity context, List<DisplayAllFeedItem> list) {
        super(context, R.layout.feed_screen, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        public TextView optionalDesc, reportedBy;
        public LinearLayout layout;
        public ImageView displayFeedimg, channelIcon;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.display_all_feed_listitem, null);
            viewHolder = new ViewHolder();

            viewHolder.optionalDesc = (TextView) convertView.findViewById(R.id.txtFeedOptionalDesc);
            viewHolder.reportedBy = (TextView) convertView.findViewById(R.id.txtFeedReportedBy);
            viewHolder.displayFeedimg = (ImageView) convertView
                    .findViewById(R.id.imgFeedDisplayImage);
            viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layoutChannelImgView);

            convertView.setTag(viewHolder);

            convertView.setTag(R.id.txtFeedOptionalDesc, viewHolder.optionalDesc);
            convertView.setTag(R.id.txtFeedReportedBy, viewHolder.reportedBy);
            convertView.setTag(R.id.imgFeedDisplayImage, viewHolder.displayFeedimg);
            convertView.setTag(R.id.layoutChannelImgView, viewHolder.layout);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String temp;
        String drawableImagePath = list.get(position).getMediaChannelName();
        List<String> channelList = new ArrayList<String>();
        while (drawableImagePath.length() > 0) {

            if (drawableImagePath.indexOf(",") == -1) {
                temp = drawableImagePath.substring(0);
                channelList.add(temp);
                break;
            } else {
                temp = drawableImagePath.substring(0, drawableImagePath.indexOf(","));
            }
            channelList.add(temp);
            drawableImagePath = drawableImagePath.substring(drawableImagePath.indexOf(",") + 1);
        }
        viewHolder.layout.removeAllViews();

        for (int i = 0; i < channelList.size(); i++) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dpToPx(30),
                    dpToPx(30));
            ImageView imageView = new ImageView(context);
            layoutParams.setMargins(0, 0, 10, 0);
            imageView.setLayoutParams(layoutParams);
            imageView.setImageResource(Integer.parseInt(channelList.get(i)));
            viewHolder.layout.addView(imageView);
        }

        String tempPath = list.get(position).getMediaPath();
        File mediaFile = new File(tempPath);
        Bitmap bitmap;

            viewHolder.optionalDesc.setText(list.get(position).getMediaDesc());
        viewHolder.reportedBy.setText("Reported By " + list.get(position).getMediaDisplayName());
        if (viewHolder != null) {
            new LoadImage(viewHolder).execute(tempPath);
        }
        return convertView;
    }

    private class LoadImage extends AsyncTask<String, String, Bitmap> {

        // File mediaFile = new File(tempPath);
        Bitmap bitmap, displayBitmap;
        File mediaFile;
        String mediaPath;
        private final ViewHolder imageViewReference;

        public LoadImage(ViewHolder viewHolder) {
            imageViewReference = viewHolder;
        }

        @Override
        protected Bitmap doInBackground(String... params) {

            // File f = new File(params[0]);
            mediaPath = params[0];

            mediaFile = new File(mediaPath);
            if (mediaFile.exists()) {
                if (isImage(mediaPath)) {
                    Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
                    int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
                    Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
                    int rotate = 0;

                    try {
                        exif = new ExifInterface(mediaFile.getAbsolutePath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                            ExifInterface.ORIENTATION_UNDEFINED);
                    switch (orientation) {
                    case ExifInterface.ORIENTATION_NORMAL:
                        rotate = 0;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                    }

                    Matrix matrix = new Matrix();
                    matrix.postRotate(rotate);
                    displayBitmap = Bitmap.createBitmap(scale, 0, 0, scale.getWidth(),
                            scale.getHeight(), matrix, true);

                } else {
                    displayBitmap = ThumbnailUtils.createVideoThumbnail(mediaPath,
                            Thumbnails.MICRO_KIND);

                }

            }
            return displayBitmap;

        }

        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (imageViewReference != null) {
                imageViewReference.displayFeedimg.setImageBitmap(result);

            }
            // tempView.displayFeedimg.setImageBitmap(result);

        }
    }

    public int dpToPx(int dp) {
        DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
        int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
        return px;
    }

    public static boolean isImage(String str) {
        boolean temp = false;
        String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
        for (int i = 0; i < arr.length; i++) {
            temp = str.endsWith(arr[i]);
            if (temp) {
                break;
            }
        }
        return temp;
    }

}

2 个答案:

答案 0 :(得分:0)

我不清楚您的问题(需要更好的澄清),但我知道在加载图像时存在性能滞后的问题。

请查看Google的网络Making a Standard Request。 搜索(通用)图像加载器,文本“使用ImageLoader和NetworkImageView”。 您可以通过位于该文本下方的缓存来进一步提高性能。

网页上的代码段:

ImageLoader mImageLoader;
ImageView mImageView;
// The URL for the image that is being loaded.
private static final String IMAGE_URL =
    "http://developer.android.com/images/training/system-ui.png";
...
mImageView = (ImageView) findViewById(R.id.regularImageView);

// Get the ImageLoader through your singleton class.
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,
         R.drawable.def_image, R.drawable.err_image));

知识渊博的作者在Solving the Android image loading problem...中似乎有一个很好的教程。 玩得开心。总有一天我可能也要解决这个问题。请告诉我们。

答案 1 :(得分:0)

除了我之前的回答,还有另一种可能的解决方案。但它与谷歌的推荐有所不同,所以要小心......

查看Stackoverflow讨论ListView like in universal image loader sample app。该页面中的结帐代码。在CarListAdapter中:

ImageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));

......在另一种方法中:

imageLoader.displayImage(...);

我的两个答案都导致了一点,使用(通用)图像加载器。