BulletSpan.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
1/*
2 * Copyright (C) 2006 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
17package android.text.style;
18
19import android.graphics.Canvas;
20import android.graphics.Paint;
21import android.text.Layout;
22import android.text.Spanned;
23
24public class BulletSpan implements LeadingMarginSpan {
25
26    public BulletSpan() {
27        mGapWidth = STANDARD_GAP_WIDTH;
28    }
29
30    public BulletSpan(int gapWidth) {
31        mGapWidth = gapWidth;
32    }
33
34    public BulletSpan(int gapWidth, int color) {
35        mGapWidth = gapWidth;
36        mWantColor = true;
37        mColor = color;
38    }
39
40    public int getLeadingMargin(boolean first) {
41        return 2 * BULLET_RADIUS + mGapWidth;
42    }
43
44    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
45                                  int top, int baseline, int bottom,
46                                  CharSequence text, int start, int end,
47                                  boolean first, Layout l) {
48        if (((Spanned) text).getSpanStart(this) == start) {
49            Paint.Style style = p.getStyle();
50            int oldcolor = 0;
51
52            if (mWantColor) {
53                oldcolor = p.getColor();
54                p.setColor(mColor);
55            }
56
57            p.setStyle(Paint.Style.FILL);
58
59            c.drawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f,
60                         BULLET_RADIUS, p);
61
62            if (mWantColor) {
63                p.setColor(oldcolor);
64            }
65
66            p.setStyle(style);
67        }
68    }
69
70    private int mGapWidth;
71    private boolean mWantColor;
72    private int mColor;
73
74    private static final int BULLET_RADIUS = 3;
75    public static final int STANDARD_GAP_WIDTH = 2;
76}
77