位图更改将使View在onDraw()和无效无效调用之前自动刷新 [英] Bitmap change will make View auto refresh before onDraw() and regardless invalidate invoke

查看:295
本文介绍了位图更改将使View在onDraw()和无效无效调用之前自动刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题关于View.onDraw(),我有一个Canvas绘制一些Bitmap的文本,在onDraw方法,我绘制Bitmap使用视图保持Canvas。当用户触摸时,我调用 startDragging 然后进行一些Bitmap的更改,但我从来不执行 View.invalidate(),我认为Bitmap的任何更改都不会影响BoardView UI,因为我没有使它刷新,但它刷新与Bitmap 立即的新变化,而没有 onDraw()调用,我在onDraw第一行设置断点,调试,但onDraw不调用。之后,我调用invalidate()后startDragging(),我停止onDraw调用在第一行,但我看到BoardView已经刷新。

我不知道他们在onDraw()之前做了什么, Canvas会知道我改变Bitmap并立即使用它?我想所有的UI更改将调用onDraw方法,它可以控制如何更改或不是,我错了吗?

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();
    }
}



我有另一个项目, t这个问题,我仔细比较,然后我发现在onDraw方法关于Canvas实例有点不同。工作井版本是使用 android.view.Surface $ CompatibleCanvas ,和工作错误的版本是使用 android.view.GLES20RecordingCanvas ,我没有OpenGL调用了,所以我不知道为什么它使用,这是差异让我错了吗?

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?

------------ --- 更新1 ---------------

我看看 Android视图绘图,他们说如果你设置一个背景可绘一个视图,然后视图将绘制它之前,回调到它的onDraw()方法,所以我已经确认我没有设置背景查看既没有XML定义或程序。

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

Android 2d图形 a>说Android框架将只根据需要调用onDraw(),框架考虑改变Bitmap是不必要调用onDraw()?

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

推荐答案

我认为我是一个愚蠢的错误,我设置 android:targetSdkVersion = 16所以平台编译我的源代码作为API级别16,我更改为10然后解决非常非常愚蠢。

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" />

这篇关于位图更改将使View在onDraw()和无效无效调用之前自动刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆