【问题标题】:Android animation translate and scale at the same time is not what I wantAndroid动画同时翻译和缩放不是我想要的
【发布时间】:2019-08-29 13:51:41
【问题描述】:

我想编写一个动画来翻译视图并同时缩放它。这是一个简单的动画,但运行时,它不能正常工作。它以曲线而不是直线运行。 视图的布局 xml 和 anim.xml 在下面

//activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="clickBtn"
    android:text="startAnim"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<View
    android:id="@+id/view"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="@color/colorAccent"
    app:layout_constraintBottom_toBottomOf="parent" />
 </android.support.constraint.ConstraintLayout>

 // anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:duration="8000"
 android:shareInterpolator="true">

 <translate
    android:fromYDelta="0%p"
    android:toYDelta="-100%p" />

<alpha
    android:fromAlpha="1"
    android:toAlpha="0" />

<scale
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0.2"
    android:toYScale="0.2" />

【问题讨论】:

    标签: android animation


    【解决方案1】:

    同时运行两个动画,可以使用AnimatorSet

    在许多情况下,您希望播放一个取决于另一个动画何时开始或结束的动画。 Android 系统允许您将动画捆绑到一个AnimatorSet 中,以便您可以指定是同时启动动画、顺序启动动画还是在指定延迟后启动动画。您还可以将AnimatorSet 对象相互嵌套。

    制作两个单独的动画(一个用于平移,一个用于缩放)并使用AnimatorSet 启动它们。

    有关更多信息,您可以访问此处: https://developer.android.com/guide/topics/graphics/prop-animation

    【讨论】:

    • 是的,AnimatorSet 是属性动画师,但我认为视图动画可以做到这一点。不行吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多