为什么我的 surfaceCreated 从未被调用
Why is my surfaceCreated never called
我是 android 开发人员的菜鸟,我遇到了问题。我正在尝试使用 SurfaceView 制作一个简单的游戏。但是当我启动应用程序时,永远不会调用 surfaceCreated。
我正在使用 his 答案来减少背景内存
这里是setcontentView的放置位置:
call_surfaceView = new class_surfaceView(cMainActivity);
// Setup your SurfaceView
SurfaceView surfaceView = call_surfaceView; // use any SurfaceView you want
surfaceView.setZOrderOnTop(true);
surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
// Setup your ImageView
ImageView bgImagePanel = new ImageView(cMainActivity);
bgImagePanel.setBackgroundResource(R.drawable.background); // use any Bitmap or BitmapDrawable you want
// Use a RelativeLayout to overlap both SurfaceView and ImageView
RelativeLayout.LayoutParams fillParentLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout rootPanel = new RelativeLayout(cMainActivity);
rootPanel.setLayoutParams(fillParentLayout);
rootPanel.addView(bgImagePanel, fillParentLayout);
rootPanel.addView(surfaceView, fillParentLayout);
setContentView(rootPanel);
这里是 我的 surfaceView 代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class class_surfaceView extends SurfaceView implements SurfaceHolder.Callback {
Thread t;
Canvas c;
SurfaceHolder holder;
Bitmap ghost;
Bitmap lifebar;
Bitmap player;
Bitmap sol;
Bitmap tombe;
public class_surfaceView(Context context) {
super(context);
holder = getHolder();
Log.d("Essai", "Class_surfaceView");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("Essai", "surfaceCreated");
new Thread(new Runnable() {
@Override
public void run() {
lauch_game();
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
public void lauch_game() {
ghost = BitmapFactory.decodeResource(getResources(),R.drawable.ghost);
c = holder.lockCanvas();
if(c != null){
c.drawColor(0, PorterDuff.Mode.CLEAR);
c.drawBitmap(ghost,0,0,null);
holder.unlockCanvasAndPost(c);
} else {
Log.d("DEBUG","c is null");
}
Log.d("Essai", "Essais de dessin");
}
}
感谢您的帮助!
尝试使用@Override上面的方法
在 holder = getHolder() 之后的 class_surfaceView 构造函数中,您必须调用 holder.addCallback(this) 来注册回调。
我是 android 开发人员的菜鸟,我遇到了问题。我正在尝试使用 SurfaceView 制作一个简单的游戏。但是当我启动应用程序时,永远不会调用 surfaceCreated。
我正在使用 his 答案来减少背景内存 这里是setcontentView的放置位置:
call_surfaceView = new class_surfaceView(cMainActivity);
// Setup your SurfaceView
SurfaceView surfaceView = call_surfaceView; // use any SurfaceView you want
surfaceView.setZOrderOnTop(true);
surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
// Setup your ImageView
ImageView bgImagePanel = new ImageView(cMainActivity);
bgImagePanel.setBackgroundResource(R.drawable.background); // use any Bitmap or BitmapDrawable you want
// Use a RelativeLayout to overlap both SurfaceView and ImageView
RelativeLayout.LayoutParams fillParentLayout = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout rootPanel = new RelativeLayout(cMainActivity);
rootPanel.setLayoutParams(fillParentLayout);
rootPanel.addView(bgImagePanel, fillParentLayout);
rootPanel.addView(surfaceView, fillParentLayout);
setContentView(rootPanel);
这里是 我的 surfaceView 代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class class_surfaceView extends SurfaceView implements SurfaceHolder.Callback {
Thread t;
Canvas c;
SurfaceHolder holder;
Bitmap ghost;
Bitmap lifebar;
Bitmap player;
Bitmap sol;
Bitmap tombe;
public class_surfaceView(Context context) {
super(context);
holder = getHolder();
Log.d("Essai", "Class_surfaceView");
}
public void surfaceCreated(SurfaceHolder holder) {
Log.d("Essai", "surfaceCreated");
new Thread(new Runnable() {
@Override
public void run() {
lauch_game();
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
public void lauch_game() {
ghost = BitmapFactory.decodeResource(getResources(),R.drawable.ghost);
c = holder.lockCanvas();
if(c != null){
c.drawColor(0, PorterDuff.Mode.CLEAR);
c.drawBitmap(ghost,0,0,null);
holder.unlockCanvasAndPost(c);
} else {
Log.d("DEBUG","c is null");
}
Log.d("Essai", "Essais de dessin");
}
}
感谢您的帮助!
尝试使用@Override上面的方法
在 holder = getHolder() 之后的 class_surfaceView 构造函数中,您必须调用 holder.addCallback(this) 来注册回调。