2023年7月7日发(作者:)
Android游戏开发之旅一 长按Button原理
今天Android123开始新的Android游戏开发之旅系列,主要从控制方法(按键、轨迹球、触屏、重力感应、摄像头、话筒气流、光线亮度)、图形View(高效绘图技术如双缓冲)、音效(游戏音乐)以及最后的OpenGL ES(Java层)和NDK的OpenGL和J2ME游戏移植到Android方法,当然还有一些游戏实现惯用方法,比如地图编辑器,在Android OpenGL如何使用MD2文件,个部分讲述下Android游戏开发的过程最终实现一个比较完整的游戏引擎。相信大家都清楚Android
Market下载量比较好的都是游戏,未来手机网游的发展相信Android使用的Java在这方面有比iPhone有更低的入门门槛。
对于很多游戏使用屏幕控制一般需要考虑长按事件,比如在动作类的游戏中需要长按发射武器,结合Android Button模型,我们实现一个带图片的Button的长按,为了更清晰的显示原理,Android开发网这里使用ImageButton作为基类
public class RepeatingImageButton extends ImageButton {
private long mStartTime; //记录长按开始
private int mRepeatCount; //重复次数计数
private RepeatListener mListener;
private long mInterval = 500; //Timer触发间隔,即每0.5秒算一次按下
public RepeatingImageButton(Context context) {
this(context, null);
}
public RepeatingImageButton(Context context, AttributeSet attrs) {
this(context, attrs, uttonStyle);
}
public RepeatingImageButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true); //允许获得焦点
setLongClickable(true); //启用长按事件
}
public void setRepeatListener(RepeatListener l, long interval) { //实现重复按下事件listener
mListener = l;
mInterval = interval;
}
@Override
public boolean performLongClick() {
mStartTime = dRealtime();
mRepeatCount = 0; post(mRepeater);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (ion() == _UP) { // 本方法原理同onKeyUp的一样,这里处理屏幕事件,下面的onKeyUp处理Android手机上的物理按键事件
removeCallbacks(mRepeater);
if (mStartTime != 0) {
doRepeat(true);
mStartTime = 0;
}
}
return hEvent(event);
}
//处理导航键事件的中键或轨迹球按下事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case E_DPAD_CENTER:
case E_ENTER:
own(keyCode, event);
return true;
}
return own(keyCode, event);
}
//当按键弹起通知长按结束
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case E_DPAD_CENTER:
case E_ENTER:
removeCallbacks(mRepeater); //取消重复listener捕获
if (mStartTime != 0) {
doRepeat(true); //如果长按事件累计时间不为0则说明长按了
mStartTime = 0; //重置长按计时器 }
}
return p(keyCode, event);
}
private Runnable mRepeater = new Runnable() { //在线程中判断重复
public void run() {
doRepeat(false);
if (isPressed()) {
postDelayed(this, mInterval); //计算长按后延迟下一次累加
}
}
};
private void doRepeat(boolean last) {
long now = dRealtime();
if (mListener != null) {
at(this, now - mStartTime, last ? -1 : mRepeatCount++);
}
}
下面是重复Button Listener接口的定义,调用时在Button中先使用setRepeatListener()方法实现RepeatListener接口
public interface RepeatListener {
void onRepeat(View v, long duration, int repeatcount); //参数一为用户传入的Button对象,参数二为延迟的毫秒数,第三位重复次数回调。
}
}
Android游戏开发之旅二 View和SurfaceView
在Android游戏当中充当主要的除了控制类外就是显示类,在J2ME中我们用Display和Canvas来实现这些,而Google Android中涉及到显示的为view类,Android游戏开发中比较重要和复杂的就是显示和游戏逻辑的处理。这里我们说下和eView。SurfaceView是从View基类中派生出来的显示类,直接子类有GLSurfaceView和VideoView,可以看出GL和视频播放以及Camera摄像头一般均使用SurfaceView,到底有哪些优势呢? SurfaceView可以控制表面的格式,比如大小,显示在屏幕中的位置,最关键是的提供了SurfaceHolder类,使用getHolder方法获取,相关的有Canvas lockCanvas()
Canvas lockCanvas(Rect dirty) 、void removeCallback(ck callback)、void unlockCanvasAndPost(Canvas canvas) 控制图形以及绘制,而在ck 接口回调中可以通过下面三个抽象类可以自己定义具体的实现,比如第一个更改格式和显示画面。 abstract void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
abstract void surfaceCreated(SurfaceHolder holder)
abstract void surfaceDestroyed(SurfaceHolder holder)
对于Surface相关的,Android底层还提供了GPU加速功能,所以一般实时性很强的应用中主要使用SurfaceView而不是直接从View构建,同时Android123未来后面说到的OpenGL中的GLSurfaceView也是从该类实现。
Android游戏开发之旅三 View类详解
在Android游戏开发之旅二中我们讲到了View和SurfaceView的区别,今天Android123从View类开始着重的介绍Android图形显示基类的相关方法和注意点。
自定义View的常用方法:
onFinishInflate() 当View中所有的子控件均被映射成xml后触发
onMeasure(int, int) 确定所有子元素的大小
onLayout(boolean, int, int, int, int) 当View分配所有的子元素的大小和位置时触发
onSizeChanged(int, int, int, int) 当view的大小发生变化时触发
onDraw(Canvas) view渲染内容的细节
onKeyDown(int, KeyEvent) 有按键按下后触发
onKeyUp(int, KeyEvent) 有按键按下后弹起时触发
onTrackballEvent(MotionEvent) 轨迹球事件
onTouchEvent(MotionEvent) 触屏事件
onFocusChanged(boolean, int, Rect) 当View获取或失去焦点时触发
onWindowFocusChanged(boolean) 当窗口包含的view获取或失去焦点时触发
onAttachedToWindow() 当view被附着到一个窗口时触发
onDetachedFromWindow() 当view离开附着的窗口时触发,Android123提示该方法和 onAttachedToWindow() 是相反的。
onWindowVisibilityChanged(int) 当窗口中包含的可见的view发生变化时触发 以上是View实现的一些基本接口的回调方法,一般我们需要处理画布的显示时,重写onDraw(Canvas)用的的是最多的:
@Override
protected void onDraw(Canvas canvas) {
//这里我们直接使用canvas对象处理当前的画布,比如说使用Paint来选择要填充的颜色
Paint paintBackground = new Paint();
or(getResources().getColor()); //从Res中找到名为xxx的color颜色定义
ct(0, 0, getWidth(), getHeight(), paintBackground); //设置当前画布的背景颜色为paintBackground中定义的颜色,以0,0作为为起点,以当前画布的宽度和高度为重点即整块画布来填充。
具体的请查看Android123未来讲到的Canvas和Paint,在Canvas中我们可以实现画路径,图形,区域,线。而Paint作为绘画方式的对象可以设置颜色,大小,甚至字体的类型等等。
}
当然还有就是处理窗口还原状态问题(一般用于横竖屏切换),除了在Activity中可以调用外,开发游戏时我们尽量在View中使用类似
@Override
protected Parcelable onSaveInstanceState() {
Parcelable p = InstanceState();
Bundle bundle = new Bundle();
("x", pX);
("y", pY);
celable("android123_state", p);
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle bundle = (Bundle) state;
dosomething(("x"), ("y")); //获取刚才存储的x和y信息
oreInstanceState(celable("android123_state"));
return;
}
在View中如果需要强制调用绘制方法onDraw,可以使用invalidate()方法,它有很多重载版本,同时在线程中的postInvailidate()方法将在Android游戏开发之旅六中的 自定义View完整篇讲到。 Android游戏开发之旅四 Canvas和Paint实例
昨天我们在Android游戏开发之旅三 View详解中提到了onDraw方法,有关详细的实现我们今天主要说下Android的Canvas和Paint对象的使用实例。
Canvas类主要实现了屏幕的绘制过程,其中包含了很多实用的方法,比如绘制一条路径、区域、贴图、画点、画线、渲染文本,下面是Canvas类常用的方法,当然Android开发网提示大家很多方法有不同的重载版本,参数更灵活。
void drawRect(RectF rect, Paint paint) //绘制区域,参数一为RectF一个区域
void drawPath(Path path, Paint paint) //绘制一个路径,参数一为Path路径对象
void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) //贴图,参数一就是我们常规的Bitmap对象,参数二是源区域(Android123提示这里是bitmap),参数三是目标区域(应该在canvas的位置和大小),参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。
void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) //画线,参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,最后一个参数为Paint画刷对象。
void drawPoint(float x, float y, Paint paint) //画点,参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。
void drawText(String text, float x, float y, Paint paint) //渲染文本,Canvas类除了上面的还可以描绘文字,参数一是String类型的文本,参数二x轴,参数三y轴,参数四是Paint对象。
void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) //在路径上绘制文本,相对于上面第二个参数是Path路径对象
从上面来看我们可以看出Canvas绘制类比较简单同时很灵活,实现一般的方法通常没有问题,同时可以叠加的处理设计出一些效果,不过细心的网友可能发现最后一个参数均为Paint对象。如果我们把Canvas当做绘画师来看,那么Paint就是我们绘画的工具,比如画笔、画刷、颜料等等。
Paint类常用方法:
void setARGB(int a, int r, int g, int b) 设置Paint对象颜色,参数一为alpha透明通道
void setAlpha(int a) 设置alpha不透明度,范围为0~255
void setAntiAlias(boolean aa) //是否抗锯齿 void setColor(int color) //设置颜色,这里Android内部定义的有Color类包含了一些常见颜色定义
.
void setFakeBoldText(boolean fakeBoldText) //设置伪粗体文本
void setLinearText(boolean linearText) //设置线性文本
PathEffect setPathEffect(PathEffect effect) //设置路径效果
Rasterizer setRasterizer(Rasterizer rasterizer) //设置光栅化
Shader setShader(Shader shader) //设置阴影
void setTextAlign( align) //设置文本对齐
void setTextScaleX(float scaleX) //设置文本缩放倍数,1.0f为原始
void setTextSize(float textSize) //设置字体大小
Typeface setTypeface(Typeface typeface) //设置字体,Typeface包含了字体的类型,粗细,还有倾斜、颜色等。
void setUnderlineText(boolean underlineText) //设置下划线
最终Canvas和Paint在onDraw中直接使用
@Override
protected void onDraw(Canvas canvas) {
Paint paintRed=new Paint();
or();
int(11,3,paintRed); //在坐标11,3上画一个红点
}
下一次Android123将会具体讲到强大的Path路径,和字体Typeface相关的使用。
Android游戏开发之旅(五)Path和Typeface
今天我们继续处理上次 Android游戏开发之旅(四)Canvas和Paint实例 中提到的Path路径和Typeface字体两个类。对于Android游戏开发或者说2D绘图中来讲Path 路径可以用强大这个词来形容。在Photoshop中我们可能还记得使用钢笔工具绘制路径的方法。Path路径类在位于 中,Path的构造方法比较简单,如下 Path cwj=new Path(); //构造方法
复制代码
下面我们画一个封闭的原型路径,我们使用Path类的addCircle方法
cle(10,10,50,); //参数一为x轴水平位置,参数二为y轴垂直位置,第三个参数为圆形的半径,最后是绘制的方向,CW为顺时针方向,而CCW是逆时针方向
复制代码
结合Android上次提到的Canvas类中的绘制方法drawPath和drawTextOnPath,我们继续可以在onDraw中加入。
th(cwj,paintPath); //Android123提示大家这里paintPath为路径的画刷颜色,可以见下文完整的源代码。
xtOnPath("Android123 - CWJ",cwj,0,15,paintText); //将文字绘制到路径中去,
复制代码
有关drawTextOnPath的参数如下:
方法原型
public void drawTextOnPath (String text, Path path, float hOffset, float vOffset, Paint paint)
复制代码
参数列表
text 为需要在路径上绘制的文字内容。
path 将文字绘制到哪个路径。
hOffset 距离路径开始的距离
vOffset 离路径的上下高度,这里Android开发网提示大家,该参数类型为float浮点型,除了精度为8位小数外,可以为正或负,当为正时文字在路径的圈里面,为负时在路径的圈外面。
paint 最后仍然是一个Paint对象用于制定Text本文的颜色、字体、大小等属性。
下面是我们的onDraw方法中如何绘制路径的演示代码为:
@Override
protected void onDraw(Canvas canvas) {
Paint paintPath=new Paint();
Paint paintText=new Paint();
or(); //路径的画刷为红色
or(); //路径上的文字为蓝色
Path pathCWJ=new Path();
cle(10,10,50,); // 半径为50px,绘制的方向CW为顺时针
th(pathCWJ,paintPath);
xtOnPath("Android123 - CWJ",pathCWJ,0,15,paintText); //在路径上绘制文字
}
复制代码
有关路径类常用的方法如下:
void addArc(RectF oval, float startAngle, float sweepAngle) //为路径添加一个多边形
void addCircle(float x, float y, float radius, ion dir) //给path添加圆圈
void addOval(RectF oval, ion dir) //添加椭圆形
void addRect(RectF rect, ion dir) //添加一个区域
void addRoundRect(RectF rect, float[] radii, ion dir) //添加一个圆角区域
boolean isEmpty() //判断路径是否为空
void transform(Matrix matrix) //应用矩阵变换
void transform(Matrix matrix, Path dst) //应用矩阵变换并将结果放到新的路径中,即第二个参数。
复制代码
有关路径的高级效果大家可以使用PathEffect类,有关路径的更多实例Android123将在今后的游戏开发实战中讲解道。
Typeface字体类
平时我们在TextView中需要设置显示的字体可以通过TextView中的setTypeface方法来指定一个Typeface对象,因为 Android的字体类比较简单,我们列出所有成员方法
static Typeface create(Typeface family, int style) //静态方法,参数一为字体类型这里是Typeface的静态定义,如宋体,参数二风格,如粗体,斜体
static Typeface create(String familyName, int style) //静态方法,参数一为字体名的字符串,参数二为风格同上,这里我们推荐使用上面的方法。
static Typeface createFromAsset(AssetManager mgr, String path) //静态方法,参数一为AssetManager对象,主要用于从APK的assets文件夹中取出字体,参数二为相对于Android工程下的 assets文件夹中的外挂字体文件的路径。
static Typeface createFromFile(File path) //静态方法,从文件系统构造一个字体,这里参数可以是sdcard中的某个字体文件
static Typeface createFromFile(String path) //静态方法,从指定路径中构造字体
static Typeface defaultFromStyle(int style) //静态方法,返回默认的字体风格
int getStyle() //获取当前字体风格
final boolean isBold() //判断当前是否为粗体
final boolean isItalic() //判断当前风格是否为斜体
复制代码
本类的常量静态定义,首先为字体类型名称
Typeface DEFAULT
Typeface DEFAULT_BOLD
Typeface MONOSPACE
Typeface SANS_SERIF
Typeface SERIF
字体风格名称
int BOLD
int BOLD_ITALIC
int ITALIC
int NORMAL
明天我们将在 Android游戏开发之旅六 自定义View 一文中具体讲解onDraw以及什么时候会触发绘制方法,来实现我们自定义或子类化控件。 Android游戏开发之旅六 自定义View
有关Android的自定义View的框架今天我们一起讨论下,对于常规的游戏,我们在View中需要处理以下几种问题: 1.控制事件 2.刷新View 3. 绘制View
1. 对于控制事件今天我们只处理按键事件onKeyDown,以后的文章中将会讲到屏幕触控的具体处理onTouchEvent以及Sensor重力感应等方法。
2. 刷新view的方法这里主要有invalidate(int l, int t, int r, int b) 刷新局部,四个参数分别为左、上、右、下。整个view刷新 invalidate(),刷新一个矩形区域 invalidate(Rect dirty) ,刷新一个特性Drawable, invalidateDrawable(Drawable drawable) ,执行invalidate类的方法将会设置view为无效,最终导致onDraw方法被重新调用。由于今天的view比较简单,Android123提示大家如果在线程中刷新,除了使用handler方式外,可以在Thread中直接使用postInvalidate方法来实现。
3. 绘制View主要是onDraw()中通过形参canvas来处理,相关的绘制主要有drawRect、drawLine、drawPath等等。view方法内部还重写了很多接口,其回调方法可以帮助我们判断出view的位置和大小,比如onMeasure(int, int) Called to determine the size requirements for this
view and all of its children. 、onLayout(boolean, int, int, int, int) Called when this view should
assign a size and position to all of its children 和onSizeChanged(int, int, int, int) Called when
the size of this view has changed. 具体的作用,大家可以用Logcat获取当view变化时每个形参的变动。
下面cwjView是我们为今后游戏设计的一个简单自定义View框架,我们可以看到在Android平台自定义view还是很简单的,同时Java支持多继承可以帮助我们不断的完善复杂的问题。
public class cwjView extends View {
public cwjView(Context context) {
super(context);
setFocusable(true); //允许获得焦点
setFocusableInTouchMode(true); //获取焦点时允许触控
}
@Override
protected Parcelable onSaveInstanceState() { //处理窗口保存事件
Parcelable pSaved = InstanceState();
Bundle bundle = new Bundle();
//dosomething
return bundle; }
@Override
protected void onRestoreInstanceState(Parcelable state) { //处理窗口还原事件
Bundle bundle = (Bundle) state;
//dosomething
oreInstanceState(celable("cwj"));
return;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) //处理窗口大小变化事件
{
Changed(w, h, oldw, oldh);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
ure(widthMeasureSpec, heightMeasureSpec); //如果不让父类处理记住调用setMeasuredDimension
}
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom)
{
ut (changed,left,top, ight,bottom) ;
}
@Override
protected void onDraw(Canvas canvas) {
Paint bg = new Paint();
or();
ct(0, 0, getWidth()/2, getHeight()/2, bg); //将view的左上角四分之一填充为红色
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return hEvent(event); //让父类处理屏幕触控事件
} @Override
public boolean onKeyDown(int keyCode, KeyEvent event) { //处理按键事件,响应的轨迹球事件为 public boolean onTrackballEvent (MotionEvent event)
switch (keyCode) {
case E_DPAD_UP:
break;
case E_DPAD_DOWN:
break;
case E_DPAD_LEFT:
break;
case E_DPAD_RIGHT:
break;
case E_DPAD_CENTER: //处理中键按下
break;
default:
return own(keyCode, event);
}
return true;
}
}
上面我们可以看到onMeasure使用的是父类的处理方法,如果我们需要解决自定义View的大小,可以尝试下面的方法
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
height = e(heightMeasureSpec);
width = e(widthMeasureSpec);
setMeasuredDimension(width,height); //这里面是原始的大小,需要重新计算可以修改本行
//dosomething
} Android游戏开发之旅七 自定义SurfaceView
今天我们说下未来的Android游戏引擎模板架构问题,对于游戏我们还是选择SurfaceView,相关的原因Android123已经在Android游戏开发之旅二 View和SurfaceView中说的很清楚了,这里我们直接继承SurfaceView,实现ck接口,处理surfaceCreated、surfaceChanged以及surfaceDestroyed方法,这里我们并没有把按键控制传入,最终游戏的控制方面仍然由View内部类处理比较好,有关SurfaceView的具体我们可以参见Android开源项目的Camera中有关画面捕捉以及VideoView的控件实现大家可以清晰了解最终的用意。
public class cwjView extends SurfaceView implements ck {
public cwjView(Context context, AttributeSet attrs) {
super(context, attrs);
SurfaceHolder holder=getHolder();
lback(this);
setFocusable(true);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
}
} Android游戏开发之旅八 SurfaceView类实例
有关SurfaceView我们将通过三个系统自带的例子来深入掌握Android绘图必会的SurfaceView,今天我们以SDK中的Sample游戏lunarlander中的LunarView具体实现,Android123建议大家导入该游戏工程到你的Eclipse然后自己编译先玩一下这个游戏,然后再看代码比较好理解。
class LunarView extends SurfaceView implements ck {
class LunarThread extends Thread {
/*
* Difficulty setting constants
*/
public static final int DIFFICULTY_EASY = 0;
public static final int DIFFICULTY_HARD = 1;
public static final int DIFFICULTY_MEDIUM = 2;
/*
* Physics constants
*/
public static final int PHYS_DOWN_ACCEL_SEC = 35;
public static final int PHYS_FIRE_ACCEL_SEC = 80;
public static final int PHYS_FUEL_INIT = 60;
public static final int PHYS_FUEL_MAX = 100;
public static final int PHYS_FUEL_SEC = 10;
public static final int PHYS_SLEW_SEC = 120; // degrees/second rotate
public static final int PHYS_SPEED_HYPERSPACE = 180;
public static final int PHYS_SPEED_INIT = 30;
public static final int PHYS_SPEED_MAX = 120;
/*
* State-tracking constants
*/
public static final int STATE_LOSE = 1;
public static final int STATE_PAUSE = 2;
public static final int STATE_READY = 3;
public static final int STATE_RUNNING = 4;
public static final int STATE_WIN = 5;
/*
* Goal condition constants
*/
public static final int TARGET_ANGLE = 18; // > this angle means crash
public static final int TARGET_BOTTOM_PADDING = 17; // px below gear
public static final int TARGET_PAD_HEIGHT = 8; // how high above ground
public static final int TARGET_SPEED = 28; // > this speed means crash
public static final double TARGET_WIDTH = 1.6; // width of target /*
* UI constants (i.e. the speed & fuel bars)
*/
public static final int UI_BAR = 100; // width of the bar(s)
public static final int UI_BAR_HEIGHT = 10; // height of the bar(s)
private static final String KEY_DIFFICULTY = "mDifficulty";
private static final String KEY_DX = "mDX";
private static final String KEY_DY = "mDY";
private static final String KEY_FUEL = "mFuel";
private static final String KEY_GOAL_ANGLE = "mGoalAngle";
private static final String KEY_GOAL_SPEED = "mGoalSpeed";
private static final String KEY_GOAL_WIDTH = "mGoalWidth";
private static final String KEY_GOAL_X = "mGoalX";
private static final String KEY_HEADING = "mHeading";
private static final String KEY_LANDER_HEIGHT = "mLanderHeight";
private static final String KEY_LANDER_WIDTH = "mLanderWidth";
private static final String KEY_WINS = "mWinsInARow";
private static final String KEY_X = "mX";
private static final String KEY_Y = "mY";
/*
* Member (state) fields
*/
/** The drawable to use as the background of the animation canvas */
private Bitmap mBackgroundImage;
/**
* Current height of the surface/canvas.
*
* @see #setSurfaceSize
*/
private int mCanvasHeight = 1;
/**
* Current width of the surface/canvas.
*
* @see #setSurfaceSize
*/
private int mCanvasWidth = 1;
/** What to draw for the Lander when it has crashed */
private Drawable mCrashedImage; /**
* Current difficulty -- amount of fuel, allowed angle, etc. Default is
* MEDIUM.
*/
private int mDifficulty;
/** Velocity dx. */
private double mDX;
/** Velocity dy. */
private double mDY;
/** Is the engine burning? */
private boolean mEngineFiring;
/** What to draw for the Lander when the engine is firing */
private Drawable mFiringImage;
/** Fuel remaining */
private double mFuel;
/** Allowed angle. */
private int mGoalAngle;
/** Allowed speed. */
private int mGoalSpeed;
/** Width of the landing pad. */
private int mGoalWidth;
/** X of the landing pad. */
private int mGoalX;
/** Message handler used by thread to interact with TextView */
private Handler mHandler;
/**
* Lander heading in degrees, with 0 up, 90 right. Kept in the range
* 0..360.
*/
private double mHeading;
/** Pixel height of lander image. */
private int mLanderHeight; /** What to draw for the Lander in its normal state */
private Drawable mLanderImage;
/** Pixel width of lander image. */
private int mLanderWidth;
/** Used to figure out elapsed time between frames */
private long mLastTime;
/** Paint to draw the lines on screen. */
private Paint mLinePaint;
/** "Bad" speed-too-high variant of the line color. */
private Paint mLinePaintBad;
/** The state of the game. One of READY, RUNNING, PAUSE, LOSE, or WIN */
private int mMode;
/** Currently rotating, -1 left, 0 none, 1 right. */
private int mRotating;
/** Indicate whether the surface has been created & is ready to draw */
private boolean mRun = false;
/** Scratch rect object. */
private RectF mScratchRect;
/** Handle to the surface manager object we interact with */
private SurfaceHolder mSurfaceHolder;
/** Number of wins in a row. */
private int mWinsInARow;
/** X of lander center. */
private double mX;
/** Y of lander center. */
private double mY;
public LunarThread(SurfaceHolder surfaceHolder, Context context,
Handler handler) {
// get handles to some important objects
mSurfaceHolder = surfaceHolder;
mHandler = handler;
mContext = context; Resources res = ources();
// cache handles to our key sprites & other drawables
mLanderImage = ources().getDrawable(
_plain);
mFiringImage = ources().getDrawable(
_firing);
mCrashedImage = ources().getDrawable(
_crashed);
// load background image as a Bitmap instead of a Drawable b/c
// we don't need to transform it and it's faster to draw this way
mBackgroundImage = Resource(res,
ise);
// Use the regular lander image as the model size for all sprites
mLanderWidth = rinsicWidth();
mLanderHeight = rinsicHeight();
// Initialize paints for speedometer
mLinePaint = new Paint();
iAlias(true);
B(255, 0, 255, 0);
mLinePaintBad = new Paint();
iAlias(true);
B(255, 120, 180, 0);
mScratchRect = new RectF(0, 0, 0, 0);
mWinsInARow = 0;
mDifficulty = DIFFICULTY_MEDIUM;
// initial show-up of lander (not yet playing)
mX = mLanderWidth;
mY = mLanderHeight * 2;
mFuel = PHYS_FUEL_INIT;
mDX = 0;
mDY = 0;
mHeading = 0;
mEngineFiring = true;
}
/**
* Starts the game, setting parameters for the current difficulty.
*/ public void doStart() {
synchronized (mSurfaceHolder) {
// First set the game for Medium difficulty
mFuel = PHYS_FUEL_INIT;
mEngineFiring = false;
mGoalWidth = (int) (mLanderWidth * TARGET_WIDTH);
mGoalSpeed = TARGET_SPEED;
mGoalAngle = TARGET_ANGLE;
int speedInit = PHYS_SPEED_INIT;
// Adjust difficulty params for EASY/HARD
if (mDifficulty == DIFFICULTY_EASY) {
mFuel = mFuel * 3 / 2;
mGoalWidth = mGoalWidth * 4 / 3;
mGoalSpeed = mGoalSpeed * 3 / 2;
mGoalAngle = mGoalAngle * 4 / 3;
speedInit = speedInit * 3 / 4;
} else if (mDifficulty == DIFFICULTY_HARD) {
mFuel = mFuel * 7 / 8;
mGoalWidth = mGoalWidth * 3 / 4;
mGoalSpeed = mGoalSpeed * 7 / 8;
speedInit = speedInit * 4 / 3;
}
// pick a convenient initial location for the lander sprite
mX = mCanvasWidth / 2;
mY = mCanvasHeight - mLanderHeight / 2;
// start with a little random motion
mDY = () * -speedInit;
mDX = () * 2 * speedInit - speedInit;
mHeading = 0;
// Figure initial spot for landing, not too near center
while (true) {
mGoalX = (int) (() * (mCanvasWidth - mGoalWidth));
if ((mGoalX - (mX - mLanderWidth / 2)) > mCanvasHeight / 6)
break;
}
mLastTime = tTimeMillis() + 100;
setState(STATE_RUNNING);
}
} /**
* Pauses the physics update & animation.
*/
public void pause() {
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING) setState(STATE_PAUSE);
}
}
/**
* Restores game state from the indicated Bundle. Typically called when
* the Activity is being restored after having been previously
* destroyed.
*
* @param savedState Bundle containing the game state
*/
public synchronized void restoreState(Bundle savedState) {
synchronized (mSurfaceHolder) {
setState(STATE_PAUSE);
mRotating = 0;
mEngineFiring = false;
mDifficulty = (KEY_DIFFICULTY);
mX = ble(KEY_X);
mY = ble(KEY_Y);
mDX = ble(KEY_DX);
mDY = ble(KEY_DY);
mHeading = ble(KEY_HEADING);
mLanderWidth = (KEY_LANDER_WIDTH);
mLanderHeight = (KEY_LANDER_HEIGHT);
mGoalX = (KEY_GOAL_X);
mGoalSpeed = (KEY_GOAL_SPEED);
mGoalAngle = (KEY_GOAL_ANGLE);
mGoalWidth = (KEY_GOAL_WIDTH);
mWinsInARow = (KEY_WINS);
mFuel = ble(KEY_FUEL);
}
}
@Override
public void run() {
while (mRun) {
Canvas c = null;
try { c = nvas(null);
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING) updatePhysics();
doDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
CanvasAndPost(c);
}
}
}
}
/**
* Dump game state to the provided Bundle. Typically called when the
* Activity is being suspended.
*
* @return Bundle with this view's state
*/
public Bundle saveState(Bundle map) {
synchronized (mSurfaceHolder) {
if (map != null) {
(KEY_DIFFICULTY, f(mDifficulty));
ble(KEY_X, f(mX));
ble(KEY_Y, f(mY));
ble(KEY_DX, f(mDX));
ble(KEY_DY, f(mDY));
ble(KEY_HEADING, f(mHeading));
(KEY_LANDER_WIDTH, f(mLanderWidth));
(KEY_LANDER_HEIGHT, Integer
.valueOf(mLanderHeight));
(KEY_GOAL_X, f(mGoalX));
(KEY_GOAL_SPEED, f(mGoalSpeed));
(KEY_GOAL_ANGLE, f(mGoalAngle));
(KEY_GOAL_WIDTH, f(mGoalWidth));
(KEY_WINS, f(mWinsInARow));
ble(KEY_FUEL, f(mFuel));
}
}
return map;
} /**
* Sets the current difficulty.
*
* @param difficulty
*/
public void setDifficulty(int difficulty) {
synchronized (mSurfaceHolder) {
mDifficulty = difficulty;
}
}
/**
* Sets if the engine is currently firing.
*/
public void setFiring(boolean firing) {
synchronized (mSurfaceHolder) {
mEngineFiring = firing;
}
}
/**
* Used to signal the thread whether it should be running or not.
* Passing true allows the thread to run; passing false will shut it
* down if it's already running. Calling start() after this was most
* recently called with false will result in an immediate shutdown.
*
* @param b true to run, false to shut down
*/
public void setRunning(boolean b) {
mRun = b;
}
/**
* Sets the game mode. That is, whether we are running, paused, in the
* failure state, in the victory state, etc.
*
* @see #setState(int, CharSequence)
* @param mode one of the STATE_* constants
*/
public void setState(int mode) {
synchronized (mSurfaceHolder) {
setState(mode, null);
}
} /**
* Sets the game mode. That is, whether we are running, paused, in the
* failure state, in the victory state, etc.
*
* @param mode one of the STATE_* constants
* @param message string to add to screen or null
*/
public void setState(int mode, CharSequence message) {
/*
* This method optionally can cause a text message to be displayed
* to the user when the mode changes. Since the View that actually
* renders that text is part of the main View hierarchy and not
* owned by this thread, we can't touch the state of that View.
* Instead we use a Message + Handler to relay commands to the main
* thread, which updates the user-text View.
*/
synchronized (mSurfaceHolder) {
mMode = mode;
if (mMode == STATE_RUNNING) {
Message msg = Message();
Bundle b = new Bundle();
ing("text", "");
("viz", BLE);
a(b);
ssage(msg);
} else {
mRotating = 0;
mEngineFiring = false;
Resources res = ources();
CharSequence str = "";
if (mMode == STATE_READY)
str = t(_ready);
else if (mMode == STATE_PAUSE)
str = t(_pause);
else if (mMode == STATE_LOSE)
str = t(_lose);
else if (mMode == STATE_WIN)
str = ing(_win_prefix)
+ mWinsInARow + " "
+ ing(_win_suffix);
if (message != null) {
str = message + "n" + str;
} if (mMode == STATE_LOSE) mWinsInARow = 0;
Message msg = Message();
Bundle b = new Bundle();
ing("text", ng());
("viz", E);
a(b);
ssage(msg);
}
}
}
/* Callback invoked when the surface dimensions change. */
public void setSurfaceSize(int width, int height) {
// synchronized to make sure these all change atomically
synchronized (mSurfaceHolder) {
mCanvasWidth = width;
mCanvasHeight = height;
// don't forget to resize the background image
mBackgroundImage = ScaledBitmap(
mBackgroundImage, width, height, true);
}
}
/**
* Resumes from a pause.
*/
public void unpause() {
// Move the real time clock up to now
synchronized (mSurfaceHolder) {
mLastTime = tTimeMillis() + 100;
}
setState(STATE_RUNNING);
}
/**
* Handles a key-down event.
*
* @param keyCode the key that was pressed
* @param msg the original event object
* @return true
*/
boolean doKeyDown(int keyCode, KeyEvent msg) {
synchronized (mSurfaceHolder) { boolean okStart = false;
if (keyCode == E_DPAD_UP) okStart = true;
if (keyCode == E_DPAD_DOWN) okStart = true;
if (keyCode == E_S) okStart = true;
boolean center = (keyCode == E_DPAD_UP);
if (okStart
&& (mMode == STATE_READY || mMode == STATE_LOSE || mMode ==
STATE_WIN)) {
// ready-to-start -> start
doStart();
return true;
} else if (mMode == STATE_PAUSE && okStart) {
// paused -> running
unpause();
return true;
} else if (mMode == STATE_RUNNING) {
// center/space -> fire
if (keyCode == E_DPAD_CENTER
|| keyCode == E_SPACE) {
setFiring(true);
return true;
// left/q -> left
} else if (keyCode == E_DPAD_LEFT
|| keyCode == E_Q) {
mRotating = -1;
return true;
// right/w -> right
} else if (keyCode == E_DPAD_RIGHT
|| keyCode == E_W) {
mRotating = 1;
return true;
// up -> pause
} else if (keyCode == E_DPAD_UP) {
pause();
return true;
}
}
return false;
}
} /**
* Handles a key-up event.
*
* @param keyCode the key that was pressed
* @param msg the original event object
* @return true if the key was handled and consumed, or else false
*/
boolean doKeyUp(int keyCode, KeyEvent msg) {
boolean handled = false;
synchronized (mSurfaceHolder) {
if (mMode == STATE_RUNNING) {
if (keyCode == E_DPAD_CENTER
|| keyCode == E_SPACE) {
setFiring(false);
handled = true;
} else if (keyCode == E_DPAD_LEFT
|| keyCode == E_Q
|| keyCode == E_DPAD_RIGHT
|| keyCode == E_W) {
mRotating = 0;
handled = true;
}
}
}
return handled;
}
/**
* Draws the ship, fuel/speed bars, and background to the provided
* Canvas.
*/
private void doDraw(Canvas canvas) {
// Draw the background image. Operations on the Canvas accumulate
// so this is like clearing the screen.
tmap(mBackgroundImage, 0, 0, null);
int yTop = mCanvasHeight - ((int) mY + mLanderHeight / 2);
int xLeft = (int) mX - mLanderWidth / 2;
// Draw the fuel gauge
int fuelWidth = (int) (UI_BAR * mFuel / PHYS_FUEL_MAX);
(4, 4, 4 + fuelWidth, 4 + UI_BAR_HEIGHT);
ct(mScratchRect, mLinePaint); // Draw the speed gauge, with a two-tone effect
double speed = (mDX * mDX + mDY * mDY);
int speedWidth = (int) (UI_BAR * speed / PHYS_SPEED_MAX);
if (speed <= mGoalSpeed) {
(4 + UI_BAR + 4, 4,
4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT);
ct(mScratchRect, mLinePaint);
} else {
// Draw the bad color in back, with the good color in front of
// it
(4 + UI_BAR + 4, 4,
4 + UI_BAR + 4 + speedWidth, 4 + UI_BAR_HEIGHT);
ct(mScratchRect, mLinePaintBad);
int goalWidth = (UI_BAR * mGoalSpeed / PHYS_SPEED_MAX);
(4 + UI_BAR + 4, 4, 4 + UI_BAR + 4 + goalWidth,
4 + UI_BAR_HEIGHT);
ct(mScratchRect, mLinePaint);
}
// Draw the landing pad
ne(mGoalX, 1 + mCanvasHeight - TARGET_PAD_HEIGHT,
mGoalX + mGoalWidth, 1 + mCanvasHeight - TARGET_PAD_HEIGHT,
mLinePaint);
// Draw the ship with its current rotation
();
((float) mHeading, (float) mX, mCanvasHeight
- (float) mY);
if (mMode == STATE_LOSE) {
nds(xLeft, yTop, xLeft + mLanderWidth, yTop
+ mLanderHeight);
(canvas);
} else if (mEngineFiring) {
nds(xLeft, yTop, xLeft + mLanderWidth, yTop
+ mLanderHeight);
(canvas);
} else {
nds(xLeft, yTop, xLeft + mLanderWidth, yTop
+ mLanderHeight);
(canvas);
}
e();
} /**
* Figures the lander state (x, y, fuel, ...) based on the passage of
* realtime. Does not invalidate(). Called at the start of draw().
* Detects the end-of-game and sets the UI to the next state.
*/
private void updatePhysics() {
long now = tTimeMillis();
// Do nothing if mLastTime is in the future.
// This allows the game-start to delay the start of the physics
// by 100ms or whatever.
if (mLastTime > now) return;
double elapsed = (now - mLastTime) / 1000.0;
// mRotating -- update heading
if (mRotating != 0) {
mHeading += mRotating * (PHYS_SLEW_SEC * elapsed);
// Bring things back into the range 0..360
if (mHeading < 0)
mHeading += 360;
else if (mHeading >= 360) mHeading -= 360;
}
// Base accelerations -- 0 for x, gravity for y
double ddx = 0.0;
double ddy = -PHYS_DOWN_ACCEL_SEC * elapsed;
if (mEngineFiring) {
// taking 0 as up, 90 as to the right
// cos(deg) is ddy component, sin(deg) is ddx component
double elapsedFiring = elapsed;
double fuelUsed = elapsedFiring * PHYS_FUEL_SEC;
// tricky case where we run out of fuel partway through the
// elapsed
if (fuelUsed > mFuel) {
elapsedFiring = mFuel / fuelUsed * elapsed;
fuelUsed = mFuel;
// Oddball case where we adjust the "control" from here
mEngineFiring = false;
} mFuel -= fuelUsed;
// have this much acceleration from the engine
double accel = PHYS_FIRE_ACCEL_SEC * elapsedFiring;
double radians = 2 * * mHeading / 360;
ddx = (radians) * accel;
ddy += (radians) * accel;
}
double dxOld = mDX;
double dyOld = mDY;
// figure speeds for the end of the period
mDX += ddx;
mDY += ddy;
// figure position based on average speed during the period
mX += elapsed * (mDX + dxOld) / 2;
mY += elapsed * (mDY + dyOld) / 2;
mLastTime = now;
// Evaluate if we have landed ... stop the game
double yLowerBound = TARGET_PAD_HEIGHT + mLanderHeight / 2
- TARGET_BOTTOM_PADDING;
if (mY <= yLowerBound) {
mY = yLowerBound;
int result = STATE_LOSE;
CharSequence message = "";
Resources res = ources();
double speed = (mDX * mDX + mDY * mDY);
boolean onGoal = (mGoalX <= mX - mLanderWidth / 2 && mX
+ mLanderWidth / 2 <= mGoalX + mGoalWidth);
// "Hyperspace" win -- upside down, going fast,
// puts you back at the top.
if (onGoal && (mHeading - 180) < mGoalAngle
&& speed > PHYS_SPEED_HYPERSPACE) {
result = STATE_WIN;
mWinsInARow++;
doStart(); return;
// Oddball case: this case does a return, all other cases
// fall through to setMode() below.
} else if (!onGoal) {
message = t(e_off_pad);
} else if (!(mHeading <= mGoalAngle || mHeading >= 360 - mGoalAngle)) {
message = t(e_bad_angle);
} else if (speed > mGoalSpeed) {
message = t(e_too_fast);
} else {
result = STATE_WIN;
mWinsInARow++;
}
setState(result, message);
}
}
}
/** Handle to the application context, used fetch Drawables. */
private Context mContext;
/** Pointer to the text view to display "Paused.." etc. */
private TextView mStatusText;
/** The thread that actually draws the animation */
private LunarThread thread;
public LunarView(Context context, AttributeSet attrs) {
super(context, attrs);
// register our interest in hearing about changes to our surface
SurfaceHolder holder = getHolder();
lback(this);
// create thread only; it's started in surfaceCreated()
thread = new LunarThread(holder, context, new Handler() {
@Override
public void handleMessage(Message m) {
ibility(a().getInt("viz"));
t(a().getString("text"));
}
}); setFocusable(true); // make sure we get key events
}
/**
* Fetches the animation thread corresponding to this LunarView.
*
* @return the animation thread
*/
public LunarThread getThread() {
return thread;
}
/**
* Standard override to get key-press events.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
return own(keyCode, msg);
}
/**
* Standard override for key-up. We actually care about these, so we can
* turn off the engine or stop rotating.
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {
return p(keyCode, msg);
}
/**
* Standard window-focus override. Notice focus lost so we can pause on
* focus lost. e.g. user switches to take a call.
*/
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if (!hasWindowFocus) ();
}
/**
* Installs a pointer to the text view used for messages.
*/
public void setTextView(TextView textView) {
mStatusText = textView;
} /* Callback invoked when the surface dimensions change. */
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
faceSize(width, height);
}
/*
* Callback invoked when the Surface has been created and is ready to be
* used.
*/
public void surfaceCreated(SurfaceHolder holder) {
// start the thread here so that we don't busy-wait in run()
// waiting for the surface to be created
ning(true);
();
}
/*
* Callback invoked when the Surface has been destroyed and must no longer
* be touched. WARNING: after this method returns, the Surface/Canvas must
* never be touched again!
*/
public void surfaceDestroyed(SurfaceHolder holder) {
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
ning(false);
while (retry) {
try {
();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
Android游戏开发之旅九 VideoView类剖析
有关SurfaceView相关的内容今天Android123继续延用系统的示例类VideoView来让大家深入了解Android平台的图形绘制基础类的实现原理。大家可能会发现VideoView类的控制方面无法改变,我们可以通过重构VideoView类来实现更个性化的播放器。
public class VideoView extends SurfaceView implements MediaPlayerControl {
private String TAG = "VideoView"; // settable by the client
private Uri mUri;
private int mDuration;
// all possible internal states
private static final int STATE_ERROR = -1;
private static final int STATE_IDLE = 0;
private static final int STATE_PREPARING = 1;
private static final int STATE_PREPARED = 2;
private static final int STATE_PLAYING = 3;
private static final int STATE_PAUSED = 4;
private static final int STATE_PLAYBACK_COMPLETED = 5;
// mCurrentState is a VideoView object's current state.
// mTargetState is the state that a method caller intends to reach.
// For instance, regardless the VideoView object's current state,
// calling pause() intends to bring the object to a target state
// of STATE_PAUSED.
private int mCurrentState = STATE_IDLE;
private int mTargetState = STATE_IDLE;
// All the stuff we need for playing and showing a video
private SurfaceHolder mSurfaceHolder = null;
private MediaPlayer mMediaPlayer = null;
private int mVideoWidth;
private int mVideoHeight;
private int mSurfaceWidth;
private int mSurfaceHeight;
private MediaController mMediaController;
private OnCompletionListener mOnCompletionListener;
private aredListener mOnPreparedListener;
private int mCurrentBufferPercentage;
private OnErrorListener mOnErrorListener;
private int mSeekWhenPrepared; // recording the seek position while preparing
private boolean mCanPause;
private boolean mCanSeekBack;
private boolean mCanSeekForward;
public VideoView(Context context) {
super(context);
initVideoView();
}
public VideoView(Context context, AttributeSet attrs) {
this(context, attrs, 0); initVideoView();
}
public VideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initVideoView();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Log.i("@@@@", "onMeasure");
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
if ( mVideoWidth * height > width * mVideoHeight ) {
//Log.i("@@@", "image too tall, correcting");
height = width * mVideoHeight / mVideoWidth;
} else if ( mVideoWidth * height < width * mVideoHeight ) {
//Log.i("@@@", "image too wide, correcting");
width = height * mVideoWidth / mVideoHeight;
} else {
//Log.i("@@@", "aspect ratio is correct: " +
//width+"/"+height+"="+
//mVideoWidth+"/"+mVideoHeight);
}
}
//Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}
public int resolveAdjustedSize(int desiredSize, int measureSpec) {
int result = desiredSize;
int specMode = e(measureSpec);
int specSize = e(measureSpec);
switch (specMode) {
case IFIED:
/* Parent says we can be as big as we want. Just don't be larger
* than max size imposed on ourselves.
*/
result = desiredSize;
break;
case _MOST:
/* Parent says we can be as big as we want, up to specSize. * Don't be larger than specSize, and don't be larger than
* the max size imposed on ourselves.
*/
result = (desiredSize, specSize);
break;
case Y:
// No choice. Do what we are told.
result = specSize;
break;
}
return result;
}
private void initVideoView() {
mVideoWidth = 0;
mVideoHeight = 0;
getHolder().addCallback(mSHCallback);
getHolder().setType(E_TYPE_PUSH_BUFFERS);
setFocusable(true);
setFocusableInTouchMode(true);
requestFocus();
mCurrentState = STATE_IDLE;
mTargetState = STATE_IDLE;
}
public void setVideoPath(String path) {
setVideoURI((path));
}
public void setVideoURI(Uri uri) {
mUri = uri;
mSeekWhenPrepared = 0;
openVideo();
requestLayout();
invalidate();
}
public void stopPlayback() {
if (mMediaPlayer != null) {
();
e();
mMediaPlayer = null;
mCurrentState = STATE_IDLE;
mTargetState = STATE_IDLE; }
}
private void openVideo() {
if (mUri == null || mSurfaceHolder == null) {
// not ready for playback just yet, will try again later
return;
}
// Tell the music playback service to pause
// TODO: these constants need to be published somewhere in the framework.
Intent i = new Intent("ervicecommand");
ra("command", "pause");
oadcast(i);
// we shouldn't clear the target state, because somebody might have
// called start() previously
release(false);
try {
mMediaPlayer = new MediaPlayer();
reparedListener(mPreparedListener);
ideoSizeChangedListener(mSizeChangedListener);
mDuration = -1;
ompletionListener(mCompletionListener);
rrorListener(mErrorListener);
ufferingUpdateListener(mBufferingUpdateListener);
mCurrentBufferPercentage = 0;
aSource(mContext, mUri);
play(mSurfaceHolder);
ioStreamType(_MUSIC);
eenOnWhilePlaying(true);
eAsync();
// we don't set the target state here either, but preserve the
// target state that was there before.
mCurrentState = STATE_PREPARING;
attachMediaController();
} catch (IOException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;
r(mMediaPlayer, _ERROR_UNKNOWN, 0);
return;
} catch (IllegalArgumentException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR; r(mMediaPlayer, _ERROR_UNKNOWN, 0);
return;
}
}
public void setMediaController(MediaController controller) {
if (mMediaController != null) {
();
}
mMediaController = controller;
attachMediaController();
}
private void attachMediaController() {
if (mMediaPlayer != null && mMediaController != null) {
iaPlayer(this);
View anchorView = ent() instanceof View ?
(View)ent() : this;
horView(anchorView);
bled(isInPlaybackState());
}
}
oSizeChangedListener mSizeChangedListener =
new oSizeChangedListener() {
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
mVideoWidth = eoWidth();
mVideoHeight = eoHeight();
if (mVideoWidth != 0 && mVideoHeight != 0) {
getHolder().setFixedSize(mVideoWidth, mVideoHeight);
}
}
};
aredListener mPreparedListener = new
aredListener() {
public void onPrepared(MediaPlayer mp) {
mCurrentState = STATE_PREPARED;
// Get the capabilities of the player for this stream
Metadata data = adata(TA_ALL,
_METADATA_FILTER);
if (data != null) {
mCanPause = !(_AVAILABLE) || lean(_AVAILABLE);
mCanSeekBack = !(_BACKWARD_AVAILABLE)
|| lean(_BACKWARD_AVAILABLE);
mCanSeekForward = !(_FORWARD_AVAILABLE)
|| lean(_FORWARD_AVAILABLE);
} else {
mCanPause = mCanSeekForward = mCanSeekForward = true;
}
if (mOnPreparedListener != null) {
ared(mMediaPlayer);
}
if (mMediaController != null) {
bled(true);
}
mVideoWidth = eoWidth();
mVideoHeight = eoHeight();
int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be
changed after seekTo() call
if (seekToPosition != 0) {
seekTo(seekToPosition);
}
if (mVideoWidth != 0 && mVideoHeight != 0) {
//Log.i("@@@@", "video size: " + mVideoWidth +"/"+ mVideoHeight);
getHolder().setFixedSize(mVideoWidth, mVideoHeight);
if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) {
// We didn't actually change the size (it was already at the size
// we need), so we won't get a "surface changed" callback, so
// start the video here instead of in the callback.
if (mTargetState == STATE_PLAYING) {
start();
if (mMediaController != null) {
();
}
} else if (!isPlaying() &&
(seekToPosition != 0 || getCurrentPosition() > 0)) {
if (mMediaController != null) {
// Show the media controls when we're paused into a video and make 'em
stick.
(0);
}
}
}
} else { // We don't know the video size yet, but should start anyway.
// The video size might be reported to us later.
if (mTargetState == STATE_PLAYING) {
start();
}
}
}
};
private letionListener mCompletionListener =
new letionListener() {
public void onCompletion(MediaPlayer mp) {
mCurrentState = STATE_PLAYBACK_COMPLETED;
mTargetState = STATE_PLAYBACK_COMPLETED;
if (mMediaController != null) {
();
}
if (mOnCompletionListener != null) {
letion(mMediaPlayer);
}
}
};
private rListener mErrorListener =
new rListener() {
public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
Log.d(TAG, "Error: " + framework_err + "," + impl_err);
mCurrentState = STATE_ERROR;
mTargetState = STATE_ERROR;
if (mMediaController != null) {
();
}
/* If an error handler has been supplied, use it and finish. */
if (mOnErrorListener != null) {
if (r(mMediaPlayer, framework_err, impl_err)) {
return true;
}
}
/* Otherwise, pop up an error dialog so the user knows that
* something bad has happened. Only try and pop up the dialog
* if we're attached to a window. When we're going away and no
* longer have a window, don't bother showing the user an error.
*/ if (getWindowToken() != null) {
Resources r = ources();
int messageId;
if (framework_err ==
_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
messageId =
iew_error_text_invalid_progressive_playback;
} else {
messageId = iew_error_text_unknown;
}
new r(mContext)
.setTitle(iew_error_title)
.setMessage(messageId)
.setPositiveButton(iew_error_button,
new kListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* If we get here, there is no onError listener, so
* at least inform them that the video is over.
*/
if (mOnCompletionListener != null) {
letion(mMediaPlayer);
}
}
})
.setCancelable(false)
.show();
}
return true;
}
};
private eringUpdateListener mBufferingUpdateListener =
new eringUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
mCurrentBufferPercentage = percent;
}
};
/**
* Register a callback to be invoked when the media file
* is loaded and ready to go.
*
* @param l The callback that will be run */
public void setOnPreparedListener(aredListener l)
{
mOnPreparedListener = l;
}
/**
* Register a callback to be invoked when the end of a media file
* has been reached during playback.
*
* @param l The callback that will be run
*/
public void setOnCompletionListener(OnCompletionListener l)
{
mOnCompletionListener = l;
}
/**
* Register a callback to be invoked when an error occurs
* during playback or setup. If no listener is specified,
* or if the listener returned false, VideoView will inform
* the user of any errors.
*
* @param l The callback that will be run
*/
public void setOnErrorListener(OnErrorListener l)
{
mOnErrorListener = l;
}
ck mSHCallback = new ck()
{
public void surfaceChanged(SurfaceHolder holder, int format,
int w, int h)
{
mSurfaceWidth = w;
mSurfaceHeight = h;
boolean isValidState = (mTargetState == STATE_PLAYING);
boolean hasValidSize = (mVideoWidth == w && mVideoHeight == h);
if (mMediaPlayer != null && isValidState && hasValidSize) {
if (mSeekWhenPrepared != 0) {
seekTo(mSeekWhenPrepared);
}
start();
if (mMediaController != null) { ();
}
}
}
public void surfaceCreated(SurfaceHolder holder)
{
mSurfaceHolder = holder;
openVideo();
}
public void surfaceDestroyed(SurfaceHolder holder)
{
// after we return from this we can't use the surface any more
mSurfaceHolder = null;
if (mMediaController != null) ();
release(true);
}
};
/*
* release the media player in any state
*/
private void release(boolean cleartargetstate) {
if (mMediaPlayer != null) {
();
e();
mMediaPlayer = null;
mCurrentState = STATE_IDLE;
if (cleartargetstate) {
mTargetState = STATE_IDLE;
}
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isInPlaybackState() && mMediaController != null) {
toggleMediaControlsVisiblity();
}
return false;
}
@Override
public boolean onTrackballEvent(MotionEvent ev) { if (isInPlaybackState() && mMediaController != null) {
toggleMediaControlsVisiblity();
}
return false;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
boolean isKeyCodeSupported = keyCode != E_BACK &&
keyCode != E_VOLUME_UP &&
keyCode != E_VOLUME_DOWN &&
keyCode != E_MENU &&
keyCode != E_CALL &&
keyCode != E_ENDCALL;
if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
if (keyCode == E_HEADSETHOOK ||
keyCode == E_MEDIA_PLAY_PAUSE) {
if (ing()) {
pause();
();
} else {
start();
();
}
return true;
} else if (keyCode == E_MEDIA_STOP
&& ing()) {
pause();
();
} else {
toggleMediaControlsVisiblity();
}
}
return own(keyCode, event);
}
private void toggleMediaControlsVisiblity() {
if (ing()) {
();
} else {
();
}
}
public void start() {
if (isInPlaybackState()) {
();
mCurrentState = STATE_PLAYING;
}
mTargetState = STATE_PLAYING;
}
public void pause() {
if (isInPlaybackState()) {
if (ing()) {
();
mCurrentState = STATE_PAUSED;
}
}
mTargetState = STATE_PAUSED;
}
// cache duration as mDuration for faster access
public int getDuration() {
if (isInPlaybackState()) {
if (mDuration > 0) {
return mDuration;
}
mDuration = ation();
return mDuration;
}
mDuration = -1;
return mDuration;
}
public int getCurrentPosition() {
if (isInPlaybackState()) {
return rentPosition();
}
return 0;
}
public void seekTo(int msec) {
if (isInPlaybackState()) {
(msec);
mSeekWhenPrepared = 0;
} else { mSeekWhenPrepared = msec;
}
}
public boolean isPlaying() {
return isInPlaybackState() && ing();
}
public int getBufferPercentage() {
if (mMediaPlayer != null) {
return mCurrentBufferPercentage;
}
return 0;
}
private boolean isInPlaybackState() {
return (mMediaPlayer != null &&
mCurrentState != STATE_ERROR &&
mCurrentState != STATE_IDLE &&
mCurrentState != STATE_PREPARING);
}
public boolean canPause() {
return mCanPause;
}
public boolean canSeekBackward() {
return mCanSeekBack;
}
public boolean canSeekForward() {
return mCanSeekForward;
}
}
Android游戏开发之旅十 位图旋转
今天有关Android游戏开发的基础,我们说下Bitmap相关的实用操作,这里我们就说下位图旋转。在Android中图形的旋转和变化提供了方便的矩阵Maxtrix类,Maxtrix类的setRotate方法接受图形的变换角度和缩放,最终Bitmap类的createBitmap方法中其中的重载函数,可以接受Maxtrix对象,方法原型如下
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m,
boolean filter)
参数的具体意思 source 源bitmap对象
x 源坐标x位置
y 源坐标y位置
width 宽度
height 高度
m 接受的maxtrix对象,如果没有可以设置为null
filter 该参数仅对maxtrix包含了超过一个翻转才有效。
下面Android123给大家一个比较经典的例子,rotate方法是静态方法可以直接调用,参数为源Bitmap对象,参数二为旋转的角度,从0~360,返回值为新的Bitmap对象。其中具体的宽高可以调整。
public static Bitmap rotate(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
ate(degrees,
(float) th() / 2, (float) ght() / 2);
try {
Bitmap b2 = Bitmap(
b, 0, 0, th(), ght(), m, true);
if (b != b2) {
e(); //Android开发网再次提示Bitmap操作完应该显示的释放
b = b2;
}
} catch (OutOfMemoryError ex) {
// Android123建议大家如何出现了内存不足异常,最好return 原始的bitmap对象。.
}
}
return b;
}
有关Maxtrix类的更多实用例子,我们将在以后多次提到。
Android游戏开发之旅11 View中手势识别
有关Android平台的游戏开发中我们需要涉及到控制,在开始的Android游戏开发之旅中我们提到了按键和轨迹球的控制方式,从今天开始Android123开始给出大家游戏中其他的一些控制方式,比如今天的手势操作和未来重力感应。
很多网友发现Android中手势识别提供了两个类,由于Android 1.6以下的版本比如cupcake中无法使用eDetector,而e是Android 1.6才开始支持的,我们考虑到仍然有很多Android 1.5固件的网友,就来看下兼容性更强的eDetector。在eDetector类中有很多种重载版本,下面我们仅提到能够自定义在View中的两种方法,分别为GestureDetector(Context context,
ureListener listener) 和 GestureDetector(Context context, ureListener listener, Handler handler) 和。我们可以看到第一个参数为Context,所以我们想附着到某View时,最简单的方法就是直接从超类派生传递Context,实现GestureDetector里中提供一些接口。
下面我们就以实现手势识别的onFling动作,在CwjView中我们从View类继承,当然大家可以从TextView等更高层的界面中实现触控。
class CwjView extends View {
private GestureDetector mGD;
public CwjView(Context context, AttributeSet attrs) {
super(context, attrs);
mGD = new GestureDetector(context, new OnGestureListener()
{
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float
velocityY) {
int dx = (int) (() - ()); //计算滑动的距离
if ((dx) > MAJOR_MOVE && (velocityX) > (velocityY))
{ //降噪处理,必须有较大的动作才识别
if (velocityX > 0) {
//向右边
} else {
//向左边
}
return true;
} else {
return false; //当然可以处理velocityY处理向上和向下的动作
}
}
});
}
在上面Android123提示大家仅仅探测了Fling动作仅仅实现了onFling方法,这里相关的还有以下几种方法来实现具体的可以参考我们以前的文章有详细的解释:
boolean onDoubleTap(MotionEvent e)
boolean onDoubleTapEvent(MotionEvent e)
boolean onDown(MotionEvent e)
void onLongPress(MotionEvent e)
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
void onShowPress(MotionEvent e) boolean onSingleTapConfirmed(MotionEvent e)
boolean onSingleTapUp(MotionEvent e)
接下来是重点,让我们的View接受触控,需要使用下面两个方法让GestureDetector类去处理onTouchEvent和onInterceptTouchEvent方法。
@Override
public boolean onTouchEvent(MotionEvent event) {
hEvent(event);
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return hEvent(event);
}
}
有关重力感应的方向识别, 我们将告诉大家如何通过重力感应来控制我们的Android游戏,目前对于大多数Android设备来说仅提供了重力感应器和加速感应器,只有较新或高端的Android设备还提供了陀螺仪,可以帮助我们测试角速度,来处理一些复杂的应用。详细的可以参考我们的 Android游戏开发之旅12 重力感应篇
Android游戏开发之旅12 Sensor重力感应
从Android手机开始,主流的智能机纷纷加入了感应器Sensor硬件,常见的有光线感应器、重力感应器、加速感应器,而更高级的有磁极方向、陀螺仪、距离感应器、温度感应器等等。对于Android游戏开发,我们主要用到重力、加速、磁力和陀螺仪四种,当然部分游戏可能需要GPS或Cellid定位来修正一些位移信息。从系统中提高的感应器主要在re中,我们可以看到系统提供了EventListener、Sensor和SensorManager这三个类,我们会发现除了可以获取感应器的信息,和感应器的原始数据外,并没有提供相关的逻辑处理。Android123将会分3篇来详细的介绍不同感应器的作用和逻辑处理,比如自由落体,晃动,磁极,当前的旋转速度。
未来Android123将完成主要是一个基于OpenGL 3D的雷电游戏,最终加入联网对战效果可以团队打怪实现手机3D网游充分发挥Android手机的娱乐能力。对于大多数新款Android手机可能没有配备轨迹球或导航键的方向控制,所以重力感应器是这类实时性较强游戏的首选控制方式。主要有以下几点问题对于Sensor
1. 降噪处理,如果做过LBS软件的大家可能明白偏移修正,在GPS无法正常获取数据较间断时地图不能乱飘,这里Sensor也不例外,除了使用采样数据平均值获取外,可以间隔采样的方法来处理。细节的算法我们将在下节给出示例代码。
发布者:admin,转转请注明出处:http://www.yc00.com/web/1688674434a161647.html
评论列表(0条)