【问题标题】:setting a drawText object visible and invisible android设置一个drawText对象可见和不可见的android
【发布时间】:2014-02-11 13:07:26
【问题描述】:

我想设置一个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);

    }

这是我的 onTouchEvent

@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.java

public static String currLoc = "Hello World";

    public static boolean isVisible = false;

    public static int counter = 0;

我的问题是,它不起作用。除了我目前所做的之外,我不知道应该如何让它工作。

【问题讨论】:

    标签: java android canvas touch-event


    【解决方案1】:

    当您通过调用invalidate()... 更改isVisible 的值时,使View 绘制它的内容...并且当您想监听点击事件时,请使用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;
    }
    

    【讨论】:

    【解决方案2】:

    只需使用事实,如果它不可见,请将其设置为可见,反之亦然。并且不要对 isVisible 使用static 声明,因为您想更改它。

      case MotionEvent.ACTION_MOVE:
            if (!GetterSetter.isVisible) {
                GetterSetter.isVisible = true;
            } else {
                GetterSetter.isVisible = false;
            }
            break;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      相关资源
      最近更新 更多