android中矩形触摸事件的检测

android中矩形触摸事件的检测,android,events,ontouch,Android,Events,Ontouch,我用android编写了一个非常简单的游戏,游戏就像触摸一个形状(如矩形、圆形、弧形…),为此我编写了一个线程maingametrade.class: package com.example.komeil.surfaceview_2; import android.graphics.Canvas; import android.graphics.Color; import android.view.SurfaceHolder; import java.util.Random; /*

我用android编写了一个非常简单的游戏,游戏就像触摸一个形状(如矩形、圆形、弧形…),为此我编写了一个线程maingametrade.class:

    package com.example.komeil.surfaceview_2;

import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;

import java.util.Random;

/**
 * Created by Komeil on 04/11/2016.
 */
public class MainGameThread extends Thread {

    private boolean running;
    private SurfaceHolder surfaceHolder;
    private MainGameView mainGameView;
    private String shapeType;
    private Random rnd;
    private int xPos;
    private int yPos;
    private int radius;
    private int left;
    private int right;
    private int top;
    private int bottom;
    private int color;


    public MainGameThread (SurfaceHolder surfaceHolder,MainGameView mainGameView)
    {
        super();
        this.surfaceHolder = surfaceHolder;
        this.mainGameView = mainGameView;
    }


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

    @Override
    public void run() {
        while (running)
        {

            Canvas canvas = null;
            try {
                canvas = surfaceHolder.lockCanvas();
                synchronized (surfaceHolder)
                {

                    makeChoose();
                    mainGameView.onDraw(canvas);
                }

            }
            finally {
                if(canvas != null)
                    surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    private void makeChoose()
    {
        int radius;
        rnd = new Random();
        switch (rnd.nextInt(2))
        {
            case 0:
                setShapeType("Rectangle");
                setColor(Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255)));
                setXpos(rnd.nextInt(mainGameView.getWidth()));
                setYpos(rnd.nextInt(mainGameView.getHeight()));
                setTop(rnd.nextInt(mainGameView.getHeight()));
                setBottom(rnd.nextInt(mainGameView.getHeight()));
                setLeft(rnd.nextInt(mainGameView.getWidth()));
                setRight(rnd.nextInt(mainGameView.getWidth()));
                break;
            case 1:
                setShapeType("Circle");
                setColor(Color.rgb(rnd.nextInt(255),rnd.nextInt(255),rnd.nextInt(255)));

                setXpos(rnd.nextInt(mainGameView.getWidth()));
                setYpos(rnd.nextInt(mainGameView.getHeight()));
                radius = rnd.nextInt(mainGameView.getWidth()-getXpos());
                setRadius(radius);
                break;
//            case 2:
//                setShapeType("Arc");
//                setXpos(rnd.nextInt(mainGameView.getWidth()));
//                setYpos(rnd.nextInt(mainGameView.getHeight()));
//                break;
        }
    }

    private void setShapeType(String shapeType)
    {
        this.shapeType = shapeType;
    }

    private void setColor(int color) {
        this.color = color;
    }

    public int getColor() {
        return color;
    }

    private void setRadius(int radius)
    {
        this.radius = radius;
    }

    public int getRadius()
    {
        return radius;
    }

    public int getBottom() {
        return bottom;
    }

    public int getLeft() {
        return left;
    }

    public int getRight() {
        return right;
    }

    public int getTop() {
        return top;
    }

    private void setBottom(int bottom) {
        this.bottom = bottom;
    }

    private void setLeft(int left) {
        this.left = left;
    }

    private void setRight(int right) {
        this.right = right;
    }

    private void setTop(int top) {
        this.top = top;
    }

    private void setXpos(int xPos)
    {
        this.xPos = xPos;
    }

    public int getXpos()
    {
        return xPos;
    }

    private void setYpos(int yPos)
    {
        this.yPos = yPos;
    }

    public int getYpos()
    {
        return yPos;
    }
    public String getShapeType()
    {
        return shapeType;
    }


}
所以,当我在UI线程中使用绘图时,它是工作。但当我触摸矩形时 这就像什么都没发生一样(我使用分数,我想在触摸矩形时更新分数),但分数并没有显示在屏幕上,有时会自动显示并很快消失。 下面是my MainGameView.class的代码:

    package com.example.komeil.surfaceview_2;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by Komeil on 04/11/2016.
 */
public class MainGameView extends SurfaceView implements SurfaceHolder.Callback {
    private Bitmap bmp;
    MainGameThread mainGameThread;
    private int x;
    private Paint paint;
    private Paint textWhite;
    private int score;
    private Rect rect;
    private boolean touched;
    public MainGameView(Context context) {
        super(context);
        getHolder().addCallback(this);
        mainGameThread = new MainGameThread(getHolder(),this);
        setFocusable(true);
        paint = new Paint();
        textWhite = new Paint();
        textWhite.setAntiAlias(true);
        textWhite.setColor(Color.WHITE);
        textWhite.setTextSize(30);
        paint.setAntiAlias(true);
        bmp = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mainGameThread.setRunning(true);
        mainGameThread.start();

    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        mainGameThread.setRunning(false);
        while (retry)
        {
            try {
                mainGameThread.join();
                retry = false;
            }
            catch (InterruptedException ex)
            {

            }
        }
    }



    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.GRAY);
        if(x<getWidth()-bmp.getWidth())
            x+=2;
        else
            x=0;
        canvas.drawBitmap(bmp,x,10,null);

        switch (mainGameThread.getShapeType())
        {
            case "Rectangle":

                paint.setColor(mainGameThread.getColor());
                canvas.drawRect(mainGameThread.getLeft(),mainGameThread.getTop(),mainGameThread.getRight(),mainGameThread.getBottom(),paint);
                break;
            case "Circle":
                paint.setColor(mainGameThread.getColor());
                canvas.drawCircle(mainGameThread.getXpos(),mainGameThread.getYpos(),mainGameThread.getRadius(),paint);
                break;
//            case "Arc":
//                paint.setColor(mainGameThread.getColor());
//                canvas.drawArc(mainGameThread.getLeft(),mainGameThread.getRight()
//                        ,mainGameThread.getTop(),mainGameThread.getBottom(),);
//                break;
        }
        if(touched)
        {
            canvas.drawText(String.valueOf(score),getWidth()/2,50,textWhite);
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int eventAction = event.getAction();
        int xEvent = (int)event.getX();
        int yEvent = (int)event.getY();

        switch (eventAction)
        {
            case MotionEvent.ACTION_DOWN:
                switch (mainGameThread.getShapeType())
                {
                    case "Rectangle":
                        rect = new Rect(mainGameThread.getLeft(),mainGameThread.getTop(),mainGameThread.getRight(),mainGameThread.getBottom());
                        if(rect.contains(xEvent,yEvent))
                        {
                            touched = true;
                            score +=1;
                        }
                        else
                        {
                            touched = false;
                        }
                        break;
                    case "Circle":
                        break;
                    case "Arc":
                        break;
                }

                break;
            case MotionEvent.ACTION_UP:
                touched = false;
                break;
        }
        return super.onTouchEvent(event);
    }
}
package com.example.komeil.surfaceview_2;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.graphics.Rect;
导入android.view.MotionEvent;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
/**
*由Komeil于2016年11月4日创建。
*/
公共类MainGameView扩展了SurfaceView实现了SurfaceHolder.Callback{
私有位图bmp;
MainGameThread MainGameThread;
私人INTX;
私人油漆;
私人油漆,白色;
个人智力得分;
私人直肠;
私有布尔值;
公共MainGameView(上下文){
超级(上下文);
getHolder().addCallback(此);
mainGameThread=新的mainGameThread(getHolder(),this);
设置聚焦(真);
油漆=新油漆();
text白色=新油漆();
textWhite.setAntiAlias(true);
textWhite.setColor(Color.WHITE);
textwite.setTextSize(30);
paint.setAntiAlias(真);
bmp=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_启动器);
}
@凌驾
已创建的公共空白表面(表面持有人){
mainGameThread.setRunning(true);
mainGameThread.start();
}
@凌驾
公共空白表面更改(表面文件夹持有者、整型格式、整型宽度、整型高度){
}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
布尔重试=真;
mainGameThread.setRunning(false);
while(重试)
{
试一试{
mainGameThread.join();
重试=错误;
}
捕获(中断异常例外)
{
}
}
}
@凌驾
受保护的void onDraw(画布){
画布。drawColor(颜色。灰色);
if(x)