在onDraw()方法之外在Canvas上绘制 [英] Drawing on Canvas outside the onDraw() method

查看:279
本文介绍了在onDraw()方法之外在Canvas上绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的OnDraw()方法

Here's my OnDraw() method

void onDraw(Canvas canvas) {
    mCanvas = canvas;
    //invalidate();
    int x = 0;
    Iterator<Letter> it = mNextUpQueue.iterator();
    while(it.hasNext()){
        mCanvas.drawBitmap(it.next().getNext(), mNextUpCoordinates.get(x).x, mNextUpCoordinates.get(x).y, mPaint);
        mCanvas.drawBitmap(mAvailableLetters.get(x).getNotPressed(), mAvailableLettersCoordinates.get(x).x, mAvailableLettersCoordinates.get(x).y, mPaint);
        x++;
    }
}

我已将canvas设置为全局变量mCanvas。但是,如果我尝试从onDraw()方法外部在mCanvas上绘画,则会出现错误。是因为我做错了还是必须始终在onDraw方法中使用画布?

I have set canvas to a global variable mCanvas. But if I try to paint on mCanvas from outside the onDraw() method I get an error. Is it because I'm doing something wrong or the canvas must always be used from within the onDraw method?

推荐答案

t引用传递的Canvas,因为它仅在 onDraw(Canvas)方法调用期间有效。

You mustn't take the reference to the Canvas passed, as it is only valid during the onDraw(Canvas) method call.

我建议阅读 http://developer.android .com / guide / topics / graphics / 2d-graphics.html#draw-with-canvas 进行了彻底的解释,其中可能的方法已得到很好的解释:

I recommend reading http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas thoroughly, the possible ways are explained there quite well:


  1. 在此方法调用期间,提供给 onDraw(Canvas)方法的Canvas。这是在UI线程中完成的,因此在这里不要尝试复杂的事情

  2. 对于您自己创建的Canvas。这对于在另一个线程中准备位图,然后将位图绘制到指定给 onDraw(Canvas)方法的Canvas上很有用。

  3. 使用SurfaceView,并使用 lockCanvas()方法从其中获取Canvas对象。

  1. To the Canvas provided to the onDraw(Canvas) method, during this method call. This is done in the UI thread, so nothing complicated should be attempted here
  2. To a Canvas you created yourself. This could be useful for preparing a bitmap in another thread and then drawing the bitmap to the Canvas given to onDraw(Canvas) method
  3. Using a SurfaceView, and obtaining a Canvas object from it with lockCanvas() method.

这篇关于在onDraw()方法之外在Canvas上绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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