gpt4 book ai didi

java - android - 从 Activity View 中调用方法

转载 作者:行者123 更新时间:2023-12-01 09:59:54 26 4
gpt4 key购买 nike

我正在开发一个 2d 游戏,我试图让我绘制和更新所有内容的 View 告诉包含它的 Activity 游戏结束并在屏幕上显示弹出窗口。问题是我不知道如何从 View 访问 Activity 中的方法,因为它们在不同的线程上工作。

代码看起来或多或少像这样,我想从 View 类访问方法displayFrame()。真实代码中的方法对其他 TextView 等执行更多操作,只有该 Activity 才能访问。

public class MainActivity extends Activity{

private MyView myView;
RelativeLayout finalFrame;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

myView = new MyView(this.getApplicationContext());
this.setContentView(myView);

finalFrame = (RelativeLayout) findViewById(R.id.framefinal);
}

private void displayFrame(){
this.finalFrame.setVisibility(View.VISIBLE);
}
}

public class MyView extends SurfaceView implements SurfaceHolder.Callback{
private MyThread myThread;
private boolean finished = false;

public MyView(Context context) {
super(context);

getHolder().addCallback(this);
this.myThread = new MyThread(getHolder(), this);

this.myThread.setRunning(true);
}

public void update() {
this.finished = someMethod();
if(this.finished){
MainActivity.displayFrame();
}
}

public void draw(Canvas canvas){
//draw something
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
}

public class MyThread extends Thread{
private MyView myView;
private SurfaceHolder surfaceHolder;
private boolean running = false;
private static Canvas canvas;

public MyThread(SurfaceHolder surfaceHolder, MyView myView){
super();
this.surfaceHolder = surfaceHolder;
this.myView = myView;
}

@Override
public void run(){
while(running){
canvas = null;

try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.myView.update();
this.myView.draw(canvas);
}
} catch (Exception e) {e.printStackTrace();}
finally{
if(canvas!=null)
{
try {
surfaceHolder.unlockCanvasAndPost(canvas);
}
catch(Exception e){e.printStackTrace();}
}
}
}
}

public void setRunning(boolean running){
this.running = running;
}
}

最佳答案

最简单的方法是在 View 中定义监听器接口(interface)并让 Activity 实现它。然后您可以通知 Activity 您的游戏已结束并执行适当的逻辑。

public class MyView extends SurfaceView implements SurfaceHolder.Callback {

// your other fields
private GameListener mGameListener;

public void setGameListener(GameListener gameListener) {
mGameListener = gameListener;
}

public void update() {
// your logic
if (mGameListener != null) {
// calling onGameEnd on the main thread
post(new Runnable() {

public void run() {
mGameListener.onGameEnd();
}

});
}
}

// rest of your class as it is

public interface GameListener {

void onGameEnded();

}
}

public class MainActivity extends Activity implements GameListener {

// the rest of your class

public void onGameEnded() {
// call the method you need
}
}

关于java - android - 从 Activity View 中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36894006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com