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

来源:互联网 发布:stayreal淘宝官方店 编辑:程序博客网 时间:2024/06/14 05:16

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

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/imgTween"android:src="@drawable/c01"android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_weight="1.0" /><Buttonandroid: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() {// 声明AnimationSetas = new AnimationSet(true);// 声明Alpha、Scale、Translate、Rotate 等AnimationAlphaAnimation 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;}@Overrideprotected 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中定义要好,可以传参,修改等,更方便操作。

原创粉丝点击