简体   繁体   中英

onDraw() not being called when move with canvas to borders of display

I have an issue with calling method onDraw(). I've tried find some solutions here but nothing helped.

The first of all, I have a paint application and for moving with shapes and background on canvas I'm using View.onTouchListener(). For redrawing canvas there is called method invalidate(). For API 28 it seems allright. It works correctly. But for API 24 and lower, the redrawing stopping when I reach borders of display.

Here is exmaple video: https://youtu.be/Nc3SLFff2Gg

In MotionEvent ACTION_MOVE there is called a method draw(), but only for API 24 and lower. It seemed to be working better but not too enough. Example in the video. Without it the onDraw() doesn't work when I reach borders of display and then it's never called again.

There is method onDraw():

 @Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    mCanvas.drawColor(mBackgroundColor);
    if (mBitmapBackground.getBitmap() != null) {
        //zvetsovani a zmensovani vlozene fotky na pozadi
        mCanvas.setMatrix(mMatrix);

        //cele kouzlo, aby se nedeformovalo pozadi
        mCanvas.drawBitmap(mBitmapBackground.getBitmap(), mBitmapBackground.getLeft(), mBitmapBackground.getTop(), mPaint);
    }

    for (SMFBaseShape shape : mShapes) {
        if (shape.isEraser()) {
            shape.setMatrix(mMatrix);
            shape.drawOnCanvas();
        }
    }

    for (SMFBaseShape shape : mShapes) {
        if (!shape.isEraser()) {
            shape.setMatrix(mMatrix);
            shape.drawOnCanvas();
        }
    }

    setCanvasBounds(canvas);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.restore();
}

Here is log:

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

Activity layout:

<?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>

Thanks for your suggestions :)

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

Maybe I have a solution. I've added a method invalidateOutline() and it seems that the redrawing finally works.

But what the method is exactly doing? Can I use it by calling it when I moving with a finger on the screen?

Thanks 😊

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM