Camera常用方法:
Public methods
void
applyToCanvas
(Canvas canvas)
Computes the matrix corresponding to the current transformation and applies it to the specified Canvas.
float
getLocationX()
Gets the x location of the camera.
floatgetLocationY()
Gets the y location of the camera.
float
getLocationZ()
Gets the z location of the camera.
void
getMatrix(Matrix matrix)
Computes the matrix corresponding to the current transformation and copies it to the supplied matrix object.
void
restore()
Restores the saved state, if any.
void
rotate(float x, float y, float z)
Applies a rotation transform around all three axis.
void
rotateX(float deg)
Applies a rotation transform around the X axis.
void
rotateY(float deg)
Applies a rotation transform around the Y axis.
void
rotateZ(float deg)
Applies a rotation transform around the Z axis.
void
save()
Saves the camera state.
void
setLocation(float x, float y, float z)
Sets the location of the camera.
void
translate(float x, float y, float z)
Applies a translation transform on all three axis.axis.
public class MyAnimation extends Animation{
//中心点位置和持续时间
private float centerX,centerY;
private int duration;
private Camera camera=new Camera();
public MyAnimation(float x,float y,int duration) {
this.centerX=x;
this.centerY=y;
this.duration=duration;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
// TODO Auto-generated method stub
super.initialize(width, height, parentWidth, parentHeight);
//设置持续时间
setDuration(duration);
//设置动画结束后效果保留
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
@Override
//参数分别代表动画的抽象持续时间(从0到1)
//和对目标组件动作变换
protected void applyTransformation(float interpolatedTime, Transformation t) {
camera.save();
//根据时间控制XYZ方向的偏移
camera.translate(100.0f-100.0f*interpolatedTime
, 150.0f*interpolatedTime-150
, 80.0f-80.0f*interpolatedTime);
//根据时间控制旋转的不同角度
camera.rotateY(360*(interpolatedTime));
camera.rotateX(360*(interpolatedTime));
//获取Transformation参数的Matrix对象
Matrix matrix=t.getMatrix();
camera.getMatrix(matrix);
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
camera.restore();
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list=(ListView)findViewById(R.id.list);
WindowManager windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
Display display=windowManager.getDefaultDisplay();
DisplayMetrics metrics=new DisplayMetrics();
//获取屏幕的宽高
display.getMetrics(metrics);
//为ListView组件设置动画
list.setAnimation(new MyAnimation(metrics.xdpi/2, metrics.ydpi/2, 3500));
}
}