1 /* 2 * Copyright (C) 2008 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.NonNull; 20 import android.os.Parcel; 21 import android.text.ParcelableSpan; 22 import android.text.TextPaint; 23 import android.text.TextUtils; 24 25 /** 26 * A span that changes the size of the text it's attached to. 27 * <p> 28 * For example, the size of the text can be changed to 55dp like this: 29 * <pre>{@code 30 * SpannableString string = new SpannableString("Text with absolute size span"); 31 *string.setSpan(new AbsoluteSizeSpan(55, true), 10, 23, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 32 * <img src="{@docRoot}reference/android/images/text/style/absolutesizespan.png" /> 33 * <figcaption>Text with text size updated.</figcaption> 34 */ 35 @android.ravenwood.annotation.RavenwoodKeepWholeClass 36 public class AbsoluteSizeSpan extends MetricAffectingSpan implements ParcelableSpan { 37 38 private final int mSize; 39 private final boolean mDip; 40 41 /** 42 * Set the text size to <code>size</code> physical pixels. 43 */ AbsoluteSizeSpan(int size)44 public AbsoluteSizeSpan(int size) { 45 this(size, false); 46 } 47 48 /** 49 * Set the text size to <code>size</code> physical pixels, or to <code>size</code> 50 * device-independent pixels if <code>dip</code> is true. 51 */ AbsoluteSizeSpan(int size, boolean dip)52 public AbsoluteSizeSpan(int size, boolean dip) { 53 mSize = size; 54 mDip = dip; 55 } 56 57 /** 58 * Creates an {@link AbsoluteSizeSpan} from a parcel. 59 */ AbsoluteSizeSpan(@onNull Parcel src)60 public AbsoluteSizeSpan(@NonNull Parcel src) { 61 mSize = src.readInt(); 62 mDip = src.readInt() != 0; 63 } 64 65 @Override getSpanTypeId()66 public int getSpanTypeId() { 67 return getSpanTypeIdInternal(); 68 } 69 70 /** @hide */ 71 @Override getSpanTypeIdInternal()72 public int getSpanTypeIdInternal() { 73 return TextUtils.ABSOLUTE_SIZE_SPAN; 74 } 75 76 @Override describeContents()77 public int describeContents() { 78 return 0; 79 } 80 81 @Override writeToParcel(@onNull Parcel dest, int flags)82 public void writeToParcel(@NonNull Parcel dest, int flags) { 83 writeToParcelInternal(dest, flags); 84 } 85 86 /** @hide */ 87 @Override writeToParcelInternal(@onNull Parcel dest, int flags)88 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 89 dest.writeInt(mSize); 90 dest.writeInt(mDip ? 1 : 0); 91 } 92 93 /** 94 * Get the text size. This is in physical pixels if {@link #getDip()} returns false or in 95 * device-independent pixels if {@link #getDip()} returns true. 96 * 97 * @return the text size, either in physical pixels or device-independent pixels. 98 * @see AbsoluteSizeSpan#AbsoluteSizeSpan(int, boolean) 99 */ getSize()100 public int getSize() { 101 return mSize; 102 } 103 104 /** 105 * Returns whether the size is in device-independent pixels or not, depending on the 106 * <code>dip</code> flag passed in {@link #AbsoluteSizeSpan(int, boolean)} 107 * 108 * @return <code>true</code> if the size is in device-independent pixels, <code>false</code> 109 * otherwise 110 * 111 * @see #AbsoluteSizeSpan(int, boolean) 112 */ getDip()113 public boolean getDip() { 114 return mDip; 115 } 116 117 @Override updateDrawState(@onNull TextPaint ds)118 public void updateDrawState(@NonNull TextPaint ds) { 119 if (mDip) { 120 ds.setTextSize(mSize * ds.density); 121 } else { 122 ds.setTextSize(mSize); 123 } 124 } 125 126 @Override updateMeasureState(@onNull TextPaint ds)127 public void updateMeasureState(@NonNull TextPaint ds) { 128 if (mDip) { 129 ds.setTextSize(mSize * ds.density); 130 } else { 131 ds.setTextSize(mSize); 132 } 133 } 134 135 136 @Override toString()137 public String toString() { 138 return "AbsoluteSizeSpan{size=" + getSize() + ", isDip=" + getDip() + '}'; 139 } 140 } 141