注册 | 登录

android canvas - can i have multiple draw methods?

itPublisher 分享于



推荐:android draw text at canvas

最近在定制一个画文字的控件,想把文字画到指定的点。开始始终会有几个像素的偏差。 经过研究找到了几个在调用画布来画文字的时候需要注意的地方: 1.文字大小的

I have made my own canvas class which extends an imageView. My onDraw() method draws out the users gps position and I keep calling this onDraw method every time the user moves. My problem is I also want to draw out a gps trail which only needs to be drawn once (doesnt need to be updated when a user moves). I am wondering is it possible to have more than 1 onDraw method or is there any way of separating 1) the user location and 2) the gps trail??

My reason is I do not want to waste memory by redrawing the gps route everytime the users gps position changes. It is a waste.

android android-canvas draw ondraw
edited Jan 31 '14 at 4:06 Kara 3,269 8 33 50 asked Jul 25 '11 at 17:52 user859348 134 1 1 13


3 Answers

Have you seen performance take a hit? If not, don't worry about it. I would think that this would be wasting CPU cycles if anything... not memory. So if the app seems fast enough already, don't worry about optimizing it.

If your app is a bit laggy, and you've found that the trail is the bottleneck... I would suggest caching it into a bitmap. This way, you will still have to draw the trail, but you will not have to calculate the coordinates of the trail on each frame.

answered Jul 25 '11 at 18:01 dfetter88 2,741 9 32 52


I have had to solve a somewhat similar problem recently and I'll explain briefly what I did in case it's of any help.

What you can do is use multiple overlapping Views, where one may contain the background graphics that you don't want to redraw often, and a foreground View that contains the graphics that are frequently updated. Then, to gain performance, you can design the background View's onDraw() so that it is backed by a Bitmap that you then retain as a class variable. In the very first onDraw() of your background graphics, you do the relatively slow drawing to Canvas. In subsequent calls to onDraw(), you simply draw that Bitmap to Canvas.

推荐:Android  draw canvas save restore saveLayer 学习

画图需要四大基本要素: 1、一个用来保存像素的Bitmap; 2、一个Canvas画布,绘制Bitmap操作; 3、绘制的东西 4、绘制的画笔Paint(颜色和样式) 如何获得一个Ca

I've just done this myself. Basically what my application does is display a number of graphical gauges. Those gauges have lots of graphics that are drawn just once (gauge face, number legends), and the pointer graphic that needs to be redrawn over and over as the model data changes. First of all, I split the background graphics and moving foreground graphics into separate overlapping Views. Now, invalidating and redrawing the foreground pointer graphic of course causes anything it overlaps to be invalidated too, so the onDraw() method for the background graphics View is being called each time the pointer View is redrawn. The background View only needs to draw the background graphics once, but retains the Bitmap that backs the canvas, and in subsequent onDraw() calls it draws this bitmap back to Canvas (which is a lot faster than initially creating the graphics using Path() objects).

Bitmap bm;


protected void onDraw(Canvas canvas){
        // do your slow vector graphics drawing to Canvas here
    Paint drawPaint = new Paint();
    canvas.drawBitmap(bm, 0, 0, drawPaint);

answered Jul 25 '11 at 18:11 Trevor 7,679 3 42 66


Well, there can't be more than 1 onDraw method, assuming that I understood your question correctly. You will need to think about alternate approaches about how to handle this.

@DeeV suggested, that can be a solution for you.

answered Jul 25 '11 at 18:02 Kumar Bibek 7,137 2 27 56


推荐:android 调用draw(canvas) 函数自动退出

我试了在android中直接调用TextView的draw 函数,结果直接退出。。。 代码如下: public class Main extends Activity { @Override public void onCreate(Bund








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱