Why Fade transition doesn't work on Shared Element

落爺英雄遲暮 提交于 2019-12-01 09:17:00

android.transition.Fade uses TransitionAlpha , which cannot be resolved in my IDE. android.transition.Fade works for enter and exit transition between activities.

So I created my own Fade to use Alpha. An Android view's opacity is set by alpha. And shared element uses View.

You call it like this :

Transition fadeOut = new FadeTransition(1f, 0f, new LinearInterpolator());
fadeOut.addTarget(transitionName);

The full code is here

@TargetApi(21)
public class FadeTransition extends Transition {

    private static final String PROPNAME_BACKGROUND = "android:faderay:background";
    private static final String PROPNAME_TEXT_COLOR = "android:faderay:textColor";
    private static final String PROPNAME_ALPHA = "android:faderay:alpha";

    private float startAlpha;
    private float endAlpha;
    private TimeInterpolator timeInterpolator;

    public FadeTransition(final float startAlpha, final float endAlpha, final TimeInterpolator timeInterpolator) {
        this.startAlpha = startAlpha;
        this.endAlpha = endAlpha;
        this.timeInterpolator = timeInterpolator;
    }

    public FadeTransition(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    private void captureValues(final TransitionValues transitionValues) {
        transitionValues.values.put(PROPNAME_BACKGROUND, transitionValues.view.getBackground());
        transitionValues.values.put(PROPNAME_ALPHA, transitionValues.view.getAlpha());
        if (transitionValues.view instanceof TextView) {
            transitionValues.values.put(PROPNAME_TEXT_COLOR, ((TextView) transitionValues.view).getCurrentTextColor());
        }
    }

    @Override
    public void captureStartValues(final TransitionValues transitionValues) {
        captureValues(transitionValues);
    }

    @Override
    public void captureEndValues(final TransitionValues transitionValues) {
        captureValues(transitionValues);
    }

    @SuppressLint("NewApi")
    @Override
    public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues,
            final TransitionValues endValues) {

        TextView textView = (TextView) endValues.view;

        if (startAlpha != endAlpha) {
            textView.setAlpha(endAlpha);
        }

        ObjectAnimator fade = ObjectAnimator.ofFloat(textView, View.ALPHA, startAlpha, endAlpha);
        fade.setInterpolator(timeInterpolator);
        return fade;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!