Android 之路68---动画基础

导读

1.逐帧动画
2.视图动画
3.速率插值器
4.属性动画

逐帧动画

逐帧动画是将静态图片连起来播放形成动画

这里写图片描述

这里写图片描述

创建loading.xml 文件

这里写图片描述

loading.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- android:oneshot="true" 动画只进行一次-->
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <!--duration 显示时长-->
    <item
        android:drawable="@drawable/frame_1"
        android:duration="100"/>

    <item
        android:drawable="@drawable/frame_2"
        android:duration="100"/>

    <item
        android:drawable="@drawable/frame_3"
        android:duration="100"/>


</animation-list>

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=".MainActivity">

    <View
        android:id="@+id/view"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/loading"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent">

        <Button
            android:id="@+id/btnStart"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="Start" />

        <Button
            android:id="@+id/btnStop"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="Stop" />


    </LinearLayout>


</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.hala.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View view=findViewById(R.id.view);
        //返回值是Drawable,要进行强制转换
        animationDrawable = (AnimationDrawable)view.getBackground();
//        动画只进行一次,也可以在loading.xml中进行设置
//        animationDrawable.setOneShot(true);
    }

    public void onClick(View view){
       switch(view.getId()){
           case R.id.btnStart:
               animationDrawable.start();
               break;
           case R.id.btnStop:
               animationDrawable.stop();
               break;
       }
    }
}

视图动画

视图动画是对Android原有控件做动画,如Button等

这里写图片描述

透明度动画

这里写图片描述

这里写图片描述

创建动画文件 alpha.xml

这里写图片描述

alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--fromAlpha 初始状态 1.0完全不透明
 toAlpha 最终状态 0.1完全透明-->
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1"/>
</set>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/viewAlpha"
            android:text="Alpha"
            android:textSize="30sp"
            android:onClick="onClick"
            android:background="@color/colorPrimary"
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </LinearLayout>


</ScrollView>

MainActivity.java

package com.hala.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onClick(View view){
       switch(view.getId()){
           case R.id.viewAlpha:
               Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);
               view.startAnimation(animation);
               break;

       }
    }
}

缩放动画

这里写图片描述

这里写图片描述

animation资源文件

scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true">
<!--fillAfter设置为true时会保持变化后的状态-->
    <!--X,Y轴变为原来的2倍 -->
    <scale
        android:fromXScale="1.0"
        android:toXScale="2.0"
        android:fromYScale="1.0"
        android:toYScale="2.0"
        android:pivotX="50%"
        android:pivotY="50%"/>

    <!--pivotX/Y 默认为0,在控件的左上角
    定义为50 距左/上边界50像素
    定义为50% 距左/上边界50%(占本控件)
    定义50%p 距父控件 左/上边界50%(占父控件)-->

</set>

这里写图片描述

这里写图片描述

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/viewAlpha"
            android:text="Alpha"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewScale"
            android:text="Scale"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />



    </LinearLayout>


</ScrollView>

⚠️技巧 这里可以对控件的属性进行抽取 style=”@style/AnimationTextView”
作用就是以后可以将它的部分属性作为模版如上应用即可
这里写图片描述

MainActivity.java

package com.hala.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onClick(View view){
       switch(view.getId()){
           case R.id.viewAlpha:
               Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);
               view.startAnimation(animation);
               break;
           case R.id.viewScale:
               Animation scaleAnimation=AnimationUtils.loadAnimation(this,R.anim.scale);
               view.startAnimation(scaleAnimation);
               break;

       }
    }
}

位移动画

这里写图片描述

这里写图片描述

animation资源文件 translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%"
        android:fromYDelta="0"
        android:toYDelta="50%p"/>

</set>

drawable资源文件 edge.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--内容透明 边线设置宽度为黑色 1像素-->
    <solid android:color="@android:color/transparent" />
    <stroke android:color="@android:color/black" android:width="1px"/>
</shape>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">


    <LinearLayout
        android:background="@drawable/edge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/viewAlpha"
            android:text="Alpha"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewScale"
            android:text="Scale"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewTranslate"
            android:text="Translate"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />




    </LinearLayout>


</ScrollView>

MainActivity.java

package com.hala.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onClick(View view){
       switch(view.getId()){

           case R.id.viewAlpha:
               Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);
               view.startAnimation(animation);
               break;
           case R.id.viewScale:
               Animation scaleAnimation=AnimationUtils.loadAnimation(this,R.anim.scale);
               view.startAnimation(scaleAnimation);
               break;
           case R.id.viewTranslate:
               Animation translateAnimation=AnimationUtils.loadAnimation(this,R.anim.translate);
               view.startAnimation(translateAnimation);
               break;

       }
    }
}

旋转动画

⚠️这节有两个知识点
一是旋转动画即相关属性,二是menu的使用

这里写图片描述

这里写图片描述

animation资源文件 rotate.xml

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

    <rotate
        android:fromDegrees="0"
        android:toDegrees="60"
        android:pivotY="0"
        android:pivotX="100%"
        android:repeatCount="3"
        android:repeatMode="reverse"
        />

    <!--旋转角度为顺时针
    repeatCount 为重复次数 可设置属性infinite为无限次
     repeatMode  属性值有两个 restart 一次之后回到开始位置再来
     reverse 一次后原路返回-->

</set>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">


    <LinearLayout
        android:background="@drawable/edge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/viewAlpha"
            android:text="Alpha"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewScale"
            android:text="Scale"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewTranslate"
            android:text="Translate"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewRotate"
            android:text="Rotate"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />




    </LinearLayout>


</ScrollView>

MainActivity.java

package com.hala.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.renew,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.renew:
                recreate();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View view){
       switch(view.getId()){

           case R.id.viewAlpha:
               Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);
               view.startAnimation(animation);
               break;
           case R.id.viewScale:
               Animation scaleAnimation=AnimationUtils.loadAnimation(this,R.anim.scale);
               view.startAnimation(scaleAnimation);
               break;
           case R.id.viewTranslate:
               Animation translateAnimation=AnimationUtils.loadAnimation(this,R.anim.translate);
               view.startAnimation(translateAnimation);
               break;
           case R.id.viewRotate:
               Animation rotateAnimation=AnimationUtils.loadAnimation(this,R.anim.rotate);
               view.startAnimation(rotateAnimation);
               break;

       }
    }
}

关于menu的使用
在drawable里边加载图片文件

这里写图片描述

这里写图片描述

menu资源文件 renew.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/renew"
    android:title="Renew"
    app:showAsAction="always"
    android:icon="@drawable/ic_redo_black_24dp"/>
</menu>

最后在MainActivity.java中加载(见上边)

这里写图片描述

集合动画

这里写图片描述

这里写图片描述

Animation资源文件 set.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:toDegrees="720"
        android:pivotX="50%"
        android:pivotY="50%"/>

    <translate
        android:startOffset="1000"
        android:duration="1000"
        android:fromXDelta="0"
        android:toXDelta="500"
        android:fromYDelta="0"
        android:toYDelta="0"/>

    <!--startOffset 开始后延迟该时长才进行这个动画-->

</set>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">


    <LinearLayout
        android:background="@drawable/edge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/viewAlpha"
            android:text="Alpha"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewScale"
            android:text="Scale"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewTranslate"
            android:text="Translate"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewRotate"
            android:text="Rotate"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewSet"
            android:text="Set"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />





    </LinearLayout>


</ScrollView>

MainActivity.java

package com.hala.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.renew,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.renew:
                recreate();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View view){
       switch(view.getId()){

           case R.id.viewAlpha:
               Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);
               view.startAnimation(animation);
               break;
           case R.id.viewScale:
               Animation scaleAnimation=AnimationUtils.loadAnimation(this,R.anim.scale);
               view.startAnimation(scaleAnimation);
               break;
           case R.id.viewTranslate:
               Animation translateAnimation=AnimationUtils.loadAnimation(this,R.anim.translate);
               view.startAnimation(translateAnimation);
               break;
           case R.id.viewRotate:
               Animation rotateAnimation=AnimationUtils.loadAnimation(this,R.anim.rotate);
               view.startAnimation(rotateAnimation);
               break;

       }
    }
}

速率插值器

这里写图片描述

速率插值器的作用是设置变化的频率

这里写图片描述

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000">
    <translate
        android:fromXDelta="0"
        android:toXDelta="500"
        android:fromYDelta="0"
        android:toYDelta="0"/>

</set>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">


    <LinearLayout
        android:background="@drawable/edge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/viewAlpha"
            android:text="Alpha"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewScale"
            android:text="Scale"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewTranslate"
            android:text="Translate"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewRotate"
            android:text="Rotate"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewSet"
            android:text="Set"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <View
            android:id="@+id/viewAccelerate"
            android:background="@color/colorPrimary"
            android:onClick="onClick"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <!--alpha 透明度-->
        <View
            android:id="@+id/viewLinear"
            android:alpha="0.5"
            android:background="@color/colorPrimary"
            android:onClick="onClick"
            android:layout_width="50dp"
            android:layout_height="50dp"/>





    </LinearLayout>


</ScrollView>

MainActivity.java

package com.hala.animation;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

public class MainActivity extends AppCompatActivity {

    private AnimationDrawable animationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.renew,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.renew:
                recreate();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View view){
       switch(view.getId()){

           case R.id.viewAlpha:
               Animation animation= AnimationUtils.loadAnimation(this,R.anim.alpha);
               view.startAnimation(animation);
               break;
           case R.id.viewScale:
               Animation scaleAnimation=AnimationUtils.loadAnimation(this,R.anim.scale);
               view.startAnimation(scaleAnimation);
               break;
           case R.id.viewTranslate:
               Animation translateAnimation=AnimationUtils.loadAnimation(this,R.anim.translate);
               view.startAnimation(translateAnimation);
               break;
           case R.id.viewRotate:
               Animation rotateAnimation=AnimationUtils.loadAnimation(this,R.anim.rotate);
               view.startAnimation(rotateAnimation);
               break;

           case R.id.viewAccelerate:
           case R.id.viewLinear:
               View viewLinear=findViewById(R.id.viewLinear);
               View viewAccelerate=findViewById(R.id.viewAccelerate);

               Animation linearAnimation=AnimationUtils.loadAnimation(this,R.anim.translate);
               Animation accelersteAnimation=AnimationUtils.loadAnimation(this,R.anim.translate);
               //匀速模式
               linearAnimation.setInterpolator(new LinearInterpolator());
               //加速模式
               accelersteAnimation.setInterpolator(new AccelerateInterpolator());

               viewLinear.startAnimation(linearAnimation);
               viewAccelerate.startAnimation(accelersteAnimation);
               break;
       }
    }
}

属性动画

⚠️属性动画与视图动画的区别:
视图动画只能操控android原有控件,而属性动画可以是任意的
属性动画在本质上改变了对象的属性,而视图动画没有

这里写图片描述

ValueAnimator

⚠️ValueAnimator是属性动画的驱动器

这里写图片描述

这里写图片描述

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=".MainActivity">

    <Button
        android:id="@+id/btnValueAnimation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Go"
        android:onClick="onClick" />



</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.hala.animation;

import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

public class MainActivity extends AppCompatActivity {


    private String TAG= "hala";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onClick(View view){
        switch (view.getId()){
            case R.id.btnValueAnimation:
                //驱动生成0~100
                ValueAnimator valueAnimator=ValueAnimator.ofInt(0,100);
                //设置时长
                valueAnimator.setDuration(100);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        //完成度
                        float animationFraction=valueAnimator.getAnimatedFraction();
                        int animatedValue=(int)valueAnimator.getAnimatedValue();
                        Log.d(TAG,"onAnimationUpdate:"+String.format("%.3f %d",animationFraction,animatedValue));
                    }
                });
                valueAnimator.start();
                break;
        }
    }


}

这里写图片描述

ObjectAnimator

这里写图片描述

这里写图片描述

animator资源文件

这里写图片描述

alpha.xml

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

    <objectAnimator
        android:propertyName="alpha"
        android:duration="1000"
        android:valueType="floatType"
        android:valueFrom="1.0"
        android:valueTo="0.1"/>
</set>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">


    <LinearLayout
        android:background="@drawable/edge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent">

        <TextView
            android:id="@+id/viewAlpha"
            android:text="Alpha"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />

        <TextView
            android:id="@+id/viewScale"
            android:text="Scale"
            android:onClick="onClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/AnimationTextView" />





    </LinearLayout>


</ScrollView>

MainActivity.java

package com.hala.animation;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

public class MainActivity extends AppCompatActivity {


    private String TAG= "hala";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void onClick(View view){
        switch (view.getId()){
            //在资源文件中设置
            case R.id.viewAlpha:
                Animator alphaAnimator= AnimatorInflater.loadAnimator(this,R.animator.alpha);
                //这个view就是被点击的视图
                alphaAnimator.setTarget(view);
                alphaAnimator.start();
                break;
                //在此处设置
            case R.id.viewScale:
                //scaleX可以被写在引号中直接设置,是因为view类中有它的set方法
                ObjectAnimator.ofFloat(view,"scaleX",1.0f,3.0f).start();
                break;

        }
    }


}

ViewPropertyAnimator

这里写图片描述

ViewPropertyAnimator的作用是可以改变视图动画控件的属性值,弥补了视图动画的不足

case R.id.viewTranslate:
                view.animate().translationX(500f).setDuration(1000).start();
                break;

属性动画集合

方法一

 case R.id.viewSet:
                Animator rotateAnimator=ObjectAnimator.ofFloat(view,"rotation",0,720);
                rotateAnimator.setDuration(1000);

                Animator moveAnimator=ObjectAnimator.ofFloat(view,"x",0,500);
                moveAnimator.setDuration(1000);

                AnimatorSet set=new AnimatorSet();
                set.playTogether(rotateAnimator,moveAnimator);
//                带有先后顺序的变化
//                set.playSequentially(rotateAnimator,moveAnimator);
                set.start();

方法二:

 view.animate().rotation(720).setDuration(1000).start();
                //设有延迟
                view.animate().translationX(500).setDuration(1000).setStartDelay(1000).start();

猜你喜欢

转载自blog.csdn.net/qq_37527943/article/details/80296709