位图的改变会使查看自动刷新的onDraw()之前,且不论调用无效 [英] Bitmap change will make View auto refresh before onDraw() and regardless invalidate invoke

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

问题描述

我对View.onDraw一个奇怪的问题(),我有画一些文字与位图画布中的onDraw方法,我绘制位图使用视图保持画布。当用户触摸,我调用 startDragging ,然后做位图的一些变化,但我从来没有执行 View.invalidate(),我想任何位图的变化不会影响BoardView UI,因为我没有使它无效刷新,但它与位图的新变化刷新的立即没有的的onDraw()的调用,我在的onDraw第一行设置一个断点,调试,但不是的onDraw调用。在那之后,我调用无效()后startDragging(),我的onDraw援引在第一线,但我看到BoardView已经刷新停止。

我不知道他们的onDraw()之前做呢,难道帆布会知道我改变位图,并立即使用它?我认为所有的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();
    }
}

我有另一个项目做相同的,但它没有这个问题,我都比较仔细,然后我找到的onDraw方法有关帆布实例的差别不大。要把工作做好的版本是使用 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 ---------------

我看<一个href=\"http://developer.android.com/reference/android/view/View.html#onDraw%28android.graphics.Canvas%29\"相对=nofollow> 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.

href=\"http://developer.android.com/guide/topics/graphics/2d-graphics.html\" rel=\"nofollow\"> Android的2D图形说,

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这样的平台上编译我的源code作为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" />

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

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