有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java设置drawText对象可见和不可见

我想设置一个drawText对象可见和不可见,只需单击一下。它将以不可见开始,但当用户点击屏幕上的任何位置时,对象将出现,反之亦然,再次点击后,对象将再次不可见

这是我的密码

public void onDraw(Canvas canvas) {

        if (GetterSetter.isVisible) {

            renderText(canvas);

        }
}

private void renderText(Canvas canvas) {
        Paint textPaint = new Paint();
        textPaint.setTextSize(18);
        textPaint.setAntiAlias(true);
        textPaint.setARGB(0xff, 0x00, 0x00, 0x00);
        canvas.drawText(GetterSetter.currLoc, 16, 50, textPaint);

    }

这是我的个人信息

@Override
    public boolean onTouchEvent(MotionEvent e) {

        x = e.getX();
        y = e.getY();

        switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
            if (GetterSetter.counter < 1) {
                GetterSetter.counter++;
                GetterSetter.isVisible = true;
            } else {
                GetterSetter.counter = 0;
                GetterSetter.isVisible = false;
            }
            break;
        }

        return true;
    }

以下是我的常数:GetterSetter。爪哇

public static String currLoc = "Hello World";

    public static boolean isVisible = false;

    public static int counter = 0;

我的问题是,它不起作用。除了我目前所做的之外,我不知道该怎么做


共 (1) 个答案

  1. # 1 楼答案

    当通过调用invalidate()更改isVisiblevalue的值时,使View绘制它的内容。。。当你想监听tap事件时,用ACTION_UP事件代替ACTION_MOVE

    public boolean onTouchEvent(MotionEvent e) {
        x = e.getX();
        y = e.getY();
        switch (e.getAction()) {
        case MotionEvent.ACTION_UP:
            GetterSetter.isVisible = !GetterSetter.isVisible;
            invalidate();
            break;
        }
        return true;
    }