0
尋找幫助,解答爲什麼位圖未被此圖形修改。看了很多例子,我的代碼似乎與所列出的內容相匹配。我錯過了什麼?爲什麼繪製到Android畫布不修改我的位圖?
這是相關代碼(該類在別處構造,並且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!");
}
}
}
你在哪裏調用'drawSomething()'? – CommonsWare
編輯說明用法。謝謝 – Jason
如果您確定此代碼可能會重現此問題,我認爲您應該簡單地創建一個示例應用程序並將其放到github上,以便人們可以更輕鬆地進行嘗試。 –