From cfbe8cf878f60ab2e3e25c09a91227dd85731718 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Wed, 10 Jun 2009 01:21:20 -0700 Subject: [PATCH] Fix the way gestures are rasterized to bitmaps. It was just not working with multiple strokes. --- core/java/android/gesture/Gesture.java | 29 ++++++++++++------- .../android/gesture/GestureOverlayView.java | 1 + 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/java/android/gesture/Gesture.java b/core/java/android/gesture/Gesture.java index 8e71c5744d41e..2262477a93c4f 100755 --- a/core/java/android/gesture/Gesture.java +++ b/core/java/android/gesture/Gesture.java @@ -216,16 +216,15 @@ public class Gesture implements Parcelable { * * @param width * @param height - * @param edge + * @param inset * @param color * @return the bitmap */ - public Bitmap toBitmap(int width, int height, int edge, int color) { - final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + public Bitmap toBitmap(int width, int height, int inset, int color) { + final Bitmap bitmap = Bitmap.createBitmap(width, height, + Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); - canvas.translate(edge, edge); - final Paint paint = new Paint(); paint.setAntiAlias(BITMAP_RENDERING_ANTIALIAS); paint.setDither(BITMAP_RENDERING_DITHER); @@ -235,12 +234,22 @@ public class Gesture implements Parcelable { paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(BITMAP_RENDERING_WIDTH); - final ArrayList strokes = mStrokes; - final int count = strokes.size(); + final Path path = toPath(); + final RectF bounds = new RectF(); + path.computeBounds(bounds, true); - for (int i = 0; i < count; i++) { - strokes.get(i).draw(canvas, paint); - } + final float sx = (width - 2 * inset) / bounds.width(); + final float sy = (height - 2 * inset) / bounds.height(); + final float scale = sx > sy ? sy : sx; + paint.setStrokeWidth(2.0f / scale); + + path.offset(-bounds.left + (width - bounds.width() * scale) / 2.0f, + -bounds.top + (height - bounds.height() * scale) / 2.0f); + + canvas.translate(inset, inset); + canvas.scale(scale, scale); + + canvas.drawPath(path, paint); return bitmap; } diff --git a/core/java/android/gesture/GestureOverlayView.java b/core/java/android/gesture/GestureOverlayView.java index 1e81780898329..fd6d850d9abe3 100755 --- a/core/java/android/gesture/GestureOverlayView.java +++ b/core/java/android/gesture/GestureOverlayView.java @@ -294,6 +294,7 @@ public class GestureOverlayView extends FrameLayout { final RectF bounds = new RectF(); path.computeBounds(bounds, true); + // TODO: The path should also be scaled to fit inside this view mPath.rewind(); mPath.addPath(path, -bounds.left + (getWidth() - bounds.width()) / 2.0f, -bounds.top + (getHeight() - bounds.height()) / 2.0f);