为什么淡入淡出过渡不适用于共享元素
Why Fade transition doesn't work on Shared Element
我有 2 个 Activity,共享元素转换工作正常。ChangeBounds 是唯一应用的转换。
我想在共享元素移动时应用淡入淡出过渡,所以顺序是 ORDERING_TOGETHER。
public class TransitionUtils {
public static Transition makeSharedElementEnterTransition(final Context context, final long duration) {
TransitionSet set = new TransitionSet();
set.setOrdering(TransitionSet.ORDERING_TOGETHER);
set.setDuration(duration);
Transition changeBounds = new ChangeBounds();
changeBounds.addTarget(context.getString(R.string.transition_name_search_text));
set.addTransition(changeBounds);
Transition fade = new Fade(Fade.OUT);
fade.addTarget(context.getString(R.string.transition_name_search_text));
set.addTransition(fade);
return set;
}
}
startActivity调用ActivityOptions.makeSceneTransitionAnimation
在 EndActivity 中,设置了进入共享元素转换
public class EndActivity extends Activity{
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blabla);
getWindow().setSharedElementEnterTransition(TransitionUtils.makeSharedElementEnterTransition(this,2000));
}
}
注意:我注意到
- Fade()常应用于getWindow().setEnterTransition()
- 设置一个
TransitionSet 的持续时间适用于包含的所有 Transitions
除了 Fade.
如何将 Fade Transition 应用到 sharedElement ?
我做错了什么?
android.transition.Fade 使用 TransitionAlpha ,在我的 IDE 中无法解析。 android.transition.Fade 适用于活动之间的进入和退出转换。
所以我创建了自己的 Fade 来使用 Alpha。 Android 视图的不透明度由 alpha 设置。共享元素使用 View.
你这样称呼它:
Transition fadeOut = new FadeTransition(1f, 0f, new LinearInterpolator());
fadeOut.addTarget(transitionName);
完整代码在这里
@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;
}
}
我有 2 个 Activity,共享元素转换工作正常。ChangeBounds 是唯一应用的转换。
我想在共享元素移动时应用淡入淡出过渡,所以顺序是 ORDERING_TOGETHER。
public class TransitionUtils {
public static Transition makeSharedElementEnterTransition(final Context context, final long duration) {
TransitionSet set = new TransitionSet();
set.setOrdering(TransitionSet.ORDERING_TOGETHER);
set.setDuration(duration);
Transition changeBounds = new ChangeBounds();
changeBounds.addTarget(context.getString(R.string.transition_name_search_text));
set.addTransition(changeBounds);
Transition fade = new Fade(Fade.OUT);
fade.addTarget(context.getString(R.string.transition_name_search_text));
set.addTransition(fade);
return set;
}
}
startActivity调用ActivityOptions.makeSceneTransitionAnimation
在 EndActivity 中,设置了进入共享元素转换
public class EndActivity extends Activity{
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blabla);
getWindow().setSharedElementEnterTransition(TransitionUtils.makeSharedElementEnterTransition(this,2000));
}
}
注意:我注意到
- Fade()常应用于getWindow().setEnterTransition()
- 设置一个 TransitionSet 的持续时间适用于包含的所有 Transitions 除了 Fade.
如何将 Fade Transition 应用到 sharedElement ? 我做错了什么?
android.transition.Fade 使用 TransitionAlpha ,在我的 IDE 中无法解析。 android.transition.Fade 适用于活动之间的进入和退出转换。
所以我创建了自己的 Fade 来使用 Alpha。 Android 视图的不透明度由 alpha 设置。共享元素使用 View.
你这样称呼它:
Transition fadeOut = new FadeTransition(1f, 0f, new LinearInterpolator());
fadeOut.addTarget(transitionName);
完整代码在这里
@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;
}
}