| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.style; |
| 18 |
|
| 19 |
import android.annotation.ColorInt; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.Px; |
| 24 |
import android.compat.annotation.UnsupportedAppUsage; |
| 25 |
import android.graphics.Canvas; |
| 26 |
import android.graphics.Paint; |
| 27 |
import android.os.Build; |
| 28 |
import android.os.Parcel; |
| 29 |
import android.text.Layout; |
| 30 |
import android.text.ParcelableSpan; |
| 31 |
import android.text.Spanned; |
| 32 |
import android.text.TextUtils; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public class BulletSpan implements LeadingMarginSpan, ParcelableSpan { |
| 67 |
|
| 68 |
private static final int STANDARD_BULLET_RADIUS = 4; |
| 69 |
public static final int STANDARD_GAP_WIDTH = 2; |
| 70 |
private static final int STANDARD_COLOR = 0; |
| 71 |
|
| 72 |
@Px |
| 73 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 74 |
private final int mGapWidth; |
| 75 |
@Px |
| 76 |
private final int mBulletRadius; |
| 77 |
@ColorInt |
| 78 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 79 |
private final int mColor; |
| 80 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 81 |
private final boolean mWantColor; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public BulletSpan() { |
| 87 |
this(STANDARD_GAP_WIDTH, STANDARD_COLOR, false, STANDARD_BULLET_RADIUS); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public BulletSpan(int gapWidth) { |
| 96 |
this(gapWidth, STANDARD_COLOR, false, STANDARD_BULLET_RADIUS); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public BulletSpan(int gapWidth, @ColorInt int color) { |
| 107 |
this(gapWidth, color, true, STANDARD_BULLET_RADIUS); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public BulletSpan(int gapWidth, @ColorInt int color, @IntRange(from = 0) int bulletRadius) { |
| 119 |
this(gapWidth, color, true, bulletRadius); |
| 120 |
} |
| 121 |
|
| 122 |
private BulletSpan(int gapWidth, @ColorInt int color, boolean wantColor, |
| 123 |
@IntRange(from = 0) int bulletRadius) { |
| 124 |
mGapWidth = gapWidth; |
| 125 |
mBulletRadius = bulletRadius; |
| 126 |
mColor = color; |
| 127 |
mWantColor = wantColor; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public BulletSpan(@NonNull Parcel src) { |
| 134 |
mGapWidth = src.readInt(); |
| 135 |
mWantColor = src.readInt() != 0; |
| 136 |
mColor = src.readInt(); |
| 137 |
mBulletRadius = src.readInt(); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public int getSpanTypeId() { |
| 142 |
return getSpanTypeIdInternal(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
@Override |
| 147 |
public int getSpanTypeIdInternal() { |
| 148 |
return TextUtils.BULLET_SPAN; |
| 149 |
} |
| 150 |
|
| 151 |
@Override |
| 152 |
public int describeContents() { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 158 |
writeToParcelInternal(dest, flags); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
@Override |
| 163 |
public void writeToParcelInternal(@NonNull Parcel dest, int flags) { |
| 164 |
dest.writeInt(mGapWidth); |
| 165 |
dest.writeInt(mWantColor ? 1 : 0); |
| 166 |
dest.writeInt(mColor); |
| 167 |
dest.writeInt(mBulletRadius); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public int getLeadingMargin(boolean first) { |
| 172 |
return 2 * mBulletRadius + mGapWidth; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public int getGapWidth() { |
| 181 |
return mGapWidth; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public int getBulletRadius() { |
| 190 |
return mBulletRadius; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public int getColor() { |
| 199 |
return mColor; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void drawLeadingMargin(@NonNull Canvas canvas, @NonNull Paint paint, int x, int dir, |
| 204 |
int top, int baseline, int bottom, |
| 205 |
@NonNull CharSequence text, int start, int end, |
| 206 |
boolean first, @Nullable Layout layout) { |
| 207 |
if (((Spanned) text).getSpanStart(this) == start) { |
| 208 |
Paint.Style style = paint.getStyle(); |
| 209 |
int oldcolor = 0; |
| 210 |
|
| 211 |
if (mWantColor) { |
| 212 |
oldcolor = paint.getColor(); |
| 213 |
paint.setColor(mColor); |
| 214 |
} |
| 215 |
|
| 216 |
paint.setStyle(Paint.Style.FILL); |
| 217 |
|
| 218 |
if (layout != null) { |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
final int line = layout.getLineForOffset(start); |
| 223 |
bottom = bottom - layout.getLineExtra(line); |
| 224 |
} |
| 225 |
|
| 226 |
final float yPosition = (top + bottom) / 2f; |
| 227 |
final float xPosition = x + dir * mBulletRadius; |
| 228 |
|
| 229 |
canvas.drawCircle(xPosition, yPosition, mBulletRadius, paint); |
| 230 |
|
| 231 |
if (mWantColor) { |
| 232 |
paint.setColor(oldcolor); |
| 233 |
} |
| 234 |
|
| 235 |
paint.setStyle(style); |
| 236 |
} |
| 237 |
} |
| 238 |
} |