现在的位置: 首页 > 综合 > 正文

Android — Tween动画示例(代码中定义的动画)

2013年08月21日 ⁄ 综合 ⁄ 共 3078字 ⁄ 字号 评论关闭

1、在layout目录中新建XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="match_parent"
	android:layout_height="match_parent">
	<ImageView
		android:id="@+id/imgTween"
		android:src="@drawable/c01"
		android:layout_height="wrap_content"
		android:layout_width="wrap_content"
		android:layout_weight="1.0" />
	<Button
		android:id="@+id/btnControl"
		android:text="开始"
		android:layout_width="match_parent"
		android:layout_height="wrap_content" />

</LinearLayout>

2、在Activity代码中写:

package com.bison;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class TweenAnimationDemo extends Activity implements OnClickListener {
	// 声明一个开始停止的标识符
	private boolean flags = true;
	private ImageView imgTween;
	private Button btnCtrl;
	private AnimationSet as;

	/** 初始化 */
	public void init() {
		// 声明AnimationSet
		as = new AnimationSet(true);
		// 声明Alpha、Scale、Translate、Rotate 等Animation
		AlphaAnimation aa = alphaAnim(1, 0.3f);
		ScaleAnimation sa = scaleAnim(0.2f, 1.0f, 0.2f, 1.0f, 1, 1);
		TranslateAnimation ta = translateAnim(50f, 100f, 50f, 100f);
		RotateAnimation ra = rotateAnim(0, 360);
		// 添加各种动画
		as.addAnimation(aa);
		as.addAnimation(sa);
		as.addAnimation(ta);
		as.addAnimation(ra);

		imgTween = (ImageView) findViewById(R.id.imgTween);
		imgTween.setScaleType(ScaleType.CENTER_INSIDE);

		btnCtrl = (Button) findViewById(R.id.btnControl);
		btnCtrl.setOnClickListener(this);

	}

	/** 缩放 */
	private ScaleAnimation scaleAnim(float start_x, float end_x, float start_y,
			float end_y, float x2, float y2) {
		// 开始x坐标伸缩尺寸,结束x坐标伸缩尺寸,开始y坐标伸缩尺寸,结束y坐标伸缩尺寸,x轴的百分比,y轴的百分比
		ScaleAnimation sa = new ScaleAnimation(start_x, end_x, start_y, end_y,
				x2, y2);
		sa.setDuration(3000);
		sa.setRepeatMode(Animation.REVERSE);
		sa.setRepeatCount(5);
		return sa;
	}

	/** 透明度 */
	private AlphaAnimation alphaAnim(float x, float y) {
		AlphaAnimation aa = new AlphaAnimation(x, y);
		aa.setDuration(2000);
		aa.setRepeatMode(Animation.REVERSE);
		aa.setRepeatCount(5);
		return aa;
	}

	/** 移动 */
	private TranslateAnimation translateAnim(float startX, float endX,
			float startY, float endY) {
		TranslateAnimation ta = new TranslateAnimation(startX, endX, startY,
				endY);
		ta.setDuration(3000);
		ta.setRepeatMode(Animation.REVERSE);
		ta.setRepeatCount(5);
		return ta;
	}

	/** 旋转 */
	private RotateAnimation rotateAnim(float startDegrees, float endDegrees) {
		RotateAnimation ra = new RotateAnimation(startDegrees, endDegrees);
		ra.setDuration(3000);
		ra.setRepeatMode(Animation.RESTART);
		ra.setRepeatCount(5);
		return ra;
	}

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

	public void onClick(View v) {
		if (flags) {
			btnCtrl.setText("停止");
			imgTween.startAnimation(as);
			flags = false;
		} else {
			btnCtrl.setText("开始");
			imgTween.clearAnimation();
			flags = true;
		}
	}
}


PS:这个方法比在XML中定义要好,可以传参,修改等,更方便操作。

抱歉!评论已关闭.