Android: Restored Canvas-based Bitmap Deteriorating?
I have a drawing app where the user can draw on the screen with their finger. The drawing happens to an off-screen bitmap, and is then posted to the screen in onDraw().
When the user is switched away from the application, via a call or by hitting home, then returns to the app, the drawing screen is shown with the previous drawing, except the drawings edges now have artifacts. Cycling through a number of home -> resume -> home -> resume cycles results in the artifacts getting worse each time. See attached images for the results after five cycles.
Has anyone seen this before? Any idea why this is happening?
开发者_如何学JAVAThanks
Original drawing:
After 5 Cycles:
EDIT: More details:
As the user touches the screen, I intercept the touches and store them as Path's on an offscreen Bitmap, mBitmap. The Paths are drawn with a Paint that has the flag Paint.ANTI_ALIAS_FLAG enabled. The in onDraw(), I write them to the screen via:
@Override
protected void onDraw(Canvas canvas) {
// wipe the canvas
canvas.drawColor(0xffffffff);
// draw the stored paths
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
// draw any active paths
if (mStrokePath != null) {
canvas.drawPath(mStrokePath, mStrokePaint);
}
}
Where mBitmapPaint is defined as:
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
EDIT 2: Ok, got it figured out
My issue was that in the onResume for the Activity that creates the Canvas-based View, I was reloading the shapes onto the canvas ON TOP of the restored Bitmap, which already had the shapes, thus the deterioration of the anti-aliasing.
I've never developed for Android before so I'm not familiar with its drawing system, but to me, your example looks like the original image has been drawn on top of itself multiple times, accentuating antialias edges. Have you any way of forcibly clearing/wiping the canvas and then redrawing when the user returns from the home screen?
Consider using Paint.FILTER_BITMAP_FLAG
.
Per the EDIT 2 section in the posted question:
My issue was that in the onResume for the Activity that creates the Canvas-based View, I was reloading the shapes onto the canvas ON TOP of the restored Bitmap, which already had the shapes, thus the deterioration of the anti-aliasing.
So, not really an issue with the restored Canvas being an issue, rather the way I implemented loading the canvas was the issue. Going to mark this and close it.
精彩评论