BulletSpan.java revision d24b8183b93e781080b2c16c487e60d51c12da31
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.os.Parcel;
22import android.text.Layout;
23import android.text.ParcelableSpan;
24import android.text.Spanned;
25import android.text.TextUtils;
26
27public class BulletSpan implements LeadingMarginSpan, ParcelableSpan {
28    private final int mGapWidth;
29    private final boolean mWantColor;
30    private final int mColor;
31
32    private static final int BULLET_RADIUS = 3;
33    public static final int STANDARD_GAP_WIDTH = 2;
34
35    public BulletSpan() {
36        mGapWidth = STANDARD_GAP_WIDTH;
37        mWantColor = false;
38        mColor = 0;
39    }
40
41    public BulletSpan(int gapWidth) {
42        mGapWidth = gapWidth;
43        mWantColor = false;
44        mColor = 0;
45    }
46
47    public BulletSpan(int gapWidth, int color) {
48        mGapWidth = gapWidth;
49        mWantColor = true;
50        mColor = color;
51    }
52
53    public BulletSpan(Parcel src) {
54        mGapWidth = src.readInt();
55        mWantColor = src.readInt() != 0;
56        mColor = src.readInt();
57    }
58
59    public int getSpanTypeId() {
60        return TextUtils.BULLET_SPAN;
61    }
62
63    public int describeContents() {
64        return 0;
65    }
66
67    public void writeToParcel(Parcel dest, int flags) {
68        dest.writeInt(mGapWidth);
69        dest.writeInt(mWantColor ? 1 : 0);
70        dest.writeInt(mColor);
71    }
72
73    public int getLeadingMargin(boolean first) {
74        return 2 * BULLET_RADIUS + mGapWidth;
75    }
76
77    public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
78                                  int top, int baseline, int bottom,
79                                  CharSequence text, int start, int end,
80                                  boolean first, Layout l) {
81        if (((Spanned) text).getSpanStart(this) == start) {
82            Paint.Style style = p.getStyle();
83            int oldcolor = 0;
84
85            if (mWantColor) {
86                oldcolor = p.getColor();
87                p.setColor(mColor);
88            }
89
90            p.setStyle(Paint.Style.FILL);
91
92            c.drawCircle(x + dir * BULLET_RADIUS, (top + bottom) / 2.0f,
93                         BULLET_RADIUS, p);
94
95            if (mWantColor) {
96                p.setColor(oldcolor);
97            }
98
99            p.setStyle(style);
100        }
101    }
102}
103