我有这样的代码:
// ...
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字母注释我想根据第三方lib绘制后的当前颜色更改画布上某些像素的颜色。我可以使用canvas.drawPoint(x, y, paint)
方法设置像素颜色,但如何通过(x,y)
获取某些像素的颜色?
答案 0 :(得分: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);
...
}