您的位置:首页 > 移动开发 > Android开发

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

2016-06-30 12:48 656 查看
***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 $
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: