Android动画
历史:
3.0以前,android支持Tween Animation[补间动画],Frame Animation[帧动画]
3.0中,android引入Property Animation[属性动画]
Tween Animation:补间动画,需要给出两个关键帧,通过算法将给定的属性值在给定的时间内在连个关键帧间渐变。
Frame Animation:帧动画,通过一系列Drawable依次来模拟动画的效果,就像老电影的制作。
Property Animation:属性动画
实现一些简单的例子来认识动画
最简单使用动画方式:
//缩放动画
ScaleAnimation sa = new ScaleAnimation(0.1f,1f,0.1f,1f);
sa.setDuration(1000);
tx.startAnimation(sa);
描述:设置一个缩放动画,设置动画持续时间,开始动画。
这样的动画简单,生硬。接下来使用一个插值器帮助动画更加生动。
首先实现一个插值器类
public class MyInterpolator implements Interpolator {
public float getInterpolation(float input) {
// TODO Auto-generated method stub
float factor = 0.4f;
// return 0;
//return (float) (Math.pow(2, -10 * input)* Math.sin((input - factor / 4) * (12 * Math.PI) / factor) + 1);
//pow(2,-10*x)*sin((x - foctor/4)*(2*PI))
//return (float) (Math.pow(2, -10 * input)* Math.sin((input - factor / 4) * (12 * Math.PI)));
return input;
}
}
在方法getInterpolation中,可以写不同的内容实现返回值的不同,
通过很多不同的复杂的数学函数可以使得返回的效果不同,这里推荐一个网站,
可以模拟很多函数效果,可以利用网站上有的函数去修改获取自己需要的函数。
http://inloop.github.io/interpolator/
Visualization是指通过不同函数实现效果的不同,如移动,放缩,旋转,透明度
Library是提供的一些函数,可以以其中的函数为基础来实现自己的函数
构建完插值器,然后是在动画中使用。
AnimationSet as = new AnimationSet(true);
as.setInterpolator(new MyInterpolator());
完整例:
MyInterpolator.java
public class MyInterpolator implements Interpolator {
public float getInterpolation(float input) {
// TODO Auto-generated method stub
return input;
}
}
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tx = (TextView)findViewById(R.id.txt);
tx.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//缩放动画
ScaleAnimation sa = new ScaleAnimation(0.1f,1f,0.1f,1f);
sa.setDuration(1000);
//旋转动画
RotateAnimation ra = new RotateAnimation(0, 90,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
ra.setDuration(1000);
//透明度动画
AlphaAnimation aa = new AlphaAnimation(0.0f,1.0f);
aa.setDuration(1000);
//位移动画
TranslateAnimation ta = new TranslateAnimation(0.0f, 0.0f, 0.0f, 150.0f); //0.0f表示在原来位置
ta.setDuration(1000);
AnimationSet as = new AnimationSet(true);
as.setInterpolator(new MyInterpolator());
//as.addAnimation(sa);
//as.addAnimation(ra);
//as.addAnimation(aa);
as.addAnimation(ta);
tx.startAnimation(as);
}
});
}
}
通过addAnimation的不同,动画也有不同效果