Home | History | Annotate | Download | only in style
      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 public class RelativeSizeSpan extends MetricAffectingSpan implements ParcelableSpan {
     38 
     39     private final float mProportion;
     40 
     41     /**
     42      * Creates a {@link RelativeSizeSpan} based on a proportion.
     43      *
     44      * @param proportion the proportion with which the text is scaled.
     45      */
     46     public RelativeSizeSpan(@FloatRange(from = 0) float proportion) {
     47         mProportion = proportion;
     48     }
     49 
     50     /**
     51      * Creates a {@link RelativeSizeSpan} from a parcel.
     52      */
     53     public RelativeSizeSpan(@NonNull Parcel src) {
     54         mProportion = src.readFloat();
     55     }
     56 
     57     @Override
     58     public int getSpanTypeId() {
     59         return getSpanTypeIdInternal();
     60     }
     61 
     62     /** @hide */
     63     @Override
     64     public int getSpanTypeIdInternal() {
     65         return TextUtils.RELATIVE_SIZE_SPAN;
     66     }
     67 
     68     @Override
     69     public int describeContents() {
     70         return 0;
     71     }
     72 
     73     @Override
     74     public void writeToParcel(@NonNull Parcel dest, int flags) {
     75         writeToParcelInternal(dest, flags);
     76     }
     77 
     78     /** @hide */
     79     @Override
     80     public void writeToParcelInternal(@NonNull Parcel dest, int flags) {
     81         dest.writeFloat(mProportion);
     82     }
     83 
     84     /**
     85      * @return the proportion with which the text size is changed.
     86      */
     87     public float getSizeChange() {
     88         return mProportion;
     89     }
     90 
     91     @Override
     92     public void updateDrawState(@NonNull TextPaint ds) {
     93         ds.setTextSize(ds.getTextSize() * mProportion);
     94     }
     95 
     96     @Override
     97     public void updateMeasureState(@NonNull TextPaint ds) {
     98         ds.setTextSize(ds.getTextSize() * mProportion);
     99     }
    100 }
    101