1 /*
2  * Copyright (C) 2011 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.bidi;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint.Align;
24 import android.text.TextPaint;
25 import android.util.AttributeSet;
26 import android.view.View;
27 
28 public class BiDiTestViewDrawText extends View {
29     private float mSize;
30     private int mColor;
31     private String mText;
32 
BiDiTestViewDrawText(Context context)33     public BiDiTestViewDrawText(Context context) {
34         this(context, null);
35     }
36 
BiDiTestViewDrawText(Context context, AttributeSet attrs)37     public BiDiTestViewDrawText(Context context, AttributeSet attrs) {
38         this(context, attrs, 0);
39     }
40 
BiDiTestViewDrawText(Context context, AttributeSet attrs, int defStyle)41     public BiDiTestViewDrawText(Context context, AttributeSet attrs, int defStyle) {
42         super(context, attrs, defStyle);
43 
44         final TypedArray a = context.obtainStyledAttributes(attrs,
45                 R.styleable.DrawTextTestView, defStyle, 0);
46         mSize = a.getDimension(R.styleable.DrawTextTestView_size, 40.0f);
47         mColor = a.getColor(R.styleable.DrawTextTestView_color, Color.YELLOW);
48         final CharSequence text = a.getText(R.styleable.DrawTextTestView_text);
49         mText = (text != null) ? text.toString() : "(empty)";
50         a.recycle();
51     }
52 
53     @Override
onDraw(Canvas canvas)54     protected void onDraw(Canvas canvas) {
55         super.onDraw(canvas);
56         final int width = getWidth();
57         final int height = getHeight();
58 
59         final TextPaint paint = new TextPaint();
60         paint.setTextSize(mSize);
61         paint.setColor(mColor);
62         paint.setTextAlign(Align.CENTER);
63 
64         canvas.drawText(mText, width / 2, height * 2 / 3, paint);
65     }
66 }