Android动画示例代码

1、布局文件:activity_animation_demo.xml

<ScrollView 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">
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.xw.firstapp.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="动画演示" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="5dp"
        android:background="@android:color/black" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_starScaleAnimation"
        android:text="启动缩放动画" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_startTransationAnimation"
        android:text="启动位移动画" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_startRoationAnimation"
        android:text="启动旋转动画" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_startAlphaAnimation"
        android:text="启动透明度动画" />

    <TextView
        android:id="@+id/aty_animation_demo_target"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:background="@android:color/holo_blue_dark" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="差值器" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="5dp"
        android:background="@android:color/black" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_bouceInterpolator"
        android:text="测试弹跳差值器" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_overShootInterpolator"
        android:text="测试OverShoot差值器" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_accelerateInterpolator"
        android:text="测试加速差值器" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_decelerateInterpolator"
        android:text="测试减速差值器" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="5dp"
        android:background="@android:color/black" />
    
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clk_listener"
        android:text="测试属性动画监听器" />
</LinearLayout>
</ScrollView>

2、activity文件:AnimationDemoActivity

package com.xw.firstapp;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 动画Demo的Activity
 * */
public class AnimationDemoActivity extends Activity {

	private TextView tv_Target;// 动画被作用的View

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animation_demo);
		tv_Target = (TextView) findViewById(R.id.aty_animation_demo_target);
	}

	/**
	 * 点击开始动画的执行函数
	 * 
	 * @param v
	 * */
	public void clk_starScaleAnimation(View v) {
		// 创建一个动画对象(作用在tv_Target身上,对x轴方向进行变换)
		// 其中(1,0,3,0,1)为可变参数,标识tv_Target从原来大小(1)缩放为0,然后变换为原来3倍,再缩放成0,再变为原大小(1)
		ObjectAnimator animator_scaleX = ObjectAnimator.ofFloat(tv_Target,
				View.SCALE_X, 1, 0, 3, 0, 1);
		// 为动画设置持续时间 2秒
		animator_scaleX.setDuration(1000 * 2);
		// 启动动画
		animator_scaleX.start();

		// 创建一个动画对象(作用在tv_Target身上,对y轴方向进行缩放变换)
		// 其中(1,0,1)为可变参数,标识tv_Target的高度从原来大小(1)缩放到0,再变化成原来大小(1)
		ObjectAnimator animator_scaleY = ObjectAnimator.ofFloat(tv_Target,
				View.SCALE_Y, 1, 0, 1);
		animator_scaleY.setDuration(1000 * 2);
		animator_scaleY.start();
	}

	/**
	 * 点击开始位移动画的执行函数
	 * 
	 * @param v
	 * */
	public void clk_startTransationAnimation(View v) {
		ObjectAnimator animor_transationX = ObjectAnimator.ofFloat(tv_Target,
				View.TRANSLATION_X, 0, 200, -200, 0);
		animor_transationX.setDuration(1000 * 2);
		animor_transationX.start();

		ObjectAnimator animor_transationY = ObjectAnimator.ofFloat(tv_Target,
				View.TRANSLATION_Y, 0, 400, -100, 0);
		animor_transationY.setDuration(1000 * 2);
		animor_transationY.start();
	}

	/**
	 * 点击开始旋转动画的执行函数
	 * 
	 * @param v
	 * */
	public void clk_startRoationAnimation(View v) {
		ObjectAnimator animator_rotationX = ObjectAnimator.ofFloat(tv_Target,
				View.ROTATION_X, 0, 180, 0);
		animator_rotationX.setDuration(1000 * 2);
		animator_rotationX.start();

		ObjectAnimator animator_rotationY = ObjectAnimator.ofFloat(tv_Target,
				View.ROTATION_Y, 0, 900, 0);
		animator_rotationY.setDuration(1000 * 2);
		animator_rotationY.start();

		ObjectAnimator animator_rotation = ObjectAnimator.ofFloat(tv_Target,
				View.ROTATION, 0, 180, 0);
		animator_rotation.setDuration(1000 * 2);

		// 添加动画监听器
		animator_rotation.addListener(new Animator.AnimatorListener() {
			// 动画开始执行的函数
			@Override
			public void onAnimationStart(Animator animation) {
				Toast.makeText(AnimationDemoActivity.this, "动画停止啦~~~",
						Toast.LENGTH_SHORT).show();
			}

			// 动画结束执行的函数
			@Override
			public void onAnimationRepeat(Animator animation) {
				Toast.makeText(AnimationDemoActivity.this, "动画停止啦~~~",
						Toast.LENGTH_SHORT).show();

			}

			// 动画被取消执行的函数
			@Override
			public void onAnimationEnd(Animator animation) {

			}

			// 动画重复播放时的函数
			@Override
			public void onAnimationCancel(Animator animation) {

			}
		});
		// animator_rotation.setInterpolator(new DecelerateInterpolator());//
		// 慢慢变慢的插值器
		// animator_rotation.setInterpolator(new AccelerateInterpolator());//
		// 慢慢变快的插值器
		// animator_rotation.setInterpolator(new
		// AccelerateDecelerateInterpolator());// 先快后慢的插值器
		// animator_rotation.setInterpolator(new BounceInterpolator());// 弹跳插值器
		animator_rotation.start();
	}

	/**
	 * 点击开始透明度动画的执行函数
	 * 
	 * @param v
	 * */
	public void clk_startAlphaAnimation(View v) {
		ObjectAnimator animator_alpha = ObjectAnimator.ofFloat(tv_Target,
				View.ALPHA, 0, 0.5f, 1);
		animator_alpha.setDuration(1000 * 2);
		animator_alpha.start();
	}

	/**
	 * 开始差值器演示
	 * 
	 * @param v
	 * */
	public void clk_bouceInterpolator(View v) {
		ObjectAnimator animator_translation_x = ObjectAnimator.ofFloat(
				tv_Target, View.TRANSLATION_X, 0, 200);
		animator_translation_x.setDuration(1000 * 1);
		animator_translation_x.start();
		// 创建并设置一个差值器
		animator_translation_x.setInterpolator(new BounceInterpolator());// 弹跳插值器
	}

	/**
	 * overShoot差值器
	 * 
	 * @param v
	 * */
	public void clk_overShootInterpolator(View v) {
		ObjectAnimator animator_translation_x = ObjectAnimator.ofFloat(
				tv_Target, View.TRANSLATION_X, 0, 200);
		animator_translation_x.setDuration(1000 * 1);
		animator_translation_x.start();
		// 创建并设置一个差值器
		animator_translation_x.setInterpolator(new OvershootInterpolator());// OverShoot插值器
	}

	/**
	 * 加速差值器
	 * 
	 * @param v
	 * */
	public void clk_accelerateInterpolator(View v) {
		ObjectAnimator animator_translation_x = ObjectAnimator.ofFloat(
				tv_Target, View.TRANSLATION_X, 0, 200);
		animator_translation_x.setDuration(1000 * 1);
		animator_translation_x.start();
		// 创建并设置一个差值器
		animator_translation_x.setInterpolator(new AccelerateInterpolator());// 加速插值器
	}

	/**
	 * 减速差值器
	 * 
	 * @param v
	 * */
	public void clk_decelerateInterpolator(View v) {
		ObjectAnimator animator_translation_x = ObjectAnimator.ofFloat(
				tv_Target, View.TRANSLATION_X, 0, 200);
		animator_translation_x.setDuration(1000 * 1);
		animator_translation_x.start();
		// 创建并设置一个差值器
		animator_translation_x.setInterpolator(new DecelerateInterpolator());// 减速插值器
	}
	
	/**
	 * 测试监听器
	 * 
	 * @param v
	 * */
	public void clk_listener(View v){
		ObjectAnimator animator_translation_x = ObjectAnimator.ofFloat(
				tv_Target, View.TRANSLATION_X, 0, 200);
		animator_translation_x.setDuration(1000 * 3);
		//创建一个属性动画监听器
		animator_translation_x.addListener(new Animator.AnimatorListener() {
			
			@Override
			public void onAnimationStart(Animator animation) {
				Toast.makeText(AnimationDemoActivity.this, "动画开始啦~~~~~~",Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onAnimationRepeat(Animator animation) {
				Toast.makeText(AnimationDemoActivity.this, "动画开始重复啦~~~~~~",Toast.LENGTH_SHORT).show();
				
			}
			
			@Override
			public void onAnimationEnd(Animator animation) {
				Toast.makeText(AnimationDemoActivity.this, "动画结束啦~~~~~~",Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onAnimationCancel(Animator animation) {
			}
		});
		animator_translation_x.setRepeatCount(3);//-1表示无限重复
		animator_translation_x.start();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_20264531/article/details/80015057
今日推荐