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.os.Parcel; 22 import android.text.ParcelableSpan; 23 import android.text.TextPaint; 24 import android.text.TextUtils; 25 26 /** 27 * Changes the background color of the text to which the span is attached. 28 * <p> 29 * For example, to set a green background color for a text you would create a {@link 30 * android.text.SpannableString} based on the text and set the span. 31 * <pre>{@code 32 * SpannableString string = new SpannableString("Text with a background color span"); 33 *string.setSpan(new BackgroundColorSpan(color), 12, 28, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 34 * <img src="{@docRoot}reference/android/images/text/style/backgroundcolorspan.png" /> 35 * <figcaption>Set a background color for the text.</figcaption> 36 */ 37 @android.ravenwood.annotation.RavenwoodKeepWholeClass 38 public class BackgroundColorSpan extends CharacterStyle 39 implements UpdateAppearance, ParcelableSpan { 40 41 private final int mColor; 42 43 /** 44 * Creates a {@link BackgroundColorSpan} from a color integer. 45 * <p> 46 * 47 * @param color color integer that defines the background color 48 * @see android.content.res.Resources#getColor(int, Resources.Theme) 49 */ BackgroundColorSpan(@olorInt int color)50 public BackgroundColorSpan(@ColorInt int color) { 51 mColor = color; 52 } 53 54 /** 55 * Creates a {@link BackgroundColorSpan} from a parcel. 56 */ BackgroundColorSpan(@onNull Parcel src)57 public BackgroundColorSpan(@NonNull Parcel src) { 58 mColor = src.readInt(); 59 } 60 61 @Override getSpanTypeId()62 public int getSpanTypeId() { 63 return getSpanTypeIdInternal(); 64 } 65 66 /** @hide */ 67 @Override getSpanTypeIdInternal()68 public int getSpanTypeIdInternal() { 69 return TextUtils.BACKGROUND_COLOR_SPAN; 70 } 71 72 @Override describeContents()73 public int describeContents() { 74 return 0; 75 } 76 77 @Override writeToParcel(@onNull Parcel dest, int flags)78 public void writeToParcel(@NonNull Parcel dest, int flags) { 79 writeToParcelInternal(dest, flags); 80 } 81 82 /** @hide */ 83 @Override writeToParcelInternal(@onNull Parcel dest, int flags)84 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 85 dest.writeInt(mColor); 86 } 87 88 /** 89 * @return the background color of this span. 90 * @see BackgroundColorSpan#BackgroundColorSpan(int) 91 */ 92 @ColorInt getBackgroundColor()93 public int getBackgroundColor() { 94 return mColor; 95 } 96 97 /** 98 * Updates the background color of the TextPaint. 99 */ 100 @Override updateDrawState(@onNull TextPaint textPaint)101 public void updateDrawState(@NonNull TextPaint textPaint) { 102 textPaint.bgColor = mColor; 103 } 104 105 @Override toString()106 public String toString() { 107 return "BackgroundColorSpan{color=#" + String.format("%08X", getBackgroundColor()) + '}'; 108 } 109 } 110