getDrawingCache不更新 [英] getDrawingCache is not updated

查看:197
本文介绍了getDrawingCache不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OnDraw函数调用getDrawingCache。的问题是,它包含改变到画布仅在第一时间,并且在这之后,它不是在所有的新的变化更新。这里是我的code:

I'm calling getDrawingCache in the onDraw function. The problem is that it contains the changes to the canvas only in the first time, and after that, it's not updated at all with the new changes. Here's my code:

        paintAction.draw(canvas);
        if (paintAction.isPermanentChange())
        {
            Bitmap partialBitmap=getDrawingCache();
            int numColored=0;
            for (int index1=0;index1<partialBitmap.getWidth();index1++)
            {
                for (int index2=0;index2<partialBitmap.getHeight();index2++)
                {
                    if (partialBitmap.getPixel(index1,index2)==0xFF000000) 
                        numColored++;
                }
            }
            Log.i("PaintDroid","Bitmap pixels: " + numColored);
            int areaWidth=partialBitmap.getWidth()-SCROLLBAR_SIZE;
            int areaHeight=partialBitmap.getHeight()-SCROLLBAR_SIZE;
            int[] pixels=new int[areaWidth*areaHeight];
            partialBitmap.getPixels(pixels,0,areaWidth,0,0,areaWidth,
                    areaHeight);    
            numColored=0;
            for (int index=0;index<pixels.length;index++)
                if (pixels[index]==0xFF000000) numColored++;
            Log.i("PaintDroid","Pixels: " + numColored);

(setDrawingCache(真)在创建视图时被调用,因为如果我把它的onDraw,getDrawingCache将返回null。)

(setDrawingCache(true) is called when the view is created, because if I call it from onDraw, getDrawingCache will return null.)

可以看出,我就指望黑色像素,无论是数量通过遍历位图和数组中获取值,正如我所说,我得到我预期在第一时间数量,但后即,它应该增加,但不会有任何变化!

As can be seen, I'm counting the number of black pixels, both by traversing the bitmap and getting the values in an array, and as I said, I get the number I expected for in the first time, but after that, it was supposed to increase, yet doesn't change at all!

没有任何人有一个想法有什么不对?
谢谢你。

Does anybody have an idea what's wrong? Thanks.

推荐答案

我解决它。问题是,我的onDraw在画布上绘制最后一次操作之前称为setDrawingCacheEnabled(真)。您完成绘制后,它必须被调用,否则你不会得到正确的结果。

I solved it. The problem was that I called setDrawingCacheEnabled(true) before the last draw operation on the canvas in onDraw. It must be called after you finished drawing, otherwise you won't get correct results.

这篇关于getDrawingCache不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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