如何在特定位置单击按钮绘制圆圈?

时间:2019-07-17 04:40:09

标签: java android

我对Android来说还很陌生,所以如果我错过了一些东西,我会提前道歉,但是我的问题是,每次单击屏幕时,我都试图在某个位置绘制一个圆圈。

我尝试了日志记录,但确实返回一个与我的if语句匹配的int,但未绘制任何内容。

 public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                drawCirc = true;
                xTouch = event.getX();
                Log.d("keyboard", "xpos" + xTouch);
                yTouch = event.getY();
                break;

            case MotionEvent.ACTION_UP:
                drawCirc = false;
        }
        return true;
    }


    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        if(drawCirc) {
            if (xTouch < 150 && xTouch>0) {
                    paint.setColor(Color.RED);
                    canvas.drawCircle(150, 500, 100, paint);
                    isPlayer1 = false;
                    invalidate();
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

@Javanator是正确的,您应该在触摸监听器中执行invalidate

同时,您可以尝试以下代码,该代码在绘制圆时会添加动画。

Paint paint = new Paint();

{
    paint.setColor(Color.RED);
}

// The radius of the circle you want to draw
// 0 by default
private int radius = 0;

// The animator to animate circle drawing
private ObjectAnimator radiusAnimator;

public void setRadius(int newRadius) {
    this.radius = newRadius;
    this.invalidate();
}

private void showCircle(boolean show) {
    ObjectAnimator animator = this.getRadiusAnimator();
    if (show) {
        animator.start();
    } else {
        animator.reverse();
    }
}

private ObjectAnimator getRadiusAnimator() {
    if (this.radiusAnimator == null) {
        this.radiusAnimator = ObjectAnimator.ofInt(this, "radius", 0, 100);
    }
    return this.radiusAnimator;
}


public boolean onTouchEvent(MotionEvent event) {
    switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            // drawCirc = true;
            xTouch = event.getX();
            Log.d("keyboard", "xpos" + xTouch);
            yTouch = event.getY();
            showCircle(true);
            break;

        case MotionEvent.ACTION_UP:
            // drawCirc = false;
            showCircle(false);
    }
    return true;
}


public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (xTouch < 150 && xTouch > 0) {
        canvas.drawCircle(xTouch, yTouch, radius, paint);
    }
    /*
    if(drawCirc) {
        if (xTouch < 150 && xTouch>0) {
                paint.setColor(Color.RED);
                canvas.drawCircle(150, 500, 100, paint);
                isPlayer1 = false;
                invalidate();
    */            
 }

答案 1 :(得分:0)

public class CustomView extends View {
boolean drawCirc;
float xTouch;
boolean isPlayer1;
float yTouch;
public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        drawCirc=true;
        xTouch = event.getX();
        yTouch = event.getY();
        invalidate();
    }
    return false;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    if (drawCirc) {
        paint.setColor(Color.RED);
        canvas.drawCircle(xTouch, yTouch, 100, paint);
        isPlayer1 = false;
        invalidate();
    }
}}

我已经解决了您的功能并实现了示例代码。我已经检查了它是否可以正常工作,并且您没有在onTouch Listener中放入代码,这就是为什么它不起作用的原因,但是现在我已经解决了。如果您要限制区域,则将您的if (xTouch < 150 && xTouch>0)