在自定义视图中具有无效行为的奇怪行为

时间:2020-08-28 13:43:32

标签: java android android-custom-view invalidation

我有一个customview,当我触摸customview并将其显示在我的customview中时,会在其中增加一个值。

对于95%的用户来说,这很好用,但有时自定义视图会显示旧值,我无法解释这种情况如何发生。

示例: 日志说:

1 2 3 4 5 6

Customview显示:

1 2 3 4 5 4

我的代码:

public class MyView extends View {
    
    private int testvalue = 0;

    private final Handler updateHandler = new Handler() {
        public void handleMessage(final Message msg) {
            testvalue = testvalue + 1;
            invalidate();
        }
    };

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paintstatus.setTextSize(120);
        canvas.drawText("" + testvalue,400,600,paintstatus);
        Log.v("DEBUG_DRAW","" + testvalue);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
            updateHandler.sendEmptyMessage(0);
            return true;
    }
}

日志说:

2020-08-28 08:56:46.638 20171-20171/myapp V/DEBUG_DRAW: 40
2020-08-28 08:56:46.705 20171-20171/myapp  V/DEBUG_DRAW: 41
2020-08-28 08:56:47.505 20171-20171/myapp  V/DEBUG_DRAW: 42
2020-08-28 08:56:47.587 20171-20171/myapp  V/DEBUG_DRAW: 43

在2020-08-28 08:56:47.587时,我在屏幕上看到以下内容: enter image description here

有人对此有解释吗?我在做什么错了?

0 个答案:

没有答案
相关问题