gpt4 book ai didi

android - 比例过渡动画

转载 作者:太空狗 更新时间:2023-10-29 16:26:46 24 4
gpt4 key购买 nike

我想为 ConstraintLayout 中的 Scale View 元素创建自定义 Transition。在 AutoTransition.java 中。我们有淡入、AdjustBounds 和淡出。因此执行简单的 Scale 不会动画变化。

经过一些解决方法后,我能够创建带有比例的自定义过渡。我的观点是创建具有可见性和比例的过渡集。尽管有 Scale Pivot,但通过下面的更改,此动画效果很好。

 //..................... 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 的左上角)。而且我无法更改动画 View 的过渡枢轴。无论是将此属性添加到 View.java 还是 ConstraintSet.java 参数。所以问题是如何更改此比例转换的 View 枢轴。 (作为附加问题,我对更改默认 AdjustBounds Transition 的 Pivot 很感兴趣,我们在其中更改 View 的高度/宽度)。

最佳答案

尝试在每个动画步骤中重置枢轴。如果您正在寻找中心枢轴,您可以通过 getWidth() 进行斜杠,否则您需要捕获另一个枢轴值并使用它。

试试这个转换:

public class ScaleTransition extends Transition {
private final static String PROPNAME_SCALE_X = "PROPNAME_SCALE_X";
private final static String PROPNAME_SCALE_Y = "PROPNAME_SCALE_Y";

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

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

private void captureValues(TransitionValues values){
values.values.put(PROPNAME_SCALE_X, values.view.getScaleX());
values.values.put(PROPNAME_SCALE_Y, values.view.getScaleY());
}

@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
if(endValues == null || startValues == null)
return null; // no values

float startX = (float) startValues.values.get(PROPNAME_SCALE_X);
float startY = (float) startValues.values.get(PROPNAME_SCALE_Y);
float endX = (float) endValues.values.get(PROPNAME_SCALE_X);
float endY = (float) endValues.values.get(PROPNAME_SCALE_Y);

if(startX == endX && startY == endY)
return null; // no scale to run

final View view = startValues.view;
PropertyValuesHolder propX = PropertyValuesHolder.ofFloat(PROPNAME_SCALE_X, startX, endX);
PropertyValuesHolder propY = PropertyValuesHolder.ofFloat(PROPNAME_SCALE_Y, startY, endY);
ValueAnimator valAnim = ValueAnimator.ofPropertyValuesHolder(propX, propY);
valAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
view.setPivotX(view.getWidth()/2f);
view.setPivotY(view.getHeight()/2f);
view.setScaleX((float) valueAnimator.getAnimatedValue(PROPNAME_SCALE_X));
view.setScaleY((float) valueAnimator.getAnimatedValue(PROPNAME_SCALE_Y));
}
});
return valAnim;
}
}

关于android - 比例过渡动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48430640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com