1 /* 2 * Copyright (C) 2012 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 17 package com.android.inputmethod.keyboard.internal; 18 19 import android.content.Context; 20 import android.graphics.Canvas; 21 import android.graphics.Paint; 22 import android.graphics.PorterDuff; 23 import android.graphics.PorterDuffXfermode; 24 import android.util.AttributeSet; 25 import android.widget.RelativeLayout; 26 27 import com.android.inputmethod.latin.CollectionUtils; 28 import com.android.inputmethod.latin.CoordinateUtils; 29 30 import java.util.ArrayList; 31 32 public final class PreviewPlacerView extends RelativeLayout { 33 private final int[] mKeyboardViewOrigin = CoordinateUtils.newInstance(); 34 35 private final ArrayList<AbstractDrawingPreview> mPreviews = CollectionUtils.newArrayList(); 36 37 public PreviewPlacerView(final Context context, final AttributeSet attrs) { 38 super(context, attrs); 39 setWillNotDraw(false); 40 41 final Paint layerPaint = new Paint(); 42 layerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); 43 setLayerType(LAYER_TYPE_HARDWARE, layerPaint); 44 } 45 46 public void addPreview(final AbstractDrawingPreview preview) { 47 mPreviews.add(preview); 48 } 49 50 public void setKeyboardViewGeometry(final int[] originCoords, final int width, 51 final int height) { 52 CoordinateUtils.copy(mKeyboardViewOrigin, originCoords); 53 final int count = mPreviews.size(); 54 for (int i = 0; i < count; i++) { 55 mPreviews.get(i).setKeyboardGeometry(originCoords, width, height); 56 } 57 } 58 59 @Override 60 protected void onDetachedFromWindow() { 61 super.onDetachedFromWindow(); 62 final int count = mPreviews.size(); 63 for (int i = 0; i < count; i++) { 64 mPreviews.get(i).onDetachFromWindow(); 65 } 66 } 67 68 @Override 69 public void onDraw(final Canvas canvas) { 70 super.onDraw(canvas); 71 final int originX = CoordinateUtils.x(mKeyboardViewOrigin); 72 final int originY = CoordinateUtils.y(mKeyboardViewOrigin); 73 canvas.translate(originX, originY); 74 final int count = mPreviews.size(); 75 for (int i = 0; i < count; i++) { 76 mPreviews.get(i).drawPreview(canvas); 77 } 78 canvas.translate(-originX, -originY); 79 } 80 } 81