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 17 package android.text.style; 18 19 import android.annotation.ColorInt; 20 import android.annotation.NonNull; 21 import android.annotation.Px; 22 import android.graphics.Canvas; 23 import android.graphics.Paint; 24 import android.os.Parcel; 25 import android.text.ParcelableSpan; 26 import android.text.TextUtils; 27 28 /** 29 * Used to change the background of lines where the span is attached to. 30 */ 31 @android.ravenwood.annotation.RavenwoodKeepWholeClass 32 public interface LineBackgroundSpan extends ParagraphStyle 33 { 34 /** 35 * Draw the background on the canvas. 36 * 37 * @param canvas canvas on which the span should be rendered 38 * @param paint paint used to draw text, which should be left unchanged on exit 39 * @param left left position of the line relative to input canvas, in pixels 40 * @param right right position of the line relative to input canvas, in pixels 41 * @param top top position of the line relative to input canvas, in pixels 42 * @param baseline baseline of the text relative to input canvas, in pixels 43 * @param bottom bottom position of the line relative to input canvas, in pixels 44 * @param text current text 45 * @param start start character index of the line 46 * @param end end character index of the line 47 * @param lineNumber line number in the current text layout 48 */ drawBackground(@onNull 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)49 void drawBackground(@NonNull Canvas canvas, @NonNull Paint paint, 50 @Px int left, @Px int right, 51 @Px int top, @Px int baseline, @Px int bottom, 52 @NonNull CharSequence text, int start, int end, 53 int lineNumber); 54 /** 55 * Default implementation of the {@link LineBackgroundSpan}, which changes the background 56 * color of the lines to which the span is attached. 57 * <p> 58 * For example, an <code>LineBackgroundSpan</code> can be used like this: 59 * <pre> 60 * String text = "This is a multiline text. LineBackgroundSpan is applied here. This is a multiline text."; 61 * SpannableString string = new SpannableString(text); 62 * string.setSpan(new LineBackgroundSpan.Standard(Color.YELLOW), 26, 61, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 63 * </pre> 64 * <img src="{@docRoot}reference/android/images/text/style/linebackgroundspan.png" /> 65 * <figcaption>Text with <code>LineBackgroundSpan</code></figcaption> 66 */ 67 class Standard implements LineBackgroundSpan, ParcelableSpan { 68 69 private final int mColor; 70 71 /** 72 * Constructor taking a color integer. 73 * 74 * @param color Color integer that defines the background color. 75 */ Standard(@olorInt int color)76 public Standard(@ColorInt int color) { 77 mColor = color; 78 } 79 80 /** 81 * Creates a {@link LineBackgroundSpan.Standard} from a parcel 82 */ Standard(@onNull Parcel src)83 public Standard(@NonNull Parcel src) { 84 mColor = src.readInt(); 85 } 86 87 @Override getSpanTypeId()88 public int getSpanTypeId() { 89 return getSpanTypeIdInternal(); 90 } 91 92 /** @hide */ 93 @Override getSpanTypeIdInternal()94 public int getSpanTypeIdInternal() { 95 return TextUtils.LINE_BACKGROUND_SPAN; 96 } 97 98 @Override describeContents()99 public int describeContents() { 100 return 0; 101 } 102 103 @Override writeToParcel(@onNull Parcel dest, int flags)104 public void writeToParcel(@NonNull Parcel dest, int flags) { 105 writeToParcelInternal(dest, flags); 106 } 107 108 /** @hide */ 109 @Override writeToParcelInternal(@onNull Parcel dest, int flags)110 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 111 dest.writeInt(mColor); 112 } 113 114 /** 115 * @return the color of this span. 116 * @see Standard#Standard(int) 117 */ 118 @ColorInt getColor()119 public final int getColor() { 120 return mColor; 121 } 122 123 @Override drawBackground(@onNull 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)124 public void drawBackground(@NonNull Canvas canvas, @NonNull Paint paint, 125 @Px int left, @Px int right, 126 @Px int top, @Px int baseline, @Px int bottom, 127 @NonNull CharSequence text, int start, int end, 128 int lineNumber) { 129 final int originColor = paint.getColor(); 130 paint.setColor(mColor); 131 canvas.drawRect(left, top, right, bottom, paint); 132 paint.setColor(originColor); 133 } 134 } 135 } 136