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