Java 使用画布移动到显示边界时未调用onDraw()

Java 使用画布移动到显示边界时未调用onDraw(),java,android,canvas,ondraw,Java,Android,Canvas,Ondraw,我在调用方法onDraw()时遇到问题。我试着在这里找到一些解决办法,但没有任何帮助 首先,我有一个绘画应用程序,为了在画布上移动形状和背景,我使用View.onTouchListener()。对于重新绘制画布,有一个名为invalidate()的方法。对于API 28,这似乎是正确的。它工作正常。但对于API24及更低版本,当我到达显示边界时,重绘停止 以下是exmaple视频: 在MotionEvent ACTION_MOVE中,称为方法draw(),但仅适用于API 24及更低版本。它似乎

我在调用方法onDraw()时遇到问题。我试着在这里找到一些解决办法,但没有任何帮助

首先,我有一个绘画应用程序,为了在画布上移动形状和背景,我使用View.onTouchListener()。对于重新绘制画布,有一个名为invalidate()的方法。对于API 28,这似乎是正确的。它工作正常。但对于API24及更低版本,当我到达显示边界时,重绘停止

以下是exmaple视频:

在MotionEvent ACTION_MOVE中,称为方法draw(),但仅适用于API 24及更低版本。它似乎工作得更好,但还不够。视频中的示例。如果没有它,当我到达显示边界时,onDraw()将无法工作,然后再也不会调用它

有一个方法onDraw():

以下是日志:

2020-02-07 07:16:39.508 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:39.509 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:39.509 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:39.656 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 9.268ms
2020-02-07 07:16:39.663 19966-19977/cz.kctdata.mpaint.innogy I/art: Background sticky concurrent mark sweep GC freed 37(1608B) AllocSpace objects, 0(0B) LOS objects, 0% free, 52MB/52MB, paused 10.348ms total 40.218ms
2020-02-07 07:16:40.817 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:40.818 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
2020-02-07 07:16:41.183 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:41.184 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:41.184 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:41.330 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 7.160ms
2020-02-07 07:16:41.337 19966-19977/cz.kctdata.mpaint.innogy I/art: Background sticky concurrent mark sweep GC freed 35(1568B) AllocSpace objects, 0(0B) LOS objects, 0% free, 43MB/43MB, paused 11.693ms total 31.519ms
2020-02-07 07:16:41.503 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 5.428ms
2020-02-07 07:16:41.510 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 124(4KB) AllocSpace objects, 3(25MB) LOS objects, 37% free, 26MB/42MB, paused 11.120ms total 46.491ms
2020-02-07 07:16:41.615 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 5.913ms
2020-02-07 07:16:41.618 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 87(3KB) AllocSpace objects, 2(16MB) LOS objects, 37% free, 26MB/42MB, paused 6.992ms total 37.200ms
2020-02-07 07:16:42.342 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 5.979ms
2020-02-07 07:16:42.345 19966-19977/cz.kctdata.mpaint.innogy I/art: Background sticky concurrent mark sweep GC freed 24(776B) AllocSpace objects, 0(0B) LOS objects, 0% free, 53MB/53MB, paused 7.220ms total 13.834ms
2020-02-07 07:16:42.427 19966-19977/cz.kctdata.mpaint.innogy I/art: Background sticky concurrent mark sweep GC freed 19(632B) AllocSpace objects, 0(0B) LOS objects, 0% free, 53MB/53MB, paused 5.091ms total 11.886ms
2020-02-07 07:16:42.464 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 10.545ms
2020-02-07 07:16:42.467 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 125(4KB) AllocSpace objects, 3(26MB) LOS objects, 30% free, 35MB/51MB, paused 11.655ms total 39.680ms
2020-02-07 07:16:42.521 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:42.523 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
2020-02-07 07:16:42.829 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:42.829 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:42.829 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:43.764 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:43.765 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
2020-02-07 07:16:44.421 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:44.421 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:44.421 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:44.885 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 5.606ms
2020-02-07 07:16:44.890 19966-19977/cz.kctdata.mpaint.innogy I/art: Background sticky concurrent mark sweep GC freed 24(776B) AllocSpace objects, 0(0B) LOS objects, 0% free, 70MB/70MB, paused 6.978ms total 13.931ms
2020-02-07 07:16:45.094 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:45.095 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
2020-02-07 07:16:45.506 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:45.506 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:45.506 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:45.747 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 157(5KB) AllocSpace objects, 5(43MB) LOS objects, 23% free, 53MB/69MB, paused 5.407ms total 39.418ms
2020-02-07 07:16:46.108 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 6.487ms
2020-02-07 07:16:46.111 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 143(4KB) AllocSpace objects, 3(26MB) LOS objects, 26% free, 44MB/60MB, paused 7.583ms total 42.841ms
2020-02-07 07:16:46.261 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:46.261 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
2020-02-07 07:16:46.613 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:46.614 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:46.614 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:47.250 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 7.470ms
2020-02-07 07:16:47.252 19966-19977/cz.kctdata.mpaint.innogy I/art: Background sticky concurrent mark sweep GC freed 24(776B) AllocSpace objects, 0(0B) LOS objects, 0% free, 53MB/53MB, paused 8.739ms total 17.598ms
2020-02-07 07:16:47.448 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:47.449 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
2020-02-07 07:16:47.759 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:47.759 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:47.760 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:48.015 19966-19977/cz.kctdata.mpaint.innogy I/art: Background sticky concurrent mark sweep GC freed 28(1000B) AllocSpace objects, 0(0B) LOS objects, 0% free, 61MB/61MB, paused 6.823ms total 17.334ms
2020-02-07 07:16:48.181 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 9.486ms
2020-02-07 07:16:48.184 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 142(5KB) AllocSpace objects, 3(25MB) LOS objects, 30% free, 35MB/51MB, paused 10.515ms total 52.516ms
2020-02-07 07:16:48.340 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 10.629ms
2020-02-07 07:16:48.345 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 130(4KB) AllocSpace objects, 3(25MB) LOS objects, 31% free, 35MB/51MB, paused 11.778ms total 56.589ms
2020-02-07 07:16:48.688 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 11.928ms
2020-02-07 07:16:48.691 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 90(3KB) AllocSpace objects, 2(16MB) LOS objects, 37% free, 26MB/42MB, paused 13.499ms total 42.253ms
2020-02-07 07:16:48.706 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:48.707 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
2020-02-07 07:16:49.060 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 0
2020-02-07 07:16:49.060 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=DRAG
2020-02-07 07:16:49.060 19966-19966/cz.kctdata.mpaint.innogy I/cz.kctdata.mpaint.innogy.listeners.SMFShapeInteractListener: Build Version SDK: 24
2020-02-07 07:16:49.307 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 125(4KB) AllocSpace objects, 3(25MB) LOS objects, 37% free, 26MB/42MB, paused 5.568ms total 39.592ms
2020-02-07 07:16:49.420 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 15.885ms
2020-02-07 07:16:49.423 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 86(3056B) AllocSpace objects, 2(16MB) LOS objects, 37% free, 26MB/42MB, paused 16.908ms total 48.915ms
2020-02-07 07:16:49.614 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 6.277ms
2020-02-07 07:16:49.618 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 83(2952B) AllocSpace objects, 2(16MB) LOS objects, 37% free, 26MB/42MB, paused 7.650ms total 33.178ms
2020-02-07 07:16:49.844 19966-19977/cz.kctdata.mpaint.innogy W/art: Suspending all threads took: 21.130ms
2020-02-07 07:16:49.847 19966-19977/cz.kctdata.mpaint.innogy I/art: Background partial concurrent mark sweep GC freed 68(2392B) AllocSpace objects, 2(16MB) LOS objects, 37% free, 26MB/42MB, paused 22.507ms total 48.887ms
2020-02-07 07:16:49.890 19966-19966/cz.kctdata.mpaint.innogy D/ViewRootImpl@b2860ba[SMFMainActivity]: ViewPostImeInputStage processPointer 1
2020-02-07 07:16:49.890 19966-19966/cz.kctdata.mpaint.innogy D/cz.kctdata.mpaint.innogy.listeners.SMFBaseTouchListener: mMode=NONE
活动布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="cz.kctdata.mpaint.innogy.activity.SMFMainActivity">

<cz.kctdata.mpaint.innogy.drawtool.SMFPaintView
    android:id="@+id/pvActivityMain_paintView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:longClickable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_height="100dp"
    tools:layout_width="100dp" />

<cz.kctdata.mpaint.innogy.widget.SMFShapeSettingsWidget
    android:id="@+id/sSActivityMain_shapeSettings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>


感谢您的建议:)

如果您在一个视图的onDraw函数中绘制所有图形,那么一旦视图离开屏幕,操作系统可能会停止渲染该视图,这就是不再调用该视图的onDraw函数的原因

也许我有个解决办法。我添加了一个invalidateOutline()方法,看起来重画终于可以工作了

但是这个方法到底在做什么呢?当我用手指在屏幕上移动时,我可以通过调用它来使用它吗


谢谢,是的,但是画面一直在屏幕上。视图不会离开屏幕,甚至不会移动一个像素。您可以发布活动的布局文件吗?活动的布局将添加到主要问题帖子中。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="cz.kctdata.mpaint.innogy.activity.SMFMainActivity">

<cz.kctdata.mpaint.innogy.drawtool.SMFPaintView
    android:id="@+id/pvActivityMain_paintView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:longClickable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_height="100dp"
    tools:layout_width="100dp" />

<cz.kctdata.mpaint.innogy.widget.SMFShapeSettingsWidget
    android:id="@+id/sSActivityMain_shapeSettings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>