Skip to content
Snippets Groups Projects
Commit ffe36cdc authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Small cleanup for keyboard drawing code" into jb-mr1-dev

parents 76c242a1 8344259f
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,7 @@ import android.graphics.Paint; ...@@ -25,7 +25,7 @@ import android.graphics.Paint;
import android.graphics.Paint.Align; import android.graphics.Paint.Align;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.Region.Op; import android.graphics.Region;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Message; import android.os.Message;
...@@ -120,12 +120,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { ...@@ -120,12 +120,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
private boolean mInvalidateAllKeys; private boolean mInvalidateAllKeys;
/** The keys that should be drawn */ /** The keys that should be drawn */
private final HashSet<Key> mInvalidatedKeys = new HashSet<Key>(); private final HashSet<Key> mInvalidatedKeys = new HashSet<Key>();
/** The region of invalidated keys */ /** The working rectangle variable */
private final Rect mInvalidatedKeysRect = new Rect(); private final Rect mWorkingRect = new Rect();
/** The keyboard bitmap buffer for faster updates */ /** The keyboard bitmap buffer for faster updates */
private Bitmap mBuffer; private Bitmap mOffscreenBuffer;
/** The canvas for the above mutable keyboard bitmap */ /** The canvas for the above mutable keyboard bitmap */
private Canvas mCanvas; private Canvas mOffscreenCanvas;
private final Paint mPaint = new Paint(); private final Paint mPaint = new Paint();
private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics(); private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();
// This sparse array caches key label text height in pixel indexed by key label text size. // This sparse array caches key label text height in pixel indexed by key label text size.
...@@ -457,46 +457,61 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { ...@@ -457,46 +457,61 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
@Override @Override
public void onDraw(Canvas canvas) { public void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
if (mBufferNeedsUpdate || mBuffer == null) { if (mBufferNeedsUpdate || mOffscreenBuffer == null) {
mBufferNeedsUpdate = false; mBufferNeedsUpdate = false;
onBufferDraw(); if (maybeAllocateOffscreenBuffer()) {
mInvalidateAllKeys = true;
if (mOffscreenCanvas != null) {
mOffscreenCanvas.setBitmap(mOffscreenBuffer);
} else {
mOffscreenCanvas = new Canvas(mOffscreenBuffer);
}
}
onDrawKeyboard(mOffscreenCanvas);
} }
canvas.drawBitmap(mBuffer, 0, 0, null); canvas.drawBitmap(mOffscreenBuffer, 0, 0, null);
} }
private void onBufferDraw() { private boolean maybeAllocateOffscreenBuffer() {
final int width = getWidth(); final int width = getWidth();
final int height = getHeight(); final int height = getHeight();
if (width == 0 || height == 0) if (width == 0 || height == 0) {
return; return false;
if (mBuffer == null || mBuffer.getWidth() != width || mBuffer.getHeight() != height) { }
if (mBuffer != null) if (mOffscreenBuffer != null && mOffscreenBuffer.getWidth() == width
mBuffer.recycle(); && mOffscreenBuffer.getHeight() == height) {
mBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); return false;
mInvalidateAllKeys = true;
if (mCanvas != null) {
mCanvas.setBitmap(mBuffer);
} else {
mCanvas = new Canvas(mBuffer);
}
} }
freeOffscreenBuffer();
mOffscreenBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
return true;
}
private void freeOffscreenBuffer() {
if (mOffscreenBuffer != null) {
mOffscreenBuffer.recycle();
mOffscreenBuffer = null;
}
}
private void onDrawKeyboard(final Canvas canvas) {
if (mKeyboard == null) return; if (mKeyboard == null) return;
final Canvas canvas = mCanvas; final int width = getWidth();
final int height = getHeight();
final Paint paint = mPaint; final Paint paint = mPaint;
final KeyDrawParams params = mKeyDrawParams; final KeyDrawParams params = mKeyDrawParams;
if (mInvalidateAllKeys || mInvalidatedKeys.isEmpty()) { if (mInvalidateAllKeys || mInvalidatedKeys.isEmpty()) {
mInvalidatedKeysRect.set(0, 0, width, height); mWorkingRect.set(0, 0, width, height);
canvas.clipRect(mInvalidatedKeysRect, Op.REPLACE); canvas.clipRect(mWorkingRect, Region.Op.REPLACE);
canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR); canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
// Draw all keys. // Draw all keys.
for (final Key key : mKeyboard.mKeys) { for (final Key key : mKeyboard.mKeys) {
onDrawKey(key, canvas, paint, params); onDrawKey(key, canvas, paint, params);
} }
if (mNeedsToDimEntireKeyboard) { if (mNeedsToDimEntireKeyboard) {
drawDimRectangle(canvas, mInvalidatedKeysRect, mBackgroundDimAlpha, paint); drawDimRectangle(canvas, mWorkingRect, mBackgroundDimAlpha, paint);
} }
} else { } else {
// Draw invalidated keys. // Draw invalidated keys.
...@@ -506,12 +521,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { ...@@ -506,12 +521,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
final int x = key.mX + getPaddingLeft(); final int x = key.mX + getPaddingLeft();
final int y = key.mY + getPaddingTop(); final int y = key.mY + getPaddingTop();
mInvalidatedKeysRect.set(x, y, x + key.mWidth, y + key.mHeight); mWorkingRect.set(x, y, x + key.mWidth, y + key.mHeight);
canvas.clipRect(mInvalidatedKeysRect, Op.REPLACE); canvas.clipRect(mWorkingRect, Region.Op.REPLACE);
canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR); canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
onDrawKey(key, canvas, paint, params); onDrawKey(key, canvas, paint, params);
if (mNeedsToDimEntireKeyboard) { if (mNeedsToDimEntireKeyboard) {
drawDimRectangle(canvas, mInvalidatedKeysRect, mBackgroundDimAlpha, paint); drawDimRectangle(canvas, mWorkingRect, mBackgroundDimAlpha, paint);
} }
} }
} }
...@@ -524,7 +539,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { ...@@ -524,7 +539,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
mInvalidatedKeys.clear(); mInvalidatedKeys.clear();
mInvalidatedKeysRect.setEmpty();
mInvalidateAllKeys = false; mInvalidateAllKeys = false;
} }
...@@ -1026,9 +1040,9 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { ...@@ -1026,9 +1040,9 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
mInvalidatedKeys.add(key); mInvalidatedKeys.add(key);
final int x = key.mX + getPaddingLeft(); final int x = key.mX + getPaddingLeft();
final int y = key.mY + getPaddingTop(); final int y = key.mY + getPaddingTop();
mInvalidatedKeysRect.union(x, y, x + key.mWidth, y + key.mHeight); mWorkingRect.set(x, y, x + key.mWidth, y + key.mHeight);
mBufferNeedsUpdate = true; mBufferNeedsUpdate = true;
invalidate(mInvalidatedKeysRect); invalidate(mWorkingRect);
} }
public void closing() { public void closing() {
...@@ -1054,9 +1068,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { ...@@ -1054,9 +1068,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
super.onDetachedFromWindow(); super.onDetachedFromWindow();
closing(); closing();
mPreviewPlacerView.removeAllViews(); mPreviewPlacerView.removeAllViews();
if (mBuffer != null) { freeOffscreenBuffer();
mBuffer.recycle();
mBuffer = null;
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment