android tween动画是什么,Android---Tween动画示例(代码中定义的动画)

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

[html]

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

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/imgTween"

android:src="@drawable/c01"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_weight="1.0" />

android:id="@+id/btnControl"

android:text="开始"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

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

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/imgTween"

android:src="@drawable/c01"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_weight="1.0" />

android:id="@+id/btnControl"

android:text="开始"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

2、在Activity代码中写:

[java]

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;

}

}

}

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中定义要好,可以传参,修改等,更方便操作。

摘自  今非昔…毕…的专栏

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值