Android 渲染SurfaceView-触摸时画布清除

Android 渲染SurfaceView-触摸时画布清除,android,canvas,surfaceview,Android,Canvas,Surfaceview,我是Android游戏开发的新手。这就是我想做的 使用项目渲染背景(绿色区域)(项目位于背景后面,但我已使其显示在顶部) 在触摸屏上绘制附加图像(挖洞) 执行冲突测试以查看用户是否已找到该项 我被第二步卡住了,因为我根本无法让它工作。触摸事件方法一调用,就会出现空白屏幕。在空白屏幕上只画了一个挖孔图像。我把文件通读了一遍,但不知道这里出了什么问题 这是我的密码 class DrawSurface extends android.view.SurfaceView implements Surf

我是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;
}
class DrawSurface扩展了android.view.SurfaceView实现了SurfaceHolder.Callback、view.OnTouchListener{
位图mBMPField;
位图mBMPHole;
浮雕浮雕;
浮动x=0;
浮动y=0;
布尔值=假;
公共绘图面(上下文){
超级(上下文);
getHolder().addCallback(此);
setOnTouchListener(这个);
}
公共绘图面(上下文,属性集属性集){
super(上下文,属性集);
getHolder().addCallback(此);
setOnTouchListener(这个);
}
公共绘图面(上下文上下文、属性集属性集、int-styleAttr){
super(上下文、属性集、样式属性);
getHolder().addCallback(此);
setOnTouchListener(这个);
}
@凌驾
已创建的公共空白表面(表面持有人){
Log.i(“a”,“内表面处理”);
setWillNotDraw(假);
mBMPField=BitmapFactory.decodeResource(getResources(),R.drawable.field);
mBMPHole=BitmapFactory.decodeResource(getResources(),R.drawable.hole);
表面层=支架;
Canvas=null;
试一试{
canvas=surfaceHolder.lockCanvas();
Log.d(this.getClass().getName(),String.valueOf(canvas.hashCode());
drawBitmap(mBMPField,0,0,null);
Paint paintText=新的Paint();
paintText.setColor(Color.WHITE);
paintText.setStyle(Paint.Style.FILL);
paintText.setAntiAlias(true);
paintText.setTextSize(20);
ArrayList mItems=loadItems();
用于(项目:mItems){
item.setX((int)(Math.random()*this.getWidth());
item.setY((int)(Math.random()*this.getHeight());
drawText(item.getText()、item.getX()、item.getY()、paintText);
}
}
最后
{
if(canvas!=null){
surfaceHolder.unlockCanvasAndPost(画布);
}
}
}
@凌驾
公共空白表面更改(表面文件夹持有者、整型格式、整型宽度、整型高度){
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
}
@凌驾
公共布尔onTouch(视图v,运动事件){
Log.i(“a”,“内部onTouch”);
if(event.getAction()==MotionEvent.ACTION\u向下){
if(surfaceHolder.getSurface().isValid()){
触摸=真实;
x=event.getX();
y=event.getY();
Canvas=null;
试一试{
canvas=surfaceHolder.lockCanvas();
Log.d(this.getClass().getName(),String.valueOf(canvas.hashCode());
drawBitmap(mBMPHole,x-mBMPHole.getWidth()/2,y-mBMPHole.getHeight()/2,null);
}
最后{
if(canvas!=null){
surfaceHolder.unlockCanvasAndPost(画布);
}
}
}
}
返回false;
}
私人类项目{
私人INTX;
私营企业;
私有字符串文本;
发现私有布尔值;
公共布尔值isFound(){
发现退货;
}
找到公共void集合(找到布尔值){
this.found=found;
}
公共字符串getText(){
返回文本;
}
公共void setText(字符串文本){
this.text=文本;
}
公共int getX(){
返回x;
}
公共无效集合x(整数x){
这个.x=x;
}
公共int getY(){
返回y;
}
公共空间设置(整数y){
这个。y=y;
}
}
私有ArrayList loadItems()
{
InputStream input=getResources().openRawResource(R.raw.items);
BufferedReader reader=null;
ArrayList items=新建ArrayList();
弦线;
尝试
{
reader=新的BufferedReader(新的InputStreamReader(输入));
而((line=reader.readLine())!=null)
{
项目=新项目();
item.setText(行);
项目。添加(项目);
}
}
捕获(例外e)
{
Log.e(“MainActivity”,“读取项目列表失败!”,e);
}
最后{
尝试
{
if(reader!=null)reader.close();
}
捕获(例外e)
{
Log.e(“MainActivity”,“关闭文件读取器时出错”,e);
}
}
退货项目;
}
}

这里的任何建议都会非常有用