动画

package com.example.lenovo.donghua;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    ImageView iv_1, iv_2;

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

        iv_1 = (ImageView) findViewById(R.id.iv_1);
        iv_2 = (ImageView) findViewById(R.id.iv_2);
    }

    //缩放动画代码
    public void bt_1(View v) {
        //实例化
        // ScaleAnimation sc=new ScaleAnimation(0f,1.5f,0f,1.5f);以左上角原点为基准

//        ScaleAnimation sc=new ScaleAnimation(0f,1.1f,0f,1.1f,
//                Animation.ABSOLUTE,iv_1.getWidth()/2,
//                Animation.ABSOLUTE,iv_1.getHeight()/2);//以中心点

        ScaleAnimation sc = new ScaleAnimation(0f, 1.1f, 0f, 1.1f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);//

        sc.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e("TAG", "动画开始");
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                Log.e("TAG", "动画结束");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

                Log.e("TAG", "动画循环");
            }
        });
        sc.setDuration(2000);//持续时间
        iv_1.startAnimation(sc);
    }

    //缩放动画XML"
    public void bt_2(View v) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
        iv_1.startAnimation(animation);
    }

    //旋转动画代码
    public void bt_3(View v) {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 1800, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setRepeatCount(3);
        //设置线性变化
        rotateAnimation.setInterpolator(new LinearInterpolator());
        //rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());减速加速,中间快
        rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e("TAG", "动画开始");
            }

            @Override
            public void onAnimationEnd(Animation animation) {

                Log.e("TAG", "动画结束");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

                Log.e("TAG", "动画循环");
            }
        });
        iv_1.startAnimation(rotateAnimation);
    }

    //旋转动画Xml
    public void bt_4(View v) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotateanimation);
        iv_1.startAnimation(animation);
    }

    //透明度动画代码
    public void bt_5(View v) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(3000);
        iv_1.startAnimation(alphaAnimation);
    }

    //透明度动画xml
    public void bt_6(View v) {
        Animation Animation = AnimationUtils.loadAnimation(this, R.anim.alphaanimation);
        iv_1.startAnimation(Animation);
    }

    //平移动画代码
    public void bt_7(View v) {
        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.75f,
                Animation.RELATIVE_TO_PARENT, 0f,
                Animation.ABSOLUTE, 0f,
                Animation.ABSOLUTE, 0f);
        translateAnimation.setDuration(4000);
        iv_1.startAnimation(translateAnimation);
    }


    //平移动画xml
    public void bt_8(View v) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
        iv_1.startAnimation(animation);
    }

    //复合动画代码
    public void bt_9(View v) {
        AnimationSet as = new AnimationSet(false);
        //透明度
        AlphaAnimation aa = new AlphaAnimation(0, 1);
        aa.setDuration(2000);

        as.addAnimation(aa);

        RotateAnimation ro = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        ro.setDuration(2000);
        //ro.setStartOffset(2000);//延迟两秒启动。

        as.addAnimation(ro);

        //透明度跟旋转同时进行
        iv_1.startAnimation(as);

//        iv_1.startAnimation(aa);
//        iv_1.startAnimation(ro);
    }

    //复合动画xml
    public void bt_10(View v) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.animationset);
        iv_1.startAnimation(animation);
    }


    AnimationDrawable ad;

    //帧动画
    public void bt_11(View v) {
        if (ad == null) {
            //得到动画对象
            ad = (AnimationDrawable) iv_2.getBackground();
            ad.start();
        }
    }

    //停止
    public void bt_12(View v) {
        if (ad != null) {
            ad.stop();
            ad = null;
        }
    }
}
JAVA
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lenovo.donghua.MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="缩放动画代码"
        android:onClick="bt_1"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩放动画XML"
            android:onClick="bt_2"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转动画代码"
            android:onClick="bt_3"/>


        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转动画XML"
            android:onClick="bt_4"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="透明度动画代码"
            android:onClick="bt_5"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="透明度动画XML"
            android:onClick="bt_6"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="平移动画代码"
            android:onClick="bt_7"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="平移动画XML"
            android:onClick="bt_8"/>
</LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="复合动画代码"
            android:onClick="bt_9"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="复合动画XML"
            android:onClick="bt_10"/>
        </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/nv4"
        android:layout_gravity="center"
        android:id="@+id/iv_1"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv_2"
        android:layout_gravity="center"
        android:background="@drawable/frame"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="启动帧动画"
            android:onClick="bt_11"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="结束帧动画"
            android:onClick="bt_12"/>
        </LinearLayout>
    </LinearLayout>
activity

1).frame

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

    <item android:drawable="@drawable/nv1" android:duration="100"/>
    <item android:drawable="@drawable/nv2" android:duration="100"/>
    <item android:drawable="@drawable/nv3" android:duration="400"/>
    <item android:drawable="@drawable/nv4" android:duration="100"/>
</animation-list>
View Code

2)缩放动画

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="2" android:toXScale="0.5"
    android:fromYScale="2" android:toYScale="0.5"
    android:pivotX="100%" android:pivotY="100%"
    android:duration="3000">

</scale>
scale

3)旋转动画

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0" android:toDegrees="720"
   android:pivotX="100%" android:pivotY="50%"
    android:duration="3000"><!--设置旋转点-->

</rotate>
rotate

4)透明度动画

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1" android:toAlpha="0"
    android:duration="3000">

</alpha>
alpha

5)平移动画

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

</translate>
translate

6)复合动画

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

    <!--透明度跟缩放同时进行-->
    <alpha android:duration="2000" android:fromAlpha="0" android:toAlpha="1"/>

    <scale android:duration="3000" android:fromXScale="1" android:toXScale="2"
         android:fromYScale="1" android:toYScale="2" android:pivotX="50%" android:pivotY="100%"
        android:startOffset="2000"/>
</set>
set

7)帧动画

 //帧动画
    public void bt_11(View v) {
        if (ad == null) {
            //得到动画对象
            ad = (AnimationDrawable) iv_2.getBackground();
            ad.start();
        }
    }

    //停止
    public void bt_12(View v) {
        if (ad != null) {
            ad.stop();
            ad = null;
        }
    }
View Code

原文地址:https://www.cnblogs.com/1ming/p/5655552.html