我正在ondraw函数中调用getdrawingcache。问题是,它只在第一次包含对画布的更改,之后,它根本不会随新更改而更新。这是我的代码:

        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(true),因为如果我从ondraw调用它,getDrawingCache将返回null。)
如图所示,我通过遍历位图和获取数组中的值来计算黑色像素的数量,正如我所说的,我第一次得到了我所期望的数字,但之后,应该会增加,但根本没有改变!
有人知道怎么了吗?
谢谢。

最佳答案

我解决了。问题是,在OnDraw的画布上的最后一个绘制操作之前,我调用了setDrawingCacheenabled(true)。必须在绘制完成后调用它,否则将无法获得正确的结果。

07-24 09:44
查看更多