android 的动画主要分为逐帧动画,布局动画、视图动画,在这三种动画中,视图动画最复杂,本文主要讲解一下视图动画,实现其旋转的效果。
按照以往楼主的文章风格,就是将完第一段,贴代码,然后就没然后了。。但本文中先向各位看官说明一下几点,代码中注释完全使用英文,但楼主英文不好,所以注释很有问题,但各位可以查看下函数的英文应该就没问题了。
说明几个函数:在实现Animaito重写时,一下几个函数需要重写,public void initialize(int width, int height, int parentWidth, int parentHeight)初始化函数
protected void applyTransformation(float interpolatedTime, Transformation t) 动画效果函数
其中 参数 interpolatedTime 是一个0~1的浮点数,一直变化,直到动画结束。其持续时间和this.setDuration(参数);中参数有关,这个参数表示一个毫秒数,如2500表示2.5秒。
好了,上代码了,文章中的使用菜单,提供了几个效果。其中有的参数可以修改实施效果。
动画重写效果类:
package com.example.testanimation;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
public class TestAnimation extends Animation{
private Camera mCamera= null;
private float centerX , centerY;
private int index = -1;
public TestAnimation(int index) {
this.mCamera = new Camera();
this.index = index;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
// TODO Auto-generated method stub
super.initialize(width, height, parentWidth, parentHeight);
this.centerX = width/2;
this.centerY = height/2;
this.setDuration(2500);//set the play time
// this.setFillAfter(true);//stop move when animation over
this.setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
final Matrix matrix = t.getMatrix();//get the matrix
this.mCamera.save();
this.transformationStyle(interpolatedTime,matrix);
this.mCamera.getMatrix(matrix);
matrix.preTranslate(-this.centerX,-this.centerY);// keep the view at the center
matrix.postTranslate(this.centerX,this.centerY);
this.mCamera.restore();
}
private void transformationStyle(float interpolatedTime,Matrix matrix){
switch (index) {
case 0:
// this.mCamera.translate(0.0f, 0.0f, (1300.f - 1300.f * interpolatedTime));
this.mCamera.translate(0.0f,0.0f, 0.0f);
this.mCamera.rotateY(360 * interpolatedTime);
break;
case 1:
this.mCamera.translate(-1300.f*interpolatedTime,0.0f, 0.0f);//you can trun the '-' become '+',have a try
break;
case 2:
this.mCamera.translate(0.0f,-1300.f*interpolatedTime, 0.0f);
break;
case 3:
this.mCamera.translate(0.0f, 0.0f,1300.f*interpolatedTime);//make the view become smaller
break;
default:
break;
}
}
}
调用的activity:
package com.example.myanimation;
import com.example.testanimation.TestAnimation;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout ll = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.ll= (LinearLayout) this.findViewById(R.id.show_animation);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add(0, 0, 0, "rotate");
menu.add(0, 1, 0, "fly out_x");
menu.add(0, 2, 0, "fly out_y");
menu.add(0, 3, 0, "fly out_z");
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
// switch (item.getItemId()) {
// case 0:
//
// break;
// case 1:
//
// break;
// case 2:
//
// break;
// case 3:
//
// break;
//
// default:
// break;
// }
this.ll.startAnimation(new TestAnimation(item.getItemId()));
return super.onMenuItemSelected(featureId, item);
}
}
效果图:
选择界面:点击菜单
旋转效果
源码下载地址:http://download.csdn.net/detail/luinsist/4748095
如果各位有什么问题或者好的建议的话可以给我留言,我会抽空回复的