0
votes

I had a strange issue about View.onDraw(), I had a Canvas that draw some text with a Bitmap, in onDraw method, I draw that Bitmap use the View hold Canvas. when user touch, I invoke startDragging then make some change of Bitmap, but I never perform View.invalidate(), I thought whatever change of Bitmap wouldn't affect the BoardView UI, because I didn't invalidate it to refresh, but it refresh with new change of Bitmap immediately without onDraw() invoke, I set a breakpoint at onDraw first line and Debugging, but onDraw not invoke. After that, I invoke invalidate() after startDragging(), I stop with onDraw invoking at first line but I saw BoardView already refresh.
I don't know what they do before onDraw(), Does Canvas will know I change Bitmap and use it immediately? I think all the UI change will invoke onDraw method and it can control how to change or not, Is wrong with me?

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class BoardView extends View {
    public BoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private Bitmap mMainPageBitmap;
    private Canvas mDrawableCanvas;

    @Override
    protected void onDraw(Canvas canvas) {
        if (mMainPageBitmap == null) initDrawing(canvas.getWidth(), canvas.getHeight());
        canvas.drawBitmap(mMainPageBitmap, 0, 0, null);
    }

    private void initDrawing(int width, int height) {
        mMainPageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        mDrawableCanvas = new Canvas();
        drawCurrentPage();
    }

    private void drawCurrentPage() {
        mDrawableCanvas.setBitmap(mMainPageBitmap);

        mDrawableCanvas.save(Canvas.MATRIX_SAVE_FLAG);
        mDrawableCanvas.translate(10, 20);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.BLUE);
        paint.setTextSize(30);

        mDrawableCanvas.drawLine(0, 50, getWidth(), 50, paint);
        mDrawableCanvas.drawText("test", 0, 4, 0, 0, paint);

        mDrawableCanvas.restore();
    }

    public void startDragging() {
        drawCurrentPage();
        // invalidate();
    }
}

I had another project did that same but it haven't this issue, I compare both carefully then I find a little difference in onDraw method about Canvas instance. The work well version is use android.view.Surface$CompatibleCanvas, and the work wrong version is use android.view.GLES20RecordingCanvas, I'm not have OpenGL invoke anymore, so I don't know why it used, Is that difference make me wrong? CompatibleCanvas

RecordingCanvas

---------------update 1---------------
I'm look at Android View Drawing, they said "If you set a background drawable for a View, then the View will draw it for you before calling back to its onDraw() method", so I have confirm I didn't set background to View neither XML Define or programmatically.

The Android 2d-graphics said "The Android framework will only call onDraw() as necessary", Does framework consider change Bitmap is unnecessary to be calling onDraw()?

1

1 Answers

0
votes

I think I'm made a stupid mistake, I set the android:targetSdkVersion=16 so the platform compiling my source code as API level 16, I change it as 10 then solve problem, very very stupid.

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />