android – 结合CoverFlow和通用图像加载器

前端之家收集整理的这篇文章主要介绍了android – 结合CoverFlow和通用图像加载器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 fancyCoverFlowuniversalImageLoader显示我的自定义3D图库:D类似下图.我的问题是它没有在下载时显示图像,除非我在画廊图片之间滑动并且该图片在屏幕上隐藏并且在下次显示图像时出现但是在UniversalImageLoader的示例中,下载的图像在下载后立即显示.

这是我的适配器的getView代码

public View getView(int position,View view,ViewGroup parent) {


    RoundedImageView photo = (RoundedImageView) view;
    if (photo == null) {
        photo = (RoundedImageView) inflater.inflate(R.layout.row_gallery_latest_issue_item,parent,false);
    }
    try {
        System.out.println("Test is good");
        ImageLoaderHelper.configureCacheableImage(mContext,photo,latestBook.get(position).getImageUrl(),R.drawable.avatar_issue,null);
    } catch (NullPointerException e) {
        photo.setImageResource(R.drawable.avatar_issue);
        e.printStackTrace();
    }

    return createReflectedImages(photo);
}

它与UniversalImageLoader完全相同Sample预计我有一个TryCatche和CreateReflectedImage(它使我们的ImageView反射)

,还有一件我的ImageLoaderHelper:

public class ImageLoaderHelper {
public static void configureCacheableImage(Context context,ImageView imageView,String imageUrl,Integer defaultImageResourceId,ImageLoadingListener imageLoadingListener) {
    ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
    builder.displayer(
            new SimpleBitmapDisplayer())
            .cacheOnDisc(true)
            .cacheInMemory(true)
            .resetViewBeforeLoading(true)
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .bitmapConfig(Bitmap.Config.RGB_565);
    if (defaultImageResourceId != null)
        builder.showImageOnFail(defaultImageResourceId).showImageForEmptyUri(defaultImageResourceId).showStubImage(defaultImageResourceId);
    if (!imageLoader.isInited())
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    imageLoader.displayImage(imageUrl,imageView,builder.build(),imageLoadingListener);
}

}

更新:

经过一天的调试后,我找到了一条线索问题出在我的适配器上但是我不知道如何解决它!

这是CreateReflectedImages()的代码

public ImageView createReflectedImages(RoundedImageView image) {

    RoundedDrawable drawable = (RoundedDrawable) image.getDrawable();
    Bitmap originalImage = drawable.toBitmap();

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1,-1);

    Bitmap reflectionImage = Bitmap.createBitmap(originalImage,height / 2,width,matrix,false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width,(height + height / 2),Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);

    canvas.drawBitmap(originalImage,null);

    canvas.drawBitmap(reflectionImage,height,null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0,bitmapWithReflection.getHeight(),0x70ffffff,0x00ffffff,TileMode.CLAMP);

    paint.setShader(shader);

    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    canvas.drawRect(0,paint);

    RoundedImageView imageView = new RoundedImageView(mContext);
    imageView.setImageBitmap(bitmapWithReflection);
    imageView.setLayoutParams(new ImageGallery3D.LayoutParams(GeneralHelper.dp(180),GeneralHelper.dp(240)));//width and height of Image
    return imageView;
}

解决方法

问题是你有2个独立的imageview实例浮动. 1在getView中创建,第二个在createReflectedImage中创建.所以基本上,当图像完成下载时,新的图像视图已经替换它,并且下载的图像被加载到不再可见(或甚至可能存在)的图像中.它第二次正确加载(在屏幕外滚动后)的原因是延迟要低得多,因为它是从内存加载而不是从网上或任何地方加载的,所以当RoundedDrawable drawable =(RoundedDrawable)image.getDrawable();它实际上有你想要的图像,而不仅仅是占位符!
public View getView(int position,ViewGroup parent) {


    RoundedImageView photo = (RoundedImageView) view;
    ...
        /*
         *Passing first instance of photo into ImageLoaderHelper
         */

    ImageLoaderHelper.configureCacheableImage(mContext,null);
    ...
    //
    //Returns the new instance of RoundedImageView that ImageLoader is not aware of to the adapter
    //
    return createReflectedImages(photo);
}

public ImageView createReflectedImages(RoundedImageView image) {
    ...

    RoundedImageView imageView = new RoundedImageView(mContext);
    imageView.setImageBitmap(bitmapWithReflection);
    imageView.setLayoutParams(new ImageGallery3D.LayoutParams(GeneralHelper.dp(180),GeneralHelper.dp(240)));//width and height of Image

    //Returning a new instance of imageView
    return imageView;
}

而不是在createReflectedImages中返回imageView,而是返回“image”.然后实现一个回调,传递给ImageLoaderHelper,在成功加载图像后调用createReflectedImages来应用效果.

原文链接:https://www.f2er.com/android/317790.html

猜你在找的Android相关文章