Android游戏开发学习(5)--实现Button悬浮于与SurfaceView之上
实现Button悬浮于与SurfaceView之上实现先看效果:
http://dl.iteye.com/upload/attachment/0063/2462/8b6ca4c2-ad25-3a5f-8cc8-6b3256bb2995.jpg
http://dl.iteye.com/upload/attachment/0063/2464/f2fb839d-369e-3c0f-b7c4-c0bd537ded7d.jpg
注意:你实现的SurfaceView和android中的Button,EditView是同级的,不能把一个包含在另一个里面
1.创建自己的SurfaceView类,一定要实现2个参数的那个函数,因为你要在XMl中使用,第二个参数指的自定义的组件的一些属性长宽等。
public GameSurfaceView(Context context, AttributeSet attrs){super(context,attrs);}
<dk.game.GameSurfaceView android:id="@+id/gameView" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
2.处理SurfaceView中事件一定要在Activity中。我们在xml中定义我们的surfaceview 和 组件button、textview等等的时候 他们是同一级别的!!而不是把button包含在surfaceview里,所以虽然在surfaceview中可以根据id索引到button但绑定的时候是无法找到button的,只有我们的 activitysetContentView(R.layout.main); 显示的button,所以只能在显示它的activity中去绑定,这里需要注意
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); } /** * 下一张按钮事件 * @param view */ public void nextBt(View view){ GameSurfaceView.cn=2; } /** * 上一张按钮事件 * @param view */ public void preBt(View view){ GameSurfaceView.cn=1; } 3.在Layout中是相对布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <dk.game.GameSurfaceView android:id="@+id/gameView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <Button android:id="@+id/nextBt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:text="下一张" android: /> <Button android:id="@+id/preBt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/nextBt" android:layout_alignParentBottom="true" android:text="上一张" android: /> </RelativeLayout> GameSurfaceView类的源码如下
package dk.game;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;public class GameSurfaceView extends SurfaceView implements Runnable ,SurfaceHolder.Callback{private Thread gameViewThread;private SurfaceHolder surfaceHolder;private Paint paint;private boolean runFlag=false;public static int screen_width,screen_height;protected Resources resources;private Canvas canvas;private Bitmap bmp_bg1,bmp_bg2;public static int cn=1;public GameSurfaceView(Context context, AttributeSet attrs){super(context,attrs);init();}public GameSurfaceView(Context context) {super(context);init();}private void init(){surfaceHolder=getHolder();surfaceHolder.addCallback(this);resources=getResources();paint=new Paint();paint.setAntiAlias(true);bmp_bg1=BitmapFactory.decodeResource(getResources(), R.drawable.bg1);bmp_bg2=BitmapFactory.decodeResource(getResources(), R.drawable.bg2);//setFocusable(true);//setFocusableInTouchMode(true);//setClickable(true);setKeepScreenOn(true);}public void draw(Canvas canvas,Paint paint){canvas.drawColor(Color.BLACK);if(cn==1){canvas.drawBitmap(bmp_bg1, 0,0, paint);}else if(cn==2){canvas.drawBitmap(bmp_bg2, 0,0, paint);}}@Overridepublic void run() {while(runFlag){try{canvas=surfaceHolder.lockCanvas();long startTime=System.currentTimeMillis();canvas.drawColor(Color.BLACK);draw(canvas,paint);long endTime=System.currentTimeMillis();if(50>(endTime-startTime)){Thread.sleep(50-(endTime-startTime));}}catch (Exception e) {Log.e("Error", "刷新屏幕出错!"+e);}finally{if(canvas!=null){surfaceHolder.unlockCanvasAndPost(canvas);}}}}@Overridepublic void surfaceCreated(SurfaceHolder holder) {screen_width=getWidth();screen_height=getHeight();runFlag=true;gameViewThread=new Thread(this);gameViewThread.start();}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {runFlag=false;}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}}
源码下载:
页:
[1]