I am using UniversalImageLoader for my application. My ImageAdapte
r 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.
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();
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