在代码中设置补间动画

补间动画就是指开发者只需指定动画开始、动画结束“关键帧”和动画持续时间即可,而动画变化的“中间帧”由系统计算、并补齐。所以开发者无须“逐一”定义动画过程中的每一帧。下面用一个简单实例来演示在代码中设置补间动画,代码如下:

Activity:

package com.lovo.testtween;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class TestTweenActivity extends Activity {
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imageView = (ImageView) findViewById(R.id.main_iv);
	}

	public void click(View v) {
		switch (v.getId()) {
		case R.id.main_btn_alpha:
			// AlphaAnimation:透明动画的类
			AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.1f);
			// 设置动画持续时间
			alphaAnimation.setDuration(3000);
			// 保持动画后的状态
			alphaAnimation.setFillAfter(true);
			// 重复的次数
			alphaAnimation.setRepeatCount(-1);
			// 重复的类型 RESTART - 每次从头开始播放动画; REVERSE - 反向播放动画
			alphaAnimation.setRepeatMode(AlphaAnimation.RESTART);
			// 让图片组件开始加载动画
			imageView.startAnimation(alphaAnimation);
			break;
		case R.id.main_btn_rotate:
			// RotateAnimation:旋转动画的类
			RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			// 设置动画持续时间
			rotateAnimation.setDuration(3000);
			// 保持动画后的状态
			rotateAnimation.setFillAfter(true);
			// 重复的次数
			rotateAnimation.setRepeatCount(-1);
			// 重复的类型 RESTART - 每次从头开始播放动画; REVERSE - 反向播放动画
			rotateAnimation.setRepeatMode(AlphaAnimation.RESTART);
			// 让图片组件开始加载动画
			imageView.startAnimation(rotateAnimation);
			break;
		case R.id.main_btn_scale:
			// ScaleAnimation:缩放动画的类。从图片的中心位置开始缩放
			ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
					0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			// 设置动画持续时间
			scaleAnimation.setDuration(3000);
			// 保持动画后的状态
			scaleAnimation.setFillAfter(true);
			// 重复的次数
			scaleAnimation.setRepeatCount(-1);
			// 重复的类型 RESTART - 每次从头开始播放动画; REVERSE - 反向播放动画
			scaleAnimation.setRepeatMode(AlphaAnimation.REVERSE);
			// 让图片组件开始加载动画
			imageView.startAnimation(scaleAnimation);
			break;
		case R.id.main_btn_translate:
			// TranslateAnimation:平移动画的类
			TranslateAnimation translateAnimation = new TranslateAnimation(0,
					150, 0, 150);
			// 设置动画持续时间
			translateAnimation.setDuration(3000);
			// 保持动画后的状态
			translateAnimation.setFillAfter(true);
			// 重复的次数
			translateAnimation.setRepeatCount(-1);
			// 重复的类型 RESTART - 每次从头开始播放动画; REVERSE - 反向播放动画
			translateAnimation.setRepeatMode(AlphaAnimation.REVERSE);
			// 让图片组件开始加载动画
			imageView.startAnimation(translateAnimation);
			//为动画播放状态绑定监听器
			translateAnimation.setAnimationListener(new AnimationListener(){

				@Override
				public void onAnimationEnd(Animation animation) {
					Toast.makeText(TestTweenActivity.this, "动画播放结束", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
					Toast.makeText(TestTweenActivity.this, "动画重复播放", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onAnimationStart(Animation animation) {
					Toast.makeText(TestTweenActivity.this, "动画开始播放", Toast.LENGTH_SHORT).show();
				}});
			break;
		}
	}
}


布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/main_btn_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="透明" />

        <Button
            android:id="@+id/main_btn_scale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="放缩" />

        <Button
            android:id="@+id/main_btn_translate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="平移" />

        <Button
            android:id="@+id/main_btn_rotate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="旋转" />
    </LinearLayout>

    <ImageView
        android:id="@+id/main_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/freedom1" />

</LinearLayout>


 

原文地址:https://www.cnblogs.com/jiangu66/p/3180068.html