Android中四种动画依次播放,Android中TweenAnimation四种动画切换效果

点击每个按钮都会有对应的动画显示

7638cac19fec9a218928cf8f74473c0d.png

activity代码:

package com.tmacsky;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

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.AnimationUtils;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

import android.view.animation.Animation.AnimationListener;

import android.view.animation.RotateAnimation;

import android.widget.Button;

import android.widget.ImageView;

public class AnimationDemoActivity extends Activity {

private ImageView imageView;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//定义四个动画的button属性

imageView = (ImageView)findViewById(R.id.imageView);

Button alpha = (Button)findViewById(R.id.Alpha);

alpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha));

Button rotate = (Button)findViewById(R.id.Rotate);

rotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate));

Button scale = (Button)findViewById(R.id.Scale);

scale.setOnClickListener(new AnimationClickListener(AnimationType.Scale));

Button translate = (Button)findViewById(R.id.Translate);

translate.setOnClickListener(new AnimationClickListener(AnimationType.Translate));

Button complex = (Button)findViewById(R.id.Complex);

complex.setOnClickListener(new AnimationClickListener(AnimationType.Complex));

}

//定义animationType属性

enum AnimationType{

Alpha,

Rotate,

Scale,

Translate,

Complex

}

//定义一个函数

class AnimationClickListener implements OnClickListener{

private AnimationType animationType;

public AnimationClickListener(AnimationType animType){

animationType = animType;

}

public void onClick(View v) {

// TODO Auto-generated method stub

switch (animationType) {

case Alpha:

//定义渐变动画,重复5次,持续1分钟

/*AlphaAnimation _animation = new AlphaAnimation(1f, 0.1f);

_animation.setDuration(3000);

_animation.setRepeatCount(5);

//设置循环

_animation.setRepeatMode(Animation.REVERSE);

_animation.setAnimationListener(new AnimationListener() {

//设置animation监听,依次是启动,重复,然后结束

public void onAnimationStart(Animation animation) {

// TODO Auto-generated method stub

Log.i("log", "animation Start");

}

public void onAnimationRepeat(Animation animation) {

// TODO Auto-generated method stub

Log.i("log", "animation Repeat");

}

public void onAnimationEnd(Animation animation) {

// TODO Auto-generated method stub

Log.i("log", "animation End");

}

});

//启动动画

imageView.startAnimation(_animation);*/

AlphaAnimation alphaAnimation = (AlphaAnimation)AnimationUtils.loadAnimation(AnimationDemoActivity.this, R.anim.alpha);

imageView.startAnimation(alphaAnimation);

break;

case Rotate:

//定义旋转动画,旋转一周持续1分钟,重复三次,在物体的中心位置

RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

rotateAnimation.setDuration(3000);

rotateAnimation.setRepeatCount(3);

//启动动画

imageView.startAnimation(rotateAnimation);

break;

case Scale:

//定义缩放动画,从中心坐标开始,缩放1.5倍大小,持续1分钟,重复三次

ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

scaleAnimation.setDuration(3000);

scaleAnimation.setRepeatCount(3);

//启动动画

imageView.startAnimation(scaleAnimation);

break;

case Translate:

//定义移动动画,都从自身坐标开始,移动2个位置,持续1分钟,重复三次

TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);

translateAnimation.setDuration(3000);

translateAnimation.setRepeatCount(3);

//启动动画

imageView.startAnimation(translateAnimation);

break;

case Complex:

//设置复杂的操作步骤,点击按钮complex后,会运行四种动画效果叠加

AnimationSet sets = new AnimationSet(false);

//定义渐变动画

AlphaAnimation _animation1 = new AlphaAnimation(1f, 0.1f);

_animation1.setDuration(3000);

//定义旋转动画,在物体的中心位置

RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

rotateAnimation1.setDuration(3000);

//定义缩放动画,从中心坐标开始,缩放1.5倍大小

ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

scaleAnimation1.setDuration(3000);

//定义移动动画,都从自身坐标开始,移动2个位置

TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);

translateAnimation1.setDuration(3000);

//启动动画

sets.addAnimation(_animation1);

sets.addAnimation(rotateAnimation1);

sets.addAnimation(scaleAnimation1);

sets.addAnimation(translateAnimation1);

imageView.startAnimation(sets);

break;

default:

break;

}

}

}

}

layout文件:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

android:id="@+id/imageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/qa" />

android:id="@+id/Alpha"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Alpha" />

android:id="@+id/Rotate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Rotate" />

android:id="@+id/Scale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Scale" />

android:id="@+id/Translate"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Translate" />

android:id="@+id/Complex"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Complex" />

资源anim文件alpha.xml

xmlns:android="http://schemas.android.com/apk/res/android"

android:fromAlpha="1" android:toAlpha="0.3" android:duration="2000" android:repeatCount="3">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值