我是Android游戏开发的新手。这就是我想要做的事情
我坚持第二步,因为我根本无法让它发挥作用。一旦调用触摸事件方法,就会出现空白屏幕。在空白屏幕上仅绘制挖孔图像。我已通过文件阅读但无法弄清楚这里出了什么问题。
这是我的代码
class DrawSurface extends android.view.SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {
Bitmap mBMPField;
Bitmap mBMPHole;
SurfaceHolder surfaceHolder;
float x = 0;
float y = 0;
boolean touched = false;
public DrawSurface(Context context) {
super(context);
getHolder().addCallback(this);
setOnTouchListener(this);
}
public DrawSurface(Context context, AttributeSet attrSet) {
super(context, attrSet);
getHolder().addCallback(this);
setOnTouchListener(this);
}
public DrawSurface(Context context, AttributeSet attrSet, int styleAttr) {
super(context, attrSet, styleAttr);
getHolder().addCallback(this);
setOnTouchListener(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.i("a", "Inside surfaceCreated");
setWillNotDraw(false);
mBMPField = BitmapFactory.decodeResource(getResources(),R.drawable.field);
mBMPHole = BitmapFactory.decodeResource(getResources(),R.drawable.hole);
surfaceHolder = holder;
Canvas canvas = null;
try {
canvas = surfaceHolder.lockCanvas();
Log.d(this.getClass().getName(), String.valueOf(canvas.hashCode()));
canvas.drawBitmap(mBMPField, 0, 0, null);
Paint paintText = new Paint();
paintText.setColor(Color.WHITE);
paintText.setStyle(Paint.Style.FILL);
paintText.setAntiAlias(true);
paintText.setTextSize(20);
ArrayList<Item> mItems = loadItems();
for (Item item : mItems) {
item.setX((int) (Math.random() * this.getWidth()));
item.setY((int) (Math.random() * this.getHeight()));
canvas.drawText(item.getText(), item.getX(), item.getY(), paintText);
}
}
finally
{
if(canvas!=null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("a", "Inside onTouch");
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if (surfaceHolder.getSurface().isValid()) {
touched = true;
x = event.getX();
y = event.getY();
Canvas canvas = null;
try {
canvas = surfaceHolder.lockCanvas();
Log.d(this.getClass().getName(), String.valueOf(canvas.hashCode()));
canvas.drawBitmap(mBMPHole, x - mBMPHole.getWidth() / 2, y - mBMPHole.getHeight() / 2, null);
}
finally {
if(canvas!=null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
return false;
}
private class Item {
private int x;
private int y;
private String text;
private boolean found;
public boolean isFound() {
return found;
}
public void setFound(boolean found) {
this.found = found;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
private ArrayList<Item> loadItems()
{
InputStream input = getResources().openRawResource(R.raw.items);
BufferedReader reader = null;
ArrayList<Item> items = new ArrayList<Item>();
String line;
try
{
reader = new BufferedReader(new InputStreamReader(input));
while((line = reader.readLine()) != null)
{
Item item = new Item();
item.setText(line);
items.add(item);
}
}
catch (Exception e)
{
Log.e("MainActivity", "Reading list of Items failed!", e);
}
finally {
try
{
if (reader != null) reader.close();
}
catch (Exception e)
{
Log.e("MainActivity", "Error closing file reader.", e);
}
}
return items;
}
}
任何建议都会对此有所帮助