Android 如何获得画布像素

Android 如何获得画布像素,android,colors,android-canvas,Android,Colors,Android Canvas,我有一块画布,在上面画线: //see code upd 我需要制作吸管工具,它将从我的画布上取颜色。我怎么做 代码upd: private static class DrawView extends View { ... public DrawView(Context context) { super(context); setFocusable(true); mBitmap = B

我有一块画布,在上面画线:

//see code upd
我需要制作吸管工具,它将从我的画布上取颜色。我怎么做


代码upd:

private static class DrawView extends View 
{
        ...
        public DrawView(Context context) {
            super(context);
            setFocusable(true);

            mBitmap = Bitmap.createBitmap(640, 860, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);

            this.setDrawingCacheEnabled(true);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFAAAAAA);
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            canvas.drawPath(mPath, mPaint);
        }
        private void touch_up()
        {
            if(!drBool) //is true when I click pipette button
            {
                ...
                mCanvas.drawPath(mPath, mPaint); // lines draw
                mPath.reset();
            }else{
                this.buildDrawingCache();
                cBitmap = this.getDrawingCache(true);
                if(cBitmap != null)
                {
                    int clr = cBitmap.getPixel((int)x, (int)y);
                    Log.v("pixel", Integer.toHexString(clr));
                    mPaint.setColor(clr);
                }else{
                    Log.v("pixel", "null");
                }
            }
            drBool = false;
        }
    }

我只看到“pixel”-“ffaaaaa”,或者如果我使用mCanvas.drawColor(Color.GRAY)“pixel”-“ff888888”

画布只不过是一个容器,其中包含用于操作位图的绘图调用。因此,没有“从画布上取颜色”的概念

相反,您应该检查视图位图的像素,您可以使用
getDrawingCache
获得这些像素

在视图的构造函数中:

this.setDrawingCacheEnabled(true);
当您需要像素的颜色时:

this.buildDrawingCache();
this.getDrawingCache(true).getPixel(x,y);
如果您多次调用它,这是非常低效的。在这种情况下,您可能需要添加一个位图字段,并使用getDrawingCache()在ondraw()中设置它


然后使用
bitmap.getPixel(x,y)

以上答案返回空白位图。这是我的解决方案

@Override
protected void onDraw(Canvas canvas) {
    ...
    bitmapUpdated = true;
}
然后获取位图

public Bitmap getBitmapImage() {
    if (bitmapUpdated) {
        this.buildDrawingCache();
        bitmapImage = Bitmap.createBitmap(this.getDrawingCache());
        this.destroyDrawingCache();
    }
    return bitmapImage;
}
这对我来说很好,没有过多的开销

也许更好的解决方案是重写invalidate()和onDraw(),以便它使用与位图链接的画布

Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;

此代码仅适用于drawColor,但他看不到由drawPathIt创建的颜色它必须工作!getPixel正是这样做的。它直接从位图(即从存储位图的字节数组)获取x,y处的像素,这是您在屏幕上看到的。当drawPath方法渲染到位图时,它最终会绘制像素,因此在该级别(getPixel),任何绘图调用之间都没有区别。我怀疑x和y的数学可能是错的。这很好!我试图从我绘制的位图中获取颜色,它确实返回了正确的值,但是,在应用过滤器后,它没有返回(好吧,它仍然返回原始颜色)。你的解决方案就可以了。
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;