• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.utils.CollectionUtils;
28 import com.android.inputmethod.latin.utils.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 
PreviewPlacerView(final Context context, final AttributeSet attrs)37     public PreviewPlacerView(final Context context, final AttributeSet attrs) {
38         super(context, attrs);
39         setWillNotDraw(false);
40     }
41 
setHardwareAcceleratedDrawingEnabled(final boolean enabled)42     public void setHardwareAcceleratedDrawingEnabled(final boolean enabled) {
43         if (!enabled) return;
44         final Paint layerPaint = new Paint();
45         layerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
46         setLayerType(LAYER_TYPE_HARDWARE, layerPaint);
47     }
48 
addPreview(final AbstractDrawingPreview preview)49     public void addPreview(final AbstractDrawingPreview preview) {
50         mPreviews.add(preview);
51     }
52 
setKeyboardViewGeometry(final int[] originCoords, final int width, final int height)53     public void setKeyboardViewGeometry(final int[] originCoords, final int width,
54             final int height) {
55         CoordinateUtils.copy(mKeyboardViewOrigin, originCoords);
56         final int count = mPreviews.size();
57         for (int i = 0; i < count; i++) {
58             mPreviews.get(i).setKeyboardGeometry(originCoords, width, height);
59         }
60     }
61 
62     @Override
onDetachedFromWindow()63     protected void onDetachedFromWindow() {
64         super.onDetachedFromWindow();
65         final int count = mPreviews.size();
66         for (int i = 0; i < count; i++) {
67             mPreviews.get(i).onDetachFromWindow();
68         }
69     }
70 
71     @Override
onDraw(final Canvas canvas)72     public void onDraw(final Canvas canvas) {
73         super.onDraw(canvas);
74         final int originX = CoordinateUtils.x(mKeyboardViewOrigin);
75         final int originY = CoordinateUtils.y(mKeyboardViewOrigin);
76         canvas.translate(originX, originY);
77         final int count = mPreviews.size();
78         for (int i = 0; i < count; i++) {
79             mPreviews.get(i).drawPreview(canvas);
80         }
81         canvas.translate(-originX, -originY);
82     }
83 }
84