Merge "Add default implementation for LineBackgroundSpan"

This commit is contained in:
Haoyu Zhang
2018-10-16 00:31:40 +00:00
committed by Android (Google) Code Review
3 changed files with 120 additions and 8 deletions

View File

@@ -42,6 +42,7 @@ import android.text.style.CharacterStyle;
import android.text.style.EasyEditSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.LeadingMarginSpan;
import android.text.style.LineBackgroundSpan;
import android.text.style.LocaleSpan;
import android.text.style.ParagraphStyle;
import android.text.style.QuoteSpan;
@@ -687,7 +688,9 @@ public class TextUtils {
/** @hide */
public static final int ACCESSIBILITY_URL_SPAN = 26;
/** @hide */
public static final int LAST_SPAN = ACCESSIBILITY_URL_SPAN;
public static final int LINE_BACKGROUND_SPAN = 27;
/** @hide */
public static final int LAST_SPAN = LINE_BACKGROUND_SPAN;
/**
* Flatten a CharSequence and whatever styles can be copied across processes
@@ -878,6 +881,10 @@ public class TextUtils {
readSpan(p, sp, new AccessibilityURLSpan(p));
break;
case LINE_BACKGROUND_SPAN:
readSpan(p, sp, new LineBackgroundSpan.Standard(p));
break;
default:
throw new RuntimeException("bogus span encoding " + kind);
}

View File

@@ -16,15 +16,110 @@
package android.text.style;
import android.annotation.ColorInt;
import android.annotation.NonNull;
import android.annotation.Px;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.text.ParcelableSpan;
import android.text.TextUtils;
public interface LineBackgroundSpan
extends ParagraphStyle
/**
* Used to change the background of lines where the span is attached to.
*/
public interface LineBackgroundSpan extends ParagraphStyle
{
public void drawBackground(Canvas c, Paint p,
int left, int right,
int top, int baseline, int bottom,
CharSequence text, int start, int end,
int lnum);
/**
* Draw the background on the canvas.
*
* @param canvas canvas on which the span should be rendered
* @param paint paint used to draw text, which should be left unchanged on exit
* @param left left position of the line relative to input canvas, in pixels
* @param right right position of the line relative to input canvas, in pixels
* @param top top position of the line relative to input canvas, in pixels
* @param baseline baseline of the text relative to input canvas, in pixels
* @param bottom bottom position of the line relative to input canvas, in pixels
* @param text current text
* @param start start character index of the line
* @param end end character index of the line
* @param lineNumber line number in the current text layout
*/
void drawBackground(@NonNull Canvas canvas, @NonNull Paint paint,
@Px int left, @Px int right,
@Px int top, @Px int baseline, @Px int bottom,
@NonNull CharSequence text, int start, int end,
int lineNumber);
/**
* Default implementation of the {@link LineBackgroundSpan}, which changes the background
* color of the lines to which the span is attached.
*/
class Standard implements LineBackgroundSpan, ParcelableSpan {
private final int mColor;
/**
* Constructor taking a color integer.
*
* @param color Color integer that defines the background color.
*/
public Standard(@ColorInt int color) {
mColor = color;
}
/**
* Creates a {@link LineBackgroundSpan.Standard} from a parcel
*/
public Standard(@NonNull Parcel src) {
mColor = src.readInt();
}
@Override
public int getSpanTypeId() {
return getSpanTypeIdInternal();
}
/** @hide */
@Override
public int getSpanTypeIdInternal() {
return TextUtils.LINE_BACKGROUND_SPAN;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
writeToParcelInternal(dest, flags);
}
/** @hide */
@Override
public void writeToParcelInternal(@NonNull Parcel dest, int flags) {
dest.writeInt(mColor);
}
/**
* @return the color of this span.
* @see Standard#Standard(int)
*/
@ColorInt
public final int getColor() {
return mColor;
}
@Override
public void drawBackground(@NonNull Canvas canvas, @NonNull Paint paint,
@Px int left, @Px int right,
@Px int top, @Px int baseline, @Px int bottom,
@NonNull CharSequence text, int start, int end,
int lineNumber) {
final int originColor = paint.getColor();
paint.setColor(mColor);
canvas.drawRect(left, right, top, bottom, paint);
paint.setColor(originColor);
}
}
}