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.FloatRange; 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 * Uniformly scales the size of the text to which it's attached by a certain proportion. 28 * <p> 29 * For example, a <code>RelativeSizeSpan</code> that increases the text size by 50% can be 30 * constructed like this: 31 * <pre>{@code 32 * SpannableString string = new SpannableString("Text with relative size span"); 33 *string.setSpan(new RelativeSizeSpan(1.5f), 10, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre> 34 * <img src="{@docRoot}reference/android/images/text/style/relativesizespan.png" /> 35 * <figcaption>Text increased by 50% with <code>RelativeSizeSpan</code>.</figcaption> 36 */ 37 @android.ravenwood.annotation.RavenwoodKeepWholeClass 38 public class RelativeSizeSpan extends MetricAffectingSpan implements ParcelableSpan { 39 40 private final float mProportion; 41 42 /** 43 * Creates a {@link RelativeSizeSpan} based on a proportion. 44 * 45 * @param proportion the proportion with which the text is scaled. 46 */ RelativeSizeSpan(@loatRangefrom = 0) float proportion)47 public RelativeSizeSpan(@FloatRange(from = 0) float proportion) { 48 mProportion = proportion; 49 } 50 51 /** 52 * Creates a {@link RelativeSizeSpan} from a parcel. 53 */ RelativeSizeSpan(@onNull Parcel src)54 public RelativeSizeSpan(@NonNull Parcel src) { 55 mProportion = src.readFloat(); 56 } 57 58 @Override getSpanTypeId()59 public int getSpanTypeId() { 60 return getSpanTypeIdInternal(); 61 } 62 63 /** @hide */ 64 @Override getSpanTypeIdInternal()65 public int getSpanTypeIdInternal() { 66 return TextUtils.RELATIVE_SIZE_SPAN; 67 } 68 69 @Override describeContents()70 public int describeContents() { 71 return 0; 72 } 73 74 @Override writeToParcel(@onNull Parcel dest, int flags)75 public void writeToParcel(@NonNull Parcel dest, int flags) { 76 writeToParcelInternal(dest, flags); 77 } 78 79 /** @hide */ 80 @Override writeToParcelInternal(@onNull Parcel dest, int flags)81 public void writeToParcelInternal(@NonNull Parcel dest, int flags) { 82 dest.writeFloat(mProportion); 83 } 84 85 /** 86 * @return the proportion with which the text size is changed. 87 */ getSizeChange()88 public float getSizeChange() { 89 return mProportion; 90 } 91 92 @Override updateDrawState(@onNull TextPaint ds)93 public void updateDrawState(@NonNull TextPaint ds) { 94 ds.setTextSize(ds.getTextSize() * mProportion); 95 } 96 97 @Override updateMeasureState(@onNull TextPaint ds)98 public void updateMeasureState(@NonNull TextPaint ds) { 99 ds.setTextSize(ds.getTextSize() * mProportion); 100 } 101 102 @Override toString()103 public String toString() { 104 return "RelativeSizeSpan{proportion=" + getSizeChange() + '}'; 105 } 106 } 107