有没有办法让对象在surfaceView中获得foregorund。
例如:
canvas.drawBitmap(...);
canvas.drawText(...);
如果我这样做,那么文字出现在位图上。如何在不重绘
的情况下撤消它抱歉我的英文。
答案 0 :(得分:0)
没有方法可以使元素正面和背面。您正尝试在此处更改元素的Z顺序,这是不可能的,或者Android中没有直接方法。您所能做的就是在布局中排列它们并启用和禁用视图,以便它们可以产生相同的效果
您可以使用addView()
或setVisibility(View.Visible)
将元素带回前方。
答案 1 :(得分:0)
请尝试使用以下代码。 // SurfaceView类 公共类MainGamePanel扩展了SurfaceView实现 SurfaceHolder.Callback { MainThread线程;
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类
公共类MainThread扩展了线程{
// 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");
}
} //主要活动
公共类MainActivity扩展了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;
}
}
答案 2 :(得分:0)
Canvas只是封装了位图对象,而位图只保存了某个位置的像素颜色,它不会保存单独的图层。这就像画MSPaint一样。因此,您无法在画布上更改基元的Z顺序。但是,为什么不改变命令的顺序呢?