动画效果

1. 透明动画 按钮透明

① 用代码控制

创建一个按钮

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/btnClickMe" />
</LinearLayout>
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                // 透明动画 透明效果从 0->1
                AlphaAnimation aa = new AlphaAnimation(0, 1);
                aa.setDuration(1000);  // 时间设置 1s
                v.startAnimation(aa);  // 把按钮设置改动画
            }
        });
        System.out.println("onCreate");
    }
}

② 用 anim目录下面的xml配置文件来实现

在 res/anim/aa.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- fromAlpha从0   toAlpha到1 duration时间1s-->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0" 
    android:toAlpha="1"
    android:duration="1000"
    />
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                /*// 透明动画 透明效果从 0->1
                AlphaAnimation aa = new AlphaAnimation(0, 1);
                aa.setDuration(1000);  // 时间设置 1s
                v.startAnimation(aa);  // 把按钮设置改动画*/
                
                // 用配置文件来实现
                v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.aa));
            }
        });
        System.out.println("onCreate");
    }
}

2. 旋转动画

视图界面 -- 一个按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/btnClickMe"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

①直接使用java代码

public class MainActivity extends Activity{

    private RotateAnimation ra = null;

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

        //ra = new RotateAnimation(0, 360); // 默认从0,0 旋转360度
        //ra = new RotateAnimation(0, 360 ,10, 10); // 从 10,10 旋转360度
        ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        ra.setDuration(1000); //时间 1s

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                v.startAnimation(ra);   //用代码

                
            }
        });
        System.out.println("onCreate");
    }
}

② 用配置文件 xml    /res/anim/rotate.xml

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

java代码

public class MainActivity extends Activity{

    private RotateAnimation ra = null;

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

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                //用xml文件
                v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.rotate));
            }
        });
        System.out.println("onCreate");
    }
}

3. 移动动画

视图----- 一个按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/btnClickMe"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

① 直接用java

public class MainActivity extends Activity{


    private TranslateAnimation ta = null;

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

        ta = new TranslateAnimation(0, 200, 0, 200);  // x + 200  y+200
        ta.setDuration(1000);// 1s 时间

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                v.startAnimation(ta);
            }
        });
        System.out.println("onCreate");
    }
}

② anim/translate.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200"
    android:toYDelta="200"
    android:duration="1000"
    xmlns:android="http://schemas.android.com/apk/res/android" />
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                // 调用配置文件 执行
                v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.translate));
            }
        });
        System.out.println("onCreate");
    }
}

4.缩放

视图 按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/btnClickMe"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

① 直接用java

public class MainActivity extends Activity{

    private ScaleAnimation sa;

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

        //sa = new ScaleAnimation(0, 1, 0, 1);//缩放比例 从0到1 (完整)
        //sa = new ScaleAnimation(0, 1, 0, 1, 100, 100); // 从x=100 y=100开始缩放
        sa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); // 相对于自己的中心点来缩放
        sa.setDuration(1000);
        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                v.startAnimation(sa);

            }
        });
        System.out.println("onCreate");
    }
}

② 使用配置文件

anim/scale.xml文件

<?xml version="1.0" encoding="utf-8"?>
<scale android:fromXScale="0"
    android:toXScale="1"
    android:fromYScale="0"
    android:toYScale="1"
    android:duration="1000"
    android:pivotY="50%"
    android:pivotX="50%"
    xmlns:android="http://schemas.android.com/apk/res/android" />
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                // 使用配置文件
                v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.scale));

            }
        });
        System.out.println("onCreate");
    }
}

5. 混合, 透明加移动

视图 一个按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/btnClickMe"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

① 直接用java

public class MainActivity extends Activity{

    private AnimationSet as = null;

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

        as = new AnimationSet(true);  // 动画容器 把要走的动画添加进去
        as.setDuration(1000);

        AlphaAnimation aa = new AlphaAnimation(0 ,1); //透明
        aa.setDuration(1000);
        as.addAnimation(aa);  //添加到容器里面

        TranslateAnimation ta = new TranslateAnimation(200, 0, 200, 0);  //移动
        ta.setDuration(1000);
        as.addAnimation(ta);  //添加到容器里面

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                // 使用配置文件

                v.startAnimation(as);
            }
        });
        System.out.println("onCreate");
    }
}

② 用配置文件 anim/animationset.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:shareInterpolator="true"
    >
    <translate android:fromXDelta="200"
        android:fromYDelta="200"
        android:toYDelta="0"
        android:toXDelta="0"
        ></translate>

    <alpha android:fromAlpha="0" android:toAlpha="1"></alpha>
</set>
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                // 使用配置文件
                v.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.animationset));
            }
        });
        System.out.println("onCreate");
    }
}

6. 动画监听

// 从配置文件获取 动画对象
                Animation a = AnimationUtils.loadAnimation(v.getContext(), R.anim.animationset);
                a.setAnimationListener(new Animation.AnimationListener() {  //监听
                    @Override
                    public void onAnimationStart(Animation animation) {  //动画开始的时候
                        Toast.makeText(MainActivity.this, "动画开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {    //动画结束的时候
                        Toast.makeText(MainActivity.this, "动画结束", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) { // 当动画重复时调用
                    }
                });
                v.startAnimation(a);

7. 自定义动画

创建我们自定义动画的类,CustomAnim类

/**
 * Created by ZhouXiaoHai on 2016/9/23.
 */
public class CustomAnim extends Animation {



    // 这个方法能得到 该组件的宽度 长度 , 父组件的宽度 长度
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    // applyTransformation 不停的自动执行这个方法
    // interpolatedTime 补间时间 0->1 从零到一 这个方法会不停的执行
    // Transformation 变化对象
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        //t.getMatrix().setTranslate(200* interpolatedTime, 200*interpolatedTime); //移动到一个位置
        //t.setAlpha(interpolatedTime);  // 透明效果

        // (Math.sin(interpolatedTime*10)*50) 这个值 是周期摆动的
        t.getMatrix().setTranslate((float) (Math.sin(interpolatedTime*10)*50), 0 );  // 两边摆动
        super.applyTransformation(interpolatedTime, t);
    }
}
public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // R.id.btnClickMe 是一个按钮
        this.findViewById(R.id.btnClickMe).setOnClickListener(new View.OnClickListener() { //按钮点击监听
            @Override
            public void onClick(View v) {
                // 从配置文件获取 动画对象
                Animation a = new CustomAnim();
                a.setDuration(1000);
                v.startAnimation(a);

            }
        });
        System.out.println("onCreate");
    }
}
原文地址:https://www.cnblogs.com/shaoshao/p/5898206.html