gpt4 book ai didi

Android - Surface 无效

转载 作者:行者123 更新时间:2023-11-29 17:34:52 24 4
gpt4 key购买 nike

我试图每 5 秒画一个红色圆圈。我有一个 draw 和一个 run 方法,run 方法调用绘制圆圈的 draw 方法。但是,当从 run 方法调用 draw 方法时,表面无效,所以我无法绘制任何东西并且屏幕是黑色的。我怎样才能使表面有效,或者如果我不能,我还能怎么做?这是代码和 logcat。

public class RandomCircles extends Activity {

MySurfaceView mySurfaceView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mySurfaceView.onResumeMySurfaceView();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mySurfaceView.onPauseMySurfaceView();
}

class MySurfaceView extends SurfaceView implements Runnable {

Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;
Handler handler = new Handler();

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Random random;

public MySurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
surfaceHolder = getHolder();
random = new Random();
}

public void onResumeMySurfaceView() {
running = true;
thread = new Thread(this);
thread.start();
}

public void onPauseMySurfaceView() {
boolean retry = true;
running = false;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

@Override
public void run() {
System.out.println("The run method is running");
// TODO Auto-generated method stub
Draw();
}

public void Draw() {
System.out.println("The draw method is running");
if (surfaceHolder.getSurface().isValid()) {
System.out.println("The surface is valid");
Canvas canvas = surfaceHolder.lockCanvas();
//... actual drawing on canvas

int x = random.nextInt(getWidth());

if (getWidth() - x < 100)
x -= 100;
else if (getWidth() - x > getWidth() - 100)
x += 100;

int y = random.nextInt(getHeight());

if (getHeight() - y < 100)
y -= 100;
else if (getHeight() - x > getHeight() - 100)
y += 100;

int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x, y, radius, paint);

surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}

There are indicators when the run and draw methods are called, but the indicator after the check for if the screen is valid does not display.

最佳答案

实现SurfaceHolder.Callback并且仅在收到 surfaceCreated(SurfaceHolder holder) 回调时在 Surface 上呈现。例如:

public MySurfaceView(Context context) {
super(context);
surfaceHolder = getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//stop render thread here
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
//start render thread here
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

});
}

关于Android - Surface 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955900/

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