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 
17 package com.android.inputmethod.latin.setup;
18 
19 import android.content.Context;
20 import android.graphics.Canvas;
21 import android.graphics.Paint;
22 import android.graphics.Path;
23 import android.support.v4.view.ViewCompat;
24 import android.util.AttributeSet;
25 import android.view.View;
26 
27 import com.android.inputmethod.latin.R;
28 
29 public final class SetupStepIndicatorView extends View {
30     private final Path mIndicatorPath = new Path();
31     private final Paint mIndicatorPaint = new Paint();
32     private float mXRatio;
33 
SetupStepIndicatorView(final Context context, final AttributeSet attrs)34     public SetupStepIndicatorView(final Context context, final AttributeSet attrs) {
35         super(context, attrs);
36         mIndicatorPaint.setColor(getResources().getColor(R.color.setup_step_background));
37         mIndicatorPaint.setStyle(Paint.Style.FILL);
38     }
39 
setIndicatorPosition(final int stepPos, final int totalStepNum)40     public void setIndicatorPosition(final int stepPos, final int totalStepNum) {
41         final int layoutDirection = ViewCompat.getLayoutDirection(this);
42         // The indicator position is the center of the partition that is equally divided into
43         // the total step number.
44         final float partionWidth = 1.0f / totalStepNum;
45         final float pos = stepPos * partionWidth + partionWidth / 2.0f;
46         mXRatio = (layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos;
47         invalidate();
48     }
49 
50     @Override
onDraw(final Canvas canvas)51     protected void onDraw(final Canvas canvas) {
52         super.onDraw(canvas);
53         final int xPos = (int)(getWidth() * mXRatio);
54         final int height = getHeight();
55         mIndicatorPath.rewind();
56         mIndicatorPath.moveTo(xPos, 0);
57         mIndicatorPath.lineTo(xPos + height, height);
58         mIndicatorPath.lineTo(xPos - height, height);
59         mIndicatorPath.close();
60         canvas.drawPath(mIndicatorPath, mIndicatorPaint);
61     }
62 }
63