1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.text.style;
import static java.lang.annotation.RetentionPolicy.SOURCE;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import java.lang.annotation.Retention;
import java.lang.ref.WeakReference;
/**
* Span that replaces the text it's attached to with a {@link Drawable} that can be aligned with
* the bottom or with the baseline of the surrounding text.
* <p>
* For an implementation that constructs the drawable from various sources (<code>Bitmap</code>,
* <code>Drawable</code>, resource id or <code>Uri</code>) use {@link ImageSpan}.
* <p>
* A simple implementation of <code>DynamicDrawableSpan</code> that uses drawables from resources
* looks like this:
* <pre>
* class MyDynamicDrawableSpan extends DynamicDrawableSpan {
*
* private final Context mContext;
* private final int mResourceId;
*
* public MyDynamicDrawableSpan(Context context, @DrawableRes int resourceId) {
* mContext = context;
* mResourceId = resourceId;
* }
*
* {@literal @}Override
* public Drawable getDrawable() {
* Drawable drawable = mContext.getDrawable(mResourceId);
* drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
* return drawable;
* }
* }</pre>
* The class can be used like this:
* <pre>
* SpannableString string = new SpannableString("Text with a drawable span");
* string.setSpan(new MyDynamicDrawableSpan(context, R.mipmap.ic_launcher), 12, 20, Spanned
* .SPAN_EXCLUSIVE_EXCLUSIVE);</pre>
* <img src="{@docRoot}reference/android/images/text/style/dynamicdrawablespan.png" />
* <figcaption>Replacing text with a drawable.</figcaption>
*/
public abstract class DynamicDrawableSpan extends ReplacementSpan {
/**
* A constant indicating that the bottom of this span should be aligned
* with the bottom of the surrounding text, i.e., at the same level as the
* lowest descender in the text.
*/
public static final int ALIGN_BOTTOM = 0;
/**
* A constant indicating that the bottom of this span should be aligned
* with the baseline of the surrounding text.
*/
public static final int ALIGN_BASELINE = 1;
/**
* A constant indicating that this span should be vertically centered between
* the top and the lowest descender.
*/
public static final int ALIGN_CENTER = 2;
/**
* Defines acceptable alignment types.
* @hide
*/
@Retention(SOURCE)
@IntDef(prefix = { "ALIGN_" }, value = {
ALIGN_BOTTOM,
ALIGN_BASELINE,
ALIGN_CENTER
})
public @interface AlignmentType {}
protected final int mVerticalAlignment;
@UnsupportedAppUsage
private WeakReference<Drawable> mDrawableRef;
/**
* Creates a {@link DynamicDrawableSpan}. The default vertical alignment is
* {@link #ALIGN_BOTTOM}
*/
public DynamicDrawableSpan() {
mVerticalAlignment = ALIGN_BOTTOM;
}
/**
* Creates a {@link DynamicDrawableSpan} based on a vertical alignment.\
*
* @param verticalAlignment one of {@link #ALIGN_BOTTOM}, {@link #ALIGN_BASELINE} or
* {@link #ALIGN_CENTER}
*/
protected DynamicDrawableSpan(@AlignmentType int verticalAlignment) {
mVerticalAlignment = verticalAlignment;
}
/**
* Returns the vertical alignment of this span, one of {@link #ALIGN_BOTTOM},
* {@link #ALIGN_BASELINE} or {@link #ALIGN_CENTER}.
*/
public @AlignmentType int getVerticalAlignment() {
return mVerticalAlignment;
}
/**
* Your subclass must implement this method to provide the bitmap
* to be drawn. The dimensions of the bitmap must be the same
* from each call to the next.
*/
public abstract Drawable getDrawable();
@Override
public int getSize(@NonNull Paint paint, CharSequence text,
@IntRange(from = 0) int start, @IntRange(from = 0) int end,
@Nullable Paint.FontMetricsInt fm) {
Drawable d = getCachedDrawable();
Rect rect = d.getBounds();
if (fm != null) {
fm.ascent = -rect.bottom;
fm.descent = 0;
fm.top = fm.ascent;
fm.bottom = 0;
}
return rect.right;
}
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
@IntRange(from = 0) int start, @IntRange(from = 0) int end, float x,
int top, int y, int bottom, @NonNull Paint paint) {
Drawable b = getCachedDrawable();
canvas.save();
int transY = bottom - b.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= paint.getFontMetricsInt().descent;
} else if (mVerticalAlignment == ALIGN_CENTER) {
transY = (bottom - top) / 2 - b.getBounds().height() / 2;
}
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
private Drawable getCachedDrawable() {
WeakReference<Drawable> wr = mDrawableRef;
Drawable d = null;
if (wr != null) {
d = wr.get();
}
if (d == null) {
d = getDrawable();
mDrawableRef = new WeakReference<Drawable>(d);
}
return d;
}
}
|