大家好:)我在玩我的Mainthreat画在画布上。
基本上,我的目标是得到一个矩形,当我触摸它时,它会更改颜色。
在onCreate方法中,我创建矩形并调用quadr1.setColor方法对其进行填充。然后我开始威胁。现在draw方法应该只调用quadr1.draw方法,然后用相同的颜色重新绘制矩形。当我触摸Quadr1时,威胁将再次调用quadr1.setColor,而不是quadr1.draw。这应该给它一种由我的RandomColor类提供的新颜色(正常工作)。

现在的问题:当威胁正在运行时,画布只是消失以显示“背后是什么”,并再次出现下一个动作。对于癫痫病患者,这似乎是一种折磨方法。我读过有关双缓冲的信息,但本人找不到任何解决方案。如何获得显示填充稳定的矩形,并且在触摸它时会发生变化?

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{

    private MainThread thread;
    private Canvas canvas;
    private Random random;
    private SurfaceHolder holder;
    RandomColor randomColor = new RandomColor();
    private boolean colorChangeTouchInput;

    private Quadr quadr1;

    public GamePanel(Context context){
        super(context);

        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        this.holder = holder;

        thread = new MainThread(holder);
        randomColor = new RandomColor();

        setFocusable(true);
    }

    class MainThread extends Thread{
        private boolean running;
        private SurfaceHolder surfaceHolder;

        public MainThread(SurfaceHolder surfaceHolder){
            this.surfaceHolder = surfaceHolder;
        }

        @Override
        public void run(){
            while(running) {
                canvas = null;
                try {
                    canvas = surfaceHolder.lockCanvas();
                    synchronized (surfaceHolder) {
                        if(colorChangeTouchInput=false) {
                            draw(canvas);
                        } else {
                            quadr1.setColor(canvas);
                        }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if(canvas!= null){
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }

        private void draw(Canvas canvas) {
            quadr1.draw(canvas);
        }
        public void setRunning(boolean b) {
            running = b;
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
        boolean retry = true;
        int counter =0;
        while(retry && counter <1000) {

            counter++;
            try {
                thread.setRunning(false);
                thread.join();
                retry=false;

            } catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder){


        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int windowWidth = size.x;
        int windowHeight = size.y;

        canvas = null;
        canvas = holder.lockCanvas();

        quadr1 = new Quadr(0,0,windowWidth/2,windowHeight / 2, randomColor.getRandomColorA(), randomColor.getRandomColorR(), randomColor.getRandomColorG(), randomColor.getRandomColorB());
        quadr1.setColor(canvas);

        holder.unlockCanvasAndPost(canvas);

        thread.setRunning(true);
        thread.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        SurfaceHolder holderTouch = getHolder();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(event.getX()>quadr1.getLeft() && event.getX()<quadr1.getRight() && event.getY()>quadr1.getTop() && event.getY()<quadr1.getBot()){
                    colorChangeTouchInput = true;
                }
        }
        return super.onTouchEvent(event);
    }

    public boolean isColorChangeTouchInput() {
        return colorChangeTouchInput;
    }
}


这是我的四合一课程:
(效果很好,随机颜色类也可以用几种方法对其进行测试。)

public class Quadr extends GameObject {
    int left;
    int top;
    int right;
    int bot;
    int colorA[];
    int colorR[];
    int colorG[];
    int colorB[];
    int colorFixed;

    Paint paint = new Paint();
    Rect rect = new Rect();
    Random rnd = new Random();

    public Quadr(int left, int top, int right, int bottom, int colorA[], int colorR[], int colorG[], int colorB[]) {
        this.left=left;
        this.top=top;
        this.right=right;
        this.bot=bottom;
        this.colorA=colorA;
        this.colorR=colorR;
        this.colorG=colorG;
        this.colorB=colorB;

        rect.set(left, top, right, bottom);
    }

    public void setColor(Canvas canvas){
        int num = rnd.nextInt(3);
        colorFixed = num;

        paint.setStyle(Paint.Style.FILL);
    rnd.nextInt(256));
        paint.setARGB(colorA[num], colorR[num], colorG[num], colorB[num]);
        canvas.drawRect(rect, paint);
    }

    public void draw(Canvas canvas) {
        paint.setStyle(Paint.Style.FILL);
        paint.setARGB(colorA[colorFixed], colorR[colorFixed], colorG[colorFixed], colorB[colorFixed]);
        canvas.drawRect(rect, paint);

    }

    public int getLeft() {
        return left;
    }
    public int getTop() {
        return top;
    }
    public int getRight() {
        return right;
    }
    public int getBot() {
        return bot;
    }
}


提前非常感谢您的任何帮助!

最佳答案

据我所知,问题可能是由colorChangeTouchInput引起的。

您在拦截触摸时将其设置为true,此后再也不会将其设置为false。这将导致在线程中永远不会调用draw()方法。建议您在呼叫colorChangeTouchInput = false后添加quadr1.setColor(canvas);

08-04 18:41