Android PagerAdapter :: Get Current Item into bitmap

喜夏-厌秋 提交于 2019-12-08 04:30:27

问题


I am using UniversalImageLoader for my application. My ImageAdapter extends PagerAdapter. I need currentImage as Bitmap. By callback methood public void onLoadingComplete(Bitmap loadedImage) i am getting bitmap. But that is not that bitmap image currently showing. Either it is the next image or previous image (depending upon swaping). Here is the full code:

    private class ImagePagerAdapter extends PagerAdapter {
    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object instantiateItem(View view, int position) {
        if (view != null) {
            final View imageLayout = inflater.inflate(
                    R.layout.item_pager_image, null);
            final ImageView imageView = (ImageView) imageLayout
                    .findViewById(R.id.image_item_pager);

            imageLoader.displayImage(images[position], imageView, options,
                    new ImageLoadingListener() {

                        @Override
                        public void onLoadingStarted() {
                        }

                        @Override
                        public void onLoadingFailed(FailReason failReason) {
                            //On Loading Failed Logic                               
                        }

                        @Override
                        public void onLoadingComplete(Bitmap loadedImage) {
                                                        bitmap = loadedImage;  
                            imgViewSetAsWp.setImageBitmap(bitmap);
                        }

                        @Override
                        public void onLoadingCancelled() {
                            // Do nothing
                        }
                    });


            ((ViewPager) view).addView(imageLayout, 0);

            return imageLayout;
        } else
            return view;
    }

    @Override
    //Other override methood of ImagePager

}

Is there any way to get the proper bitmap.


回答1:


How to get current view - https://stackoverflow.com/a/8638772/913762

and then:

View currentView = ...
ImageView currentImageView = (ImageView) currentView.findViewById(...);
BitmapDrawable bd = (BitmapDrawable) currentImageView.getDrawable();
Bitmap bmp = bd.getBitmap();



回答2:


The problem comes when you inflate view: it's the same single instance that is used throughout the list, so when your asynchronous loading is complete, the image is changed when the viewpager is trying to paint a different item using the same imageView. Here is how I fix mine.

1)Create a class named ViewHolder

class ViewHolder {
    public ImageView image;
}

2) As soon as you finish inflating, create new ViewHolder object and bind it.

ViewHolder holder = new ViewHolder();
holder.image = (ImageView) imageLayout.findViewById(R.id.image_item_pager);

After that, you can use holder.image as normal ImageView.

Hope this help.



来源:https://stackoverflow.com/questions/14079638/android-pageradapter-get-current-item-into-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!