• 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.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  * Scales horizontally the size of the text to which it's attached by a certain factor.
28  * <p>
29  * Values > 1.0 will stretch the text wider. Values < 1.0 will stretch the text narrower.
30  * <p>
31  * For example, a <code>ScaleXSpan</code> that stretches the text size by 100% can be
32  * constructed like this:
33  * <pre>{@code
34  * SpannableString string = new SpannableString("Text with ScaleX span");
35  *string.setSpan(new ScaleXSpan(2f), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);}</pre>
36  * <img src="{@docRoot}reference/android/images/text/style/scalexspan.png" />
37  * <figcaption>Text scaled by 100% with <code>ScaleXSpan</code>.</figcaption>
38  */
39 @android.ravenwood.annotation.RavenwoodKeepWholeClass
40 public class ScaleXSpan extends MetricAffectingSpan implements ParcelableSpan {
41 
42     private final float mProportion;
43 
44     /**
45      * Creates a {@link ScaleXSpan} based on a proportion. Values > 1.0 will stretch the text wider.
46      * Values < 1.0 will stretch the text narrower.
47      *
48      * @param proportion the horizontal scale factor.
49      */
ScaleXSpan(@loatRangefrom = 0) float proportion)50     public ScaleXSpan(@FloatRange(from = 0) float proportion) {
51         mProportion = proportion;
52     }
53 
54     /**
55      * Creates a {@link ScaleXSpan} from a parcel.
56      */
ScaleXSpan(@onNull Parcel src)57     public ScaleXSpan(@NonNull Parcel src) {
58         mProportion = src.readFloat();
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.SCALE_X_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.writeFloat(mProportion);
86     }
87 
88     /**
89      * Get the horizontal scale factor for the text.
90      *
91      * @return the horizontal scale factor.
92      */
getScaleX()93     public float getScaleX() {
94         return mProportion;
95     }
96 
97     @Override
updateDrawState(TextPaint ds)98     public void updateDrawState(TextPaint ds) {
99         ds.setTextScaleX(ds.getTextScaleX() * mProportion);
100     }
101 
102     @Override
updateMeasureState(TextPaint ds)103     public void updateMeasureState(TextPaint ds) {
104         ds.setTextScaleX(ds.getTextScaleX() * mProportion);
105     }
106 
107     @Override
toString()108     public String toString() {
109         return "ScaleXSpan{scaleX=" + getScaleX() + '}';
110     }
111 }
112