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

【读书笔记《Android游戏编程之从零开始》】16.游戏开发基础(动画)

2014-12-19 11:52 246 查看
1. Animation动画

在Android 中,系统提供了动画类 Animation ,其中又分为四种动画效果:
● AlphaAnimation:透明度渐变动画
● ScaleAnimation:渐变尺寸缩放动画;
● TranslateAnimation:移动动画
● RotateAnimation:旋转动画

这4种动画效果的创建方法如下:
(1) AlphaAnimation 透明度渐变动画
Animation alphaA = new AlphaAnimation(float fromAlpha,float toAlpha)
第一个参数:动画开始时的透明度
第二个参数:动画结束时的透明度
两个参数的取值范围为[0,1],从完全透明到完全不透明。

(2)ScaleAnimation 渐变尺寸缩放动画
Animation scaleA = new ScaleAnimation(float fromX,float toX,float fromY,float toY,int pivotXType,float pivotXValue,int pivoteYType,float pivoteYValue)
第一个参数:动画起始时X坐标上的伸缩比例
第二个参数:动画结束时X坐标上的伸缩比例
第三个参数:动画起始时Y坐标上的伸缩比例
第四个参数:动画结束时Y坐标上的伸缩比例
第五个参数:动画在X轴相对于物体的位置类型
第六个参数:动画相对于物体X坐标的位置
第七个参数:动画在Y轴相对于物体的位置类型
第八个参数:动画相对于物体Y坐标的位置
其中位置类型分为以下三种:
Animation.ABSOLUTE:相对位置是屏幕左上角,绝对位置;
Animation.RELATIVE_TO_SELF:相对位置是自身 View, 取值为 0 时,表示相对于是自身左上角,取值为1是相对于自身的右下角;
Animation.RELATIVE_TO_PAREND:相对父类 View 的位置。

(3)TranslateAnimation 移动动画
Animation translateA = new TranslateAnimation(float fromXDelta,float toXDelta,float fromYDelta,float YDelta)
第一个参数:动画起始时X轴上的位置
第二个参数:动画结束时X轴上的位置
第三个参数:动画起始时Y轴上的位置
第四个参数:动画结束时Y轴上的位置

(4)RotateAnimation 旋转动画
Animation rotateA = new RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,int pivotYType,float pivotYValue)

第一个参数:动画起始时旋转角度
第二个参数:动画旋转到的角度
第三个参数:动画在X轴相对于物件位置类型
第四个参数:动画相对于物件的X坐标的开始位置
第五个参数:动画在Y轴相对于物体的位置类型
第六个参数:动画相对于物体的Y坐标的位置

不管哪一种动画,都有一些通用方法,比如:
restart():重新播放动画;
setDuration(int time):设置动画播放时间,单位是毫秒。

下面是实例演示,效果如下:

package com.example.ex4_11;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {

// 用于控制SurfaceView 的大小、格式等,并且主要用于监听SurfaceView 的状态
private SurfaceHolder sfh;
// 声明一个画布
private Canvas canvas;
// 声明一个线程
private Thread th;
// 线程消亡的标识符
private boolean flag;
private Paint paint;
// 创建位图
private Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),
R.drawable.wifi_all);
// 记录当前播放帧
private int currentFrame;

public MySurfaceView(Context context) {
super(context);
// 实例SurfaceView
sfh = this.getHolder();
// 为SurfaceView添加状态监听
sfh.addCallback(this);
paint = new Paint();
// 画笔无锯齿
paint.setAntiAlias(true);

}

/**
* SurfaceView 视图创建,响应此函数
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
flag = true;
// 实例线程
th = new Thread(this);
// 启动线程
th.start();
}

/**
* SurfaceView 视图状态发生改变时,响应此函数
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

}

/**
* SurfaceView 视图消亡时,响应此函数
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}

private int minW = bmp.getWidth() / 6;

private void myDraw() {
try {
//居中点X轴坐标
int cW = this.getWidth() / 2 - minW / 2;
//居中点Y轴坐标
int cH = this.getHeight() / 2 - bmp.getHeight() / 2;
canvas = sfh.lockCanvas();
if (canvas != null) {
// 刷屏,画布白色
canvas.drawColor(Color.WHITE);
canvas.save();
// 设置画布的可视区域,大小为每帧的大小,居中
canvas.clipRect(cW, cH, cW + minW, cH + bmp.getHeight());
// 绘制位图,居中
canvas.drawBitmap(bmp, cW - currentFrame * minW, cH, paint);
canvas.restore();
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (canvas != null) {
sfh.unlockCanvasAndPost(canvas);
}
}
}

/**
* 游戏逻辑
*/
private void logic() {
currentFrame++;
// 当播放的当前帧大于并且等于帧数组时,重置当前帧为0
if (currentFrame >= 6) {
currentFrame = 0;
}
}

@Override
public void run() {
while (flag) {
long start = System.currentTimeMillis();
myDraw();
logic();
long end = System.currentTimeMillis();
try {
if (end - start < 500) {
Thread.sleep(500 - (end - start));
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

}


MySurfaceView
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐