使用.asBitmap()加载图像时淡出动画不起作用

时间:2017-05-07 14:11:51

标签: android android-animation android-imageview android-glide

我有这段代码

Glide
            .with(this)
            .load(mUrl)
            .asBitmap()
            .thumbnail(Glide
                    .with(this)
                    .load(mPreviewUrl)
                    .asBitmap()
                    .animate(R.anim.fade_in))
            .listener(new RequestListener<String, Bitmap>() {
                @Override
                public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                    mProgressBar.setVisibility(View.GONE);
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    mProgressBar.setVisibility(View.GONE);
                    return false;
                }
            })
            .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    mWallpaperImageView.setImageBitmap(resource);
                    createPaletteAsync(resource);
                }
            });

这会下载一个图像并返回一个Bitmap对象,然后我用它来生成Palette库的颜色调色板。我还想在ImageView中加载位图,然后使用mWallpaperImageView.setImageBitmap(resource)加载图像,加载图像时不会有任何淡入淡出或动画来平滑加载。

如果我像这样滑行:

Glide.with(this)
            .load(mUrl)
            .crossFade(500)
            .into(mImageView)

然后图像显示为褪色,但之后我没有Bitmap个对象。

2 个答案:

答案 0 :(得分:2)

根据this answer

  

不幸的是,没有内置的方法来交叉淡入淡出的位图。您   但是,可以使用自定义BitmapImageViewTarget,并使用   onResourceReady()中的TransitionDrawable交叉渐变。

幸运的是,启用fading effect with bitmap in Glide是一个技巧。

为此你需要两个班级,

<强> FadingDrawable

final public class FadingDrawable extends BitmapDrawable {
    // Only accessed from main thread.
    private static final float FADE_DURATION = 200; //ms
    private final float density;
    Drawable placeholder;
    long startTimeMillis;
    boolean animating;
    int alpha = 0xFF;

    FadingDrawable(Context context, Bitmap bitmap, Drawable placeholder) {
        super(context.getResources(), bitmap);

        this.density = context.getResources().getDisplayMetrics().density;

        this.placeholder = placeholder;
        animating = true;
        startTimeMillis = SystemClock.uptimeMillis();
    }

    /**
     * Create or update the drawable on the target {@link android.widget.ImageView} to display the supplied bitmap
     * image.
     */
    static public void setBitmap(ImageView target, Context context, Bitmap bitmap) {
        if (bitmap != null && !bitmap.isRecycled()) {
            Drawable placeholder = target.getDrawable();
            if (placeholder instanceof AnimationDrawable) {
                ((AnimationDrawable) placeholder).stop();
            }
            FadingDrawable drawable = new FadingDrawable(context, bitmap, placeholder);

            //this will avoid OverDraw
            //target.setBackgroundDrawable(null);
            //target.setBackgroundColor(0);

            target.setImageDrawable(drawable);

        }
    }

    /**
     * Create or update the drawable on the target {@link android.widget.ImageView} to display the supplied
     * placeholder image.
     */
    static void setPlaceholder(ImageView target, Drawable placeholderDrawable) {
        target.setImageDrawable(placeholderDrawable);
        if (target.getDrawable() instanceof AnimationDrawable) {
            ((AnimationDrawable) target.getDrawable()).start();
        }
    }

    @Override
    public void draw(Canvas canvas) {
        if (!animating) {
            super.draw(canvas);
        } else {
            float normalized = (SystemClock.uptimeMillis() - startTimeMillis) / FADE_DURATION;
            if (normalized >= 1f) {
                animating = false;
                placeholder = null;
                super.draw(canvas);
            } else {
                if (placeholder != null) {
                    placeholder.draw(canvas);
                }

                int partialAlpha = (int) (alpha * normalized);
                super.setAlpha(partialAlpha);
                super.draw(canvas);
                super.setAlpha(alpha);
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
                    invalidateSelf();
                }
            }
        }


    }

    @Override
    public void setAlpha(int alpha) {
        this.alpha = alpha;
        if (placeholder != null) {
            placeholder.setAlpha(alpha);
        }
        super.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        if (placeholder != null) {
            placeholder.setColorFilter(cf);
        }
        super.setColorFilter(cf);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        if (placeholder != null) {
            placeholder.setBounds(bounds);
        }
        super.onBoundsChange(bounds);
    }
}

GlideImageView

public class GlideImageView extends AppCompatImageView {
    public GlideImageView(Context context) {
        this(context, null);
    }

    public GlideImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GlideImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Drawable placeholder = getDrawable();
        if (placeholder instanceof AnimationDrawable) {
            ((AnimationDrawable) placeholder).stop();
            Glide.clear(this);
        }
    }

    @Override
    public void setImageBitmap(Bitmap bitmap) {
        if (bitmap != null) FadingDrawable.setBitmap(this, getContext(), bitmap);
    }

    public void setImageBitmapWithoutAnimation(Bitmap bitmap) {
        super.setImageBitmap(bitmap);
    }
}

现在将 mWallpaperImageView ImageView更改为

<com.yourpackage.GlideImageView
    android:id="@+id/mWallpaperImageView" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

最后从Glide配置中删除所有外部动画效果

Glide
     .with(this)
     .load(mUrl)
     .asBitmap()
     .thumbnail(Glide
                    .with(this)
                    .load(mPreviewUrl)
                    .asBitmap()
            .listener(new RequestListener<String, Bitmap>() {
                @Override
                public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                    mProgressBar.setVisibility(View.GONE);
                    return false;
                }

                @Override
                public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    mProgressBar.setVisibility(View.GONE);
                    return false;
                }
            })
            .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    mWallpaperImageView.setImageBitmap(resource);
                    createPaletteAsync(resource);
                }
            }); 

经过测试并正常工作!

答案 1 :(得分:0)

您可以使用图像的alpha值来创建淡入或淡出效果:

在Java中

$dom =new DOMDocument;
$dom->loadHTMLFile("my.html");
$xp = new DOMXPath($dom);

$nodeList = $xp->query('//li[starts-with(@id, "id_")]');

foreach ($nodeList as $node) {
    $result[$node->getAttribute('id')] = $node->nodeValue;
}

print_r($result);

假设图像的alpha以alpha = 1(不透明)开头。 所以图像会在2秒内消失(透明)

相关问题