| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 33 |
public BiDiTestViewDrawText(Context context) { |
| 34 |
this(context, null); |
| 35 |
} |
| 36 |
|
| 37 |
public BiDiTestViewDrawText(Context context, AttributeSet attrs) { |
| 38 |
this(context, attrs, 0); |
| 39 |
} |
| 40 |
|
| 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 |
| 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 |
} |