0

有什么方法可以让对象在surfaceView.

例如:

canvas.drawBitmap(...);
canvas.drawText(...);

如果我这样做,文本就会出现在位图上。如何在不重绘的情况下反转它

对不起我的英语不好。

4

3 回答 3

0

没有办法将元素前后移动。您试图在此处更改元素的 Z 顺序,这是不可能的,或者在 Android 中没有直接方法。您所能做的就是将它们排列在布局中并启用和禁用视图,以便它会给您相同的效果。
您可以使用addView()setVisibility(View.Visible)将元素前后带入。

于 2013-07-04T11:41:30.837 回答
0

请尝试使用以下代码。

//SurfaceView class
public class MainGamePanel extends SurfaceView implements
        SurfaceHolder.Callback {
    MainThread thread;

    public MainGamePanel(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        getHolder().addCallback(this);
        setFocusable(true);
        thread = new MainThread(getHolder(), this);

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
        Log.e("N", "Changed");

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.e("N", "Created");
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Log.e("N", "Destroyed");
        boolean retry = true;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // try again shutting down the thread
            }
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (event.getY() > getHeight() - 50) {
                thread.setRunning(false);
                ((Activity) getContext()).finish();
            } else {
                Log.d("N", "Coords: x=" + event.getX() + ",y=" + event.getY());
            }
        }
        return super.onTouchEvent(event);
    }
}

//MainThread class

public class MainThread extends Thread {

    // flag to hold game state
    private boolean running;
    private SurfaceHolder surfaceHolder;
    private MainGamePanel gamePanel;

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

    public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;
    }

    @Override
    public void run() {
        long tickCount = 0L;
        Log.d("N", "Starting game loop");
        while (running) {
            tickCount++;
            // update game state
            // render state to the screen
        }
        Log.d("N", "Game loop executed " + tickCount + " times");
    }


}
//Main Activity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MainGamePanel(this));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

于 2013-07-04T11:43:03.410 回答
0

Canvas 只是封装了位图对象,位图只保存某个位置的像素颜色,它不保存单独的图层。这就像在 MSPaint 中绘图。因此,您无法更改画布上图元的 Z 顺序。但是,你为什么不改变命令的顺序呢?

于 2013-07-04T11:54:42.470 回答