【Android动画】之Tween动画 (渐变、缩放、位移、旋转)代码中的实现

***Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。

第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。***


#分类:

Animation   动画

AlphaAnimation 渐变透明度

RotateAnimation 画面旋转

ScaleAnimation 渐变尺寸缩放

TranslateAnimation 位置移动

AnimationSet  动画集


        Animation.RELATIVE_TO_PARENT 相对于父亲

        Animation.RELATIVE_TO_SELF   相对于自己


下面是代码中的实现四种动画效果哦,还有一种是结合xml来实现的。


***********************布局是很简单的,就不写了,自己定义哦,下面是MaActivity.class中的代码*****************


//TranslateAnimation 位置移动

//平移动画
		TranslateAnimation animation = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
				1.0f, Animation.RELATIVE_TO_PARENT, 0,
				Animation.RELATIVE_TO_PARENT, 1.0f);
		// 设置运行时间 5秒
		animation.setDuration(5000);
		// 设置重复次数 5
//		animation.setRepeatCount(5);
		animation.setRepeatCount(Animation.INFINITE);

		// Animation.Reverse 往复
		// Animation.restart 重新来
		animation.setRepeatMode(Animation.REVERSE);

		// 开启动画
		imagView1.startAnimation(animation);


// AlphaAnimation 渐变透明度

//渐变透明
		AlphaAnimation r=new AlphaAnimation(1, 0);//1-0 从有到无
		//设置时间
		r.setDuration(3000);
		//设置一直循环
		r.setRepeatCount(Animation.INFINITE);
		//设置RESTART
		r.setRepeatMode(Animation.RESTART);
		//启动
		imagView.startAnimation(r);

// RotateAnimation 画面旋转

RotateAnimation r=new RotateAnimation(0, 3600, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		r.setDuration(3000);
		r.setRepeatCount(Animation.INFINITE);
		r.setRepeatMode(Animation.RESTART);
		imagView.startAnimation(r);


// ScaleAnimation 渐变尺寸缩放

ScaleAnimation r=new ScaleAnimation(0.3f, 1, 0.3f, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
		r.setDuration(3000);
		r.setRepeatCount(Animation.INFINITE);
		r.setRepeatMode(Animation.RESTART);
		imagView.startAnimation(r);

// AnimationSet  动画集(几种动画结合在一起使用)

	// shareInterpolator 是否使用同一个动画插入器
		AnimationSet animationSet = new AnimationSet(true);

		TranslateAnimation animation = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
				1.0f, Animation.RELATIVE_TO_PARENT, 0,
				Animation.RELATIVE_TO_PARENT, 1.0f);
	
		AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
		
		// 将动画添加到集合中
		animationSet.addAnimation(animation);
		animationSet.addAnimation(alphaAnimation);

		
		animationSet.setDuration(5000);
		animationSet.setRepeatMode(Animation.INFINITE);
		
		imageView.startAnimation(animationSet);
//动画监听事件 r:代表动画的对象

//动画监听
		r.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override//结束
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				//结束后执行什么
			}
		});


*****************************下面是XML中写动画, 这种方式可能更加简洁、清晰,也更利于重用。 **********************************


***** alpha.xml ****** 创建xml的时候记得选择对应的类型哦

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromAlpha="1"
    android:toAlpha="0" 
    android:repeatCount="infinite">

</alpha>

***********rotate.xml************

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%p"
    android:pivotY="50%p" >

</rotate>

*************scale.xml************

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="100%"
    android:toXScale="200%"
    android:fromYScale="100%"
    android:toYScale="200%"
    android:duration="3000"
    android:repeatCount="infinite" >


</scale>

***************translate.xml******************

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%p"
    android:fromYDelta="0%p"
    android:toXDelta="100%p"
    android:toYDelta="100%p"
    android:duration="5000"
    android:fillAfter="true" >

</translate>

***************set.xml*************

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true" >

    <alpha
        android:duration="3000"
        android:fromAlpha="1"
        android:repeatCount="infinite"
        android:toAlpha="0" >
    </alpha>

    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:toDegrees="360" >
    </rotate>

</set>

************************xml写法******MaActivity.class中的代码********************************

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.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView imageView;

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

	public void translate(View v) {
		TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils
				.loadAnimation(this, R.anim.translate);
		imageView.startAnimation(translateAnimation);
	}

	public void rotate(View v) {
		// 加载动画
		Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
		imageView.startAnimation(animation);

	}

	public void alpha(View v) {
		Animation loadAnimation = AnimationUtils.loadAnimation(this,
				R.anim.alpha);
		imageView.startAnimation(loadAnimation);

	}

	public void scale(View v) {
		Animation loadAnimation = AnimationUtils.loadAnimation(this,
				R.anim.scale);
		imageView.startAnimation(loadAnimation);
	}

	public void combine(View v) {
		Animation set = AnimationUtils.loadAnimation(this, R.anim.set);
		imageView.startAnimation(set);
	}
}

$ OK $






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值