为什么要绘制Android画布而不修改我的位图?(Why is drawing to the Android canvas not modifying my bitmap?)

寻找帮助,以解决为什么此图形未修改位图的原因。 看了很多例子,我的代码似乎与列出的内容相匹配。 我错过了什么?

这是相关的代码(该类在其他地方构建, drawSomething()调用在onTouchEvent处理程序中)。 为简洁起见,我缩短了代码:

class MyView extends View { Bitmap mBitmap; BitmapDrawable mBitmapDrawable; Canvas mCanvas; Paint mPaint; public MyView(Context context) { super(context); mBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); mBitmapDrawable = new BitmapDrawable(getResources(), mBitmap); mCanvas = new Canvas(mBitmap); mCanvas.setBitmap(mBitmap); mPaint = new Paint(); mPaint.setColor(Color.parseColor("#00FF00")); mPaint.setStrokeWidth(10); } public void drawSomething() { mCanvas.drawColor(0xFF00FF00); // This should "fill" the canvas int radius = 10; mCanvas.drawCircle(50, 50, radius, mPaint); // This should draw a circle at (50, 50) int count=0; for (int i=0; i<mBitmap.getWidth(); i++) { for (int j=0; j<mBitmap.getHeight(); j++) { if (mBitmap.getPixel(i, j) > 0) { count += 1; } } } if (count == 0) { Log.v("MyApp", "Nothing was drawn!"); } } }

Looking for help troubleshooting why the bitmap is not being modified by this drawing. Have looked at numerous examples, and my code seems to match what is listed. What am I missing?

This is the relevant code (the class is constructed elsewhere, and the drawSomething() call is in an onTouchEvent handler). I've shortened the code for brevity:

class MyView extends View { Bitmap mBitmap; BitmapDrawable mBitmapDrawable; Canvas mCanvas; Paint mPaint; public MyView(Context context) { super(context); mBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); mBitmapDrawable = new BitmapDrawable(getResources(), mBitmap); mCanvas = new Canvas(mBitmap); mCanvas.setBitmap(mBitmap); mPaint = new Paint(); mPaint.setColor(Color.parseColor("#00FF00")); mPaint.setStrokeWidth(10); } public void drawSomething() { mCanvas.drawColor(0xFF00FF00); // This should "fill" the canvas int radius = 10; mCanvas.drawCircle(50, 50, radius, mPaint); // This should draw a circle at (50, 50) int count=0; for (int i=0; i<mBitmap.getWidth(); i++) { for (int j=0; j<mBitmap.getHeight(); j++) { if (mBitmap.getPixel(i, j) > 0) { count += 1; } } } if (count == 0) { Log.v("MyApp", "Nothing was drawn!"); } } }

最满意答案

找出问题所在。 事实证明画布绘图正如预期的那样工作(毫不奇怪)。 还有另外两个问题。

我忽略了Java int数据类型的签名。 int值0xFF00FF00计算结果小于0.这就解释了为什么我的故障诊断代码中的计数评估为0。

我没有在位图中看到任何绘图,因为我最终使用mBitmapDrawable对象的draw方法将位图绘制到屏幕上。 更改我的代码以使用Canvas对象的drawBitmap方法后,正确绘制了可变位图。 我推测BitmapDrawable在其构造函数中复制了提供的位图,因此我没有看到我对位图所做的修改。

Figured out the problem. Turns out that the canvas drawing was working as expected (no surprise there). There were two other issues.

I overlooked the signed-ness of a Java int datatype. The int value 0xFF00FF00 evaluates less than 0. This explains why the count in my troubleshooting code evaluated to 0.

I was not seeing any drawing in the bitmap because I was ultimately using the draw method of the mBitmapDrawable object to draw the bitmap to the screen. After changing my code to use the drawBitmap method of the Canvas object, the mutable bitmap was properly drawn. I speculate that the BitmapDrawable makes a copy of the provided bitmap in its constructor, and therefore I was not seeing the modifications I had made to the bitmap.

为什么要绘制Android画布而不修改我的位图?(Why is drawing to the Android canvas not modifying my bitmap?)

寻找帮助,以解决为什么此图形未修改位图的原因。 看了很多例子,我的代码似乎与列出的内容相匹配。 我错过了什么?

这是相关的代码(该类在其他地方构建, drawSomething()调用在onTouchEvent处理程序中)。 为简洁起见,我缩短了代码:

class MyView extends View { Bitmap mBitmap; BitmapDrawable mBitmapDrawable; Canvas mCanvas; Paint mPaint; public MyView(Context context) { super(context); mBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); mBitmapDrawable = new BitmapDrawable(getResources(), mBitmap); mCanvas = new Canvas(mBitmap); mCanvas.setBitmap(mBitmap); mPaint = new Paint(); mPaint.setColor(Color.parseColor("#00FF00")); mPaint.setStrokeWidth(10); } public void drawSomething() { mCanvas.drawColor(0xFF00FF00); // This should "fill" the canvas int radius = 10; mCanvas.drawCircle(50, 50, radius, mPaint); // This should draw a circle at (50, 50) int count=0; for (int i=0; i<mBitmap.getWidth(); i++) { for (int j=0; j<mBitmap.getHeight(); j++) { if (mBitmap.getPixel(i, j) > 0) { count += 1; } } } if (count == 0) { Log.v("MyApp", "Nothing was drawn!"); } } }

Looking for help troubleshooting why the bitmap is not being modified by this drawing. Have looked at numerous examples, and my code seems to match what is listed. What am I missing?

This is the relevant code (the class is constructed elsewhere, and the drawSomething() call is in an onTouchEvent handler). I've shortened the code for brevity:

class MyView extends View { Bitmap mBitmap; BitmapDrawable mBitmapDrawable; Canvas mCanvas; Paint mPaint; public MyView(Context context) { super(context); mBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888); mBitmapDrawable = new BitmapDrawable(getResources(), mBitmap); mCanvas = new Canvas(mBitmap); mCanvas.setBitmap(mBitmap); mPaint = new Paint(); mPaint.setColor(Color.parseColor("#00FF00")); mPaint.setStrokeWidth(10); } public void drawSomething() { mCanvas.drawColor(0xFF00FF00); // This should "fill" the canvas int radius = 10; mCanvas.drawCircle(50, 50, radius, mPaint); // This should draw a circle at (50, 50) int count=0; for (int i=0; i<mBitmap.getWidth(); i++) { for (int j=0; j<mBitmap.getHeight(); j++) { if (mBitmap.getPixel(i, j) > 0) { count += 1; } } } if (count == 0) { Log.v("MyApp", "Nothing was drawn!"); } } }

最满意答案

找出问题所在。 事实证明画布绘图正如预期的那样工作(毫不奇怪)。 还有另外两个问题。

我忽略了Java int数据类型的签名。 int值0xFF00FF00计算结果小于0.这就解释了为什么我的故障诊断代码中的计数评估为0。

我没有在位图中看到任何绘图,因为我最终使用mBitmapDrawable对象的draw方法将位图绘制到屏幕上。 更改我的代码以使用Canvas对象的drawBitmap方法后,正确绘制了可变位图。 我推测BitmapDrawable在其构造函数中复制了提供的位图,因此我没有看到我对位图所做的修改。

Figured out the problem. Turns out that the canvas drawing was working as expected (no surprise there). There were two other issues.

I overlooked the signed-ness of a Java int datatype. The int value 0xFF00FF00 evaluates less than 0. This explains why the count in my troubleshooting code evaluated to 0.

I was not seeing any drawing in the bitmap because I was ultimately using the draw method of the mBitmapDrawable object to draw the bitmap to the screen. After changing my code to use the drawBitmap method of the Canvas object, the mutable bitmap was properly drawn. I speculate that the BitmapDrawable makes a copy of the provided bitmap in its constructor, and therefore I was not seeing the modifications I had made to the bitmap.