1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.inputmethod.keyboard.internal; 18 19import android.content.res.TypedArray; 20import android.graphics.Canvas; 21import android.graphics.Paint; 22import android.graphics.Path; 23 24import com.android.inputmethod.keyboard.PointerTracker; 25import com.android.inputmethod.latin.R; 26import com.android.inputmethod.latin.common.CoordinateUtils; 27 28/** 29 * Draw rubber band preview graphics during sliding key input. 30 * 31 * @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewColor 32 * @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewWidth 33 * @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewBodyRatio 34 * @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewShadowRatio 35 */ 36public final class SlidingKeyInputDrawingPreview extends AbstractDrawingPreview { 37 private final float mPreviewBodyRadius; 38 39 private boolean mShowsSlidingKeyInputPreview; 40 private final int[] mPreviewFrom = CoordinateUtils.newInstance(); 41 private final int[] mPreviewTo = CoordinateUtils.newInstance(); 42 43 // TODO: Finalize the rubber band preview implementation. 44 private final RoundedLine mRoundedLine = new RoundedLine(); 45 private final Paint mPaint = new Paint(); 46 47 public SlidingKeyInputDrawingPreview(final TypedArray mainKeyboardViewAttr) { 48 final int previewColor = mainKeyboardViewAttr.getColor( 49 R.styleable.MainKeyboardView_slidingKeyInputPreviewColor, 0); 50 final float previewRadius = mainKeyboardViewAttr.getDimension( 51 R.styleable.MainKeyboardView_slidingKeyInputPreviewWidth, 0) / 2.0f; 52 final int PERCENTAGE_INT = 100; 53 final float previewBodyRatio = (float)mainKeyboardViewAttr.getInt( 54 R.styleable.MainKeyboardView_slidingKeyInputPreviewBodyRatio, PERCENTAGE_INT) 55 / (float)PERCENTAGE_INT; 56 mPreviewBodyRadius = previewRadius * previewBodyRatio; 57 final int previewShadowRatioInt = mainKeyboardViewAttr.getInt( 58 R.styleable.MainKeyboardView_slidingKeyInputPreviewShadowRatio, 0); 59 if (previewShadowRatioInt > 0) { 60 final float previewShadowRatio = (float)previewShadowRatioInt / (float)PERCENTAGE_INT; 61 final float shadowRadius = previewRadius * previewShadowRatio; 62 mPaint.setShadowLayer(shadowRadius, 0.0f, 0.0f, previewColor); 63 } 64 mPaint.setColor(previewColor); 65 } 66 67 @Override 68 public void onDeallocateMemory() { 69 // Nothing to do here. 70 } 71 72 public void dismissSlidingKeyInputPreview() { 73 mShowsSlidingKeyInputPreview = false; 74 invalidateDrawingView(); 75 } 76 77 /** 78 * Draws the preview 79 * @param canvas The canvas where the preview is drawn. 80 */ 81 @Override 82 public void drawPreview(final Canvas canvas) { 83 if (!isPreviewEnabled() || !mShowsSlidingKeyInputPreview) { 84 return; 85 } 86 87 // TODO: Finalize the rubber band preview implementation. 88 final float radius = mPreviewBodyRadius; 89 final Path path = mRoundedLine.makePath( 90 CoordinateUtils.x(mPreviewFrom), CoordinateUtils.y(mPreviewFrom), radius, 91 CoordinateUtils.x(mPreviewTo), CoordinateUtils.y(mPreviewTo), radius); 92 canvas.drawPath(path, mPaint); 93 } 94 95 /** 96 * Set the position of the preview. 97 * @param tracker The new location of the preview is based on the points in PointerTracker. 98 */ 99 @Override 100 public void setPreviewPosition(final PointerTracker tracker) { 101 tracker.getDownCoordinates(mPreviewFrom); 102 tracker.getLastCoordinates(mPreviewTo); 103 mShowsSlidingKeyInputPreview = true; 104 invalidateDrawingView(); 105 } 106} 107