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 public class AbsoluteSizeSpan extends MetricAffectingSpan implements ParcelableSpan { 36 37 private final int mSize; 38 private final boolean mDip; 39 40 /** 41 * Set the text size to <code>size</code> physical pixels. 42 */ AbsoluteSizeSpan(int size)43 public AbsoluteSizeSpan(int size) { 44 this(size, false); 45 } 46 47 /** 48 * Set the text size to <code>size</code> physical pixels, or to <code>size</code> 49 * device-independent pixels if <code>dip</code> is true. 50 */ AbsoluteSizeSpan(int size, boolean dip)51 public AbsoluteSizeSpan(int size, boolean dip) { 52 mSize = size; 53 mDip = dip; 54 } 55 56 /** 57 * Creates an {@link AbsoluteSizeSpan} from a parcel. 58 */ AbsoluteSizeSpan(@onNull Parcel src)59 public AbsoluteSizeSpan(@NonNull Parcel src) { 60 mSize = src.readInt(); 61 mDip = src.readInt() != 0; 62 } 63 64 @Override getSpanTypeId()65 public int getSpanTypeId() { 66 return getSpanTypeIdInternal(); 67 } 68 69 /** @hide */ 70 @Override getSpanTypeIdInternal()71 public int getSpanTypeIdInternal() { 72 return TextUtils.ABSOLUTE_SIZE_SPAN; 73 } 74 75 @Override describeContents()76 public int describeContents() { 77 return 0; 78 } 79 80 @Override writeToParcel(@onNull Parcel dest, int flags)81 public void writeToParcel(@NonNull Parcel dest, int flags) { 82 writeToParcelInternal(dest, flags); 83 } 84 85 /** @hide */ 86 @Override writeToParcelInternal(@onNull Parcel dest, int flags)87 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 88 dest.writeInt(mSize); 89 dest.writeInt(mDip ? 1 : 0); 90 } 91 92 /** 93 * Get the text size. This is in physical pixels if {@link #getDip()} returns false or in 94 * device-independent pixels if {@link #getDip()} returns true. 95 * 96 * @return the text size, either in physical pixels or device-independent pixels. 97 * @see AbsoluteSizeSpan#AbsoluteSizeSpan(int, boolean) 98 */ getSize()99 public int getSize() { 100 return mSize; 101 } 102 103 /** 104 * Returns whether the size is in device-independent pixels or not, depending on the 105 * <code>dip</code> flag passed in {@link #AbsoluteSizeSpan(int, boolean)} 106 * 107 * @return <code>true</code> if the size is in device-independent pixels, <code>false</code> 108 * otherwise 109 * 110 * @see #AbsoluteSizeSpan(int, boolean) 111 */ getDip()112 public boolean getDip() { 113 return mDip; 114 } 115 116 @Override updateDrawState(@onNull TextPaint ds)117 public void updateDrawState(@NonNull TextPaint ds) { 118 if (mDip) { 119 ds.setTextSize(mSize * ds.density); 120 } else { 121 ds.setTextSize(mSize); 122 } 123 } 124 125 @Override updateMeasureState(@onNull TextPaint ds)126 public void updateMeasureState(@NonNull TextPaint ds) { 127 if (mDip) { 128 ds.setTextSize(mSize * ds.density); 129 } else { 130 ds.setTextSize(mSize); 131 } 132 } 133 } 134