简体   繁体   中英

SurfaceView turns black when drawing on it

I have a surfaceview that catches touchEvents, and then draws a point on the canvas. But when I touch, the SurfaceView changes from red to black, instead of drawing a point. Please take a look at my code:

GamemodeClassic.java

public class GamemodeClassic extends AppCompatActivity implements Runnable, View.OnTouchListener {

private SurfaceView view;

private GameClassic game;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_gamemode_classic);

    init();
}

public void init() {
    view = (SurfaceView) findViewById(R.id.classic_view);
    game = new GameClassic(this);
    view.getHolder().addCallback(game);
    view.setOnTouchListener(game);
}

@Override
public void run() {

}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return true;
}
}

GameClassic.java

public class GameClassic extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {

SurfaceHolder mainHolder;

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

    setFocusable(true);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    mainHolder = holder;
}

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

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

public void initDraw(SurfaceHolder holder) {
    Canvas canvas = mainHolder.lockCanvas();
    if(canvas == null) {
        Log.v("testlog", "GameClassic.java - Canvas not available");
    }else{
        Log.v("testlog", "GameClassic.java - Canvas found, drawing");
        canvas.drawRGB(255, 0, 0);
    }

    holder.unlockCanvasAndPost(canvas);
}

public void drawPoint(float x, float y) {
    Canvas canvas = mainHolder.lockCanvas();

    if(canvas != null) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLUE);

        canvas.drawPoint(x, y, paint);

        mainHolder.unlockCanvasAndPost(canvas);
    }else{
        Log.v("testlog", canvas + "");
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.v("testlog", event.getAction() + "sfsfsf");

    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        drawPoint(event.getX(), event.getY());
        Log.v("testlog", "drawing point on (" + event.getX() + ";" + event.getY() + ")");
    }
    return true;
}
}

Thanks In Advance

EDIT As it turns out, I had to change the line holder.unlockCanvasAndPost(canvas) into mainHolder.unlockCanvasAndPost(canvas) . There was still no improvement, but when I try to change the color of the surfaceview with canvas.drawRGB() in drawPoint, it works. Drawing the point however still turns the surfaceview into black.

When you override onTouch() you need to have a call to super.onTouch() . Might be the problem.

Apparently, when you lock the canvas and draw on it, the whole canvas gets resetted to its default color, which is black. When you then draw a point on top, the blue was barely visible, which lead me to thinking that the whole screen was getting turned black. Redrawing the background before the point was the solution.

At your GameClassic constructor set alpha to zero. Like this:

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

    setFocusable(true);

    setAlpha(0); // this is the secret
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM