2

我有这样的代码:

// ...

public class MyImageView extends ImageView

    public MyImageView(Context context, String value /* some other params */) {
        super(context);

        // some predefines
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // some preparations
        try {
            // here I call third party lib like:
            someObj.draw(canvas);

            // HERE I WANT TO CHANGE COLOR OF SOME PIXELS ACCORDING TO THEIR CURRENT COLOR
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

代替 UPCASE 字母注释,我想在第三方库绘制后根据它们当前的颜色更改画布上某些像素的颜色。我可以使用canvas.drawPoint(x, y, paint)设置像素颜色的方法,但我怎样才能获得某些像素的颜色(x,y)

4

1 回答 1

1

我可能在这里有一些东西(未经测试):

public static Bitmap getBitmap(Canvas canvas) {
    // mBitmap is a private value inside Canvas.
    // time for some dirty reflection:
    try {
        java.lang.reflect.Field field = Canvas.class.getDeclaredField("mBitmap");
        field.setAccessible(true);
        return (Bitmap)field.get(canvas);
    }
    catch (Throwable t) {
        return null;
    }
}

然后,您可以通过以下方式访问像素:

Bitmap map = getBitmap(canvas);
if (map != null) {
    int rgb = map.getPixel(100,100);
    int red = android.graphics.Color.red(rgb);
    ...
}
于 2014-03-19T14:34:08.673 回答