1

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

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

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值永远不会增加?动画是否在不同的 Alpha 通道上运行setAlpha

4

2 回答 2

1

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

imageView.setAlpha(0f);
于 2016-09-02T00:50:59.007 回答
0

使用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;*
    ...
}
于 2018-09-26T07:20:31.413 回答