使用 ViewPropertyAnimator 淡入图像

Fade in image with ViewPropertyAnimator

我正在尝试使用 ViewPropertyAnimator 以可变持续时间淡入 ImageView,但我无法让它工作。

这是我用于淡出的代码,效果很好:

final ImageView imageView = (ImageView)mView.findViewById(R.id.image_view);
Picasso.with(mView.getContext()).load(mItem.thumbnailURL).into(imageView, new Callback() {
      @Override
      public void onSuccess() {
           imageView.animate().alpha(0).setDuration(duration).start();
      }
      ...
});

但是如果我尝试反转淡入的方向,图像永远不会显示:

final ImageView imageView = (ImageView)mView.findViewById(R.id.image_view);
imageView.setAlpha(0);

Picasso.with(mView.getContext()).load(mItem.thumbnailURL).into(imageView, new Callback() {
      @Override
      public void onSuccess() {
           imageView.animate().alpha(1).setDuration(duration).start();
      }
      ...
});

为什么 alpha 值永远不会增加?动画 运行 是否在与 setAlpha 不同的 alpha 通道上?

将弃用的 "setAlpha(int alpha)" 更改为 "setAlpha (float alpha)",它将起作用

imageView.setAlpha(0f);

使用View.setAlpha(),下面的代码或许可以帮助您理解。

追踪ViewPropertyAnimator中的源代码:

public ViewPropertyAnimator alpha(float value) {
    *animateProperty(ALPHA, value);*
    ...
}

然后,

private void animateProperty(int constantName, float toValue) {
    float fromValue = *getValue(constantName)*;
    ...
}

就是这样,

private float getValue(int propertyConstant) {
    final RenderNode node = mView.mRenderNode;
    switch (propertyConstant) {
        ...
        case ALPHA:
            return *mView.mTransformationInfo.mAlpha;*
    }
    return 0;
}

View.setAlpha()有关:

public void setAlpha(@FloatRange(float alpha) {
    ensureTransformationInfo();
    if (mTransformationInfo.mAlpha != alpha) {
        *setAlphaInternal(alpha);*
        ...
    }
}

ViewPropertyAnimator.getValue() 引用的属性相同:

private void setAlphaInternal(float alpha) {
    float oldAlpha = mTransformationInfo.mAlpha;
    *mTransformationInfo.mAlpha = alpha;*
    ...
}