我想为ConstraintLayout中的Scale View元素创建自定义过渡。在AutoTransition.java
内部,我们有淡入、调整边界和淡出。因此,执行简单的缩放不会动画化更改。
经过一些解决方法,我能够创建具有缩放功能的自定义过渡。我的目标是创建带有可见性和缩放的过渡集。通过下面的更改,这个动画可以很好地工作,尽管缩放枢轴可能会发生变化。
//..................... General method from Custom Scale Transition
@Nullable
@Override
public Animator createAnimator(@NonNull ViewGroup sceneRoot, @Nullable TransitionValues startValues,
@Nullable TransitionValues endValues) {
if (null == startValues || null == endValues) {
return null;
}
final View view = endValues.view;
final float startX = (float) startValues.values.get(PROPNAME_SCALE_X);
final float endX = (float) endValues.values.get(PROPNAME_SCALE_X);
final float startY = (float) startValues.values.get(PROPNAME_SCALE_Y);
final float endY = (float) endValues.values.get(PROPNAME_SCALE_Y);
final AnimatorSet set = new AnimatorSet();
final ValueAnimator animatorX = ValueAnimator.ofFloat(startX, endX);
animatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final float value = (float) animation.getAnimatedValue();
view.setScaleX(value);
}
});
final ValueAnimator animatorY = ValueAnimator.ofFloat(startY, endY);
animatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final float value = (float) animation.getAnimatedValue();
view.setScaleY(value);
}
});
set.playTogether(animatorX, animatorY);
return set;
}
//..................... Complete Set Transitions
public class ScaleVisibility extends TransitionSet {
public ScaleVisibility() {
setOrdering(ORDERING_TOGETHER);
addTransition(new Fade()).
addTransition(new Scale());
}
}
//..................... Code for starting Animation.
mConstraintSet.clone(mConstraintLayout);
Transition transition = new ScaleVisibility();
transition.setDuration(3000);
mConstraintSet.setScaleX(item.resource, item.scale);
mConstraintSet.setScaleY(item.resource, item.scale);
mConstraintSet.setTransformPivot(item.resource, 0.5f, 0.5f);
mConstraintSet.setVisibility(item.resource, item.visibility);
TransitionManager.beginDelayedTransition(mConstraintLayout, transition);
mConstraintSet.applyTo(mConstraintLayout);
//..................... Main XML. Trying to Animate input_send_button
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_input_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/input_background"
android:layout_width="match_parent"
android:layout_height="@dimen/chat_input_panel_height"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent"/>
<!--This View will change Visibility with Text Changes-->
<ImageButton
android:id="@+id/input_send_button"
android:layout_width="@dimen/chat_input_panel_height"
android:layout_height="0dp"
android:background="@android:color/transparent"
android:src="@drawable/chat_send"
android:tint="@color/primary"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/input_background"
app:layout_constraintRight_toRightOf="@+id/input_background"
app:layout_constraintTop_toTopOf="@+id/input_background"
tools:ignore="ContentDescription"
tools:visibility="visible"/>
</android.support.constraint.ConstraintLayout>
主要问题在于缩放动画的中心点是零(视图左上角)。我无法更改动画视图的过渡中心点。无论是将此属性添加到View.java
还是ConstraintSet.java
参数中都不行。因此,问题是如何为此缩放转换更改视图中心点。(作为附加问题,我也想知道如何更改默认的AdjustBounds转换的中心点,其中我们更改了视图的高度/宽度)。