如何获得画布像素 [英] How to get canvas pixel

查看:98
本文介绍了如何获得画布像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我画线画布:

//see code upd

我需要做的吸管工具,将颜色从我的画布。我怎么可能做到吗?

I need to make the pipette tool which will take color from my canvas. How may I make it?

code 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;
        }
    }

我只看到像素 - ffaaaaaa,或者,如果我使用mCanvas.drawColor(Color.GRAY)像素 - ff888888

I see only "pixel"-"ffaaaaaa", or if I use mCanvas.drawColor(Color.GRAY) "pixel"-"ff888888"

推荐答案

画布无非是一种容器,其持有绘图调用来操作位图。所以不存在从画布上以色彩的概念。

A canvas is nothing more than a container which holds drawing calls to manipulate a bitmap. So there is no concept of "taking colour from a canvas".

相反,你应该检查的观点,你可以用 getDrawingCache 得到的位图的像素。

Instead, you should examine the pixels of the bitmap of the view, which you can get with getDrawingCache.

在你的观点的构造函数:

In your views' constructor:

this.setDrawingCacheEnabled(true);

当你想要一个像素的颜色:

When you want the colour of a pixel:

this.buildDrawingCache();
this.getDrawingCache(true).getPixel(x,y);

这是非常低效的,如果你调用了很多次,在这种情况下,你可能需要添加一个位图字段并使用getDrawingCache()来设置它的OnDraw()。

This is very inefficient if you are calling it many times in which case, you might want to add a bitmap field and use getDrawingCache() to set it in ondraw().

private Bitmap bitmap;

...

onDraw()

  ...

  bitmap = this.getDrawingCache(true);

然后使用 bitmap.getPixel(X,Y);

这篇关于如何获得画布像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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