Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2015 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 androidx.appcompat.widget;
     18 
     19 import android.content.res.ColorStateList;
     20 import android.graphics.Canvas;
     21 import android.graphics.PorterDuff;
     22 import android.graphics.drawable.Drawable;
     23 import android.util.AttributeSet;
     24 import android.widget.SeekBar;
     25 
     26 import androidx.annotation.Nullable;
     27 import androidx.appcompat.R;
     28 import androidx.core.graphics.drawable.DrawableCompat;
     29 import androidx.core.view.ViewCompat;
     30 
     31 class AppCompatSeekBarHelper extends AppCompatProgressBarHelper {
     32 
     33     private final SeekBar mView;
     34 
     35     private Drawable mTickMark;
     36     private ColorStateList mTickMarkTintList = null;
     37     private PorterDuff.Mode mTickMarkTintMode = null;
     38     private boolean mHasTickMarkTint = false;
     39     private boolean mHasTickMarkTintMode = false;
     40 
     41     AppCompatSeekBarHelper(SeekBar view) {
     42         super(view);
     43         mView = view;
     44     }
     45 
     46     @Override
     47     void loadFromAttributes(AttributeSet attrs, int defStyleAttr) {
     48         super.loadFromAttributes(attrs, defStyleAttr);
     49 
     50         TintTypedArray a = TintTypedArray.obtainStyledAttributes(mView.getContext(), attrs,
     51                 R.styleable.AppCompatSeekBar, defStyleAttr, 0);
     52         final Drawable drawable = a.getDrawableIfKnown(R.styleable.AppCompatSeekBar_android_thumb);
     53         if (drawable != null) {
     54             mView.setThumb(drawable);
     55         }
     56 
     57         final Drawable tickMark = a.getDrawable(R.styleable.AppCompatSeekBar_tickMark);
     58         setTickMark(tickMark);
     59 
     60         if (a.hasValue(R.styleable.AppCompatSeekBar_tickMarkTintMode)) {
     61             mTickMarkTintMode = DrawableUtils.parseTintMode(a.getInt(
     62                     R.styleable.AppCompatSeekBar_tickMarkTintMode, -1), mTickMarkTintMode);
     63             mHasTickMarkTintMode = true;
     64         }
     65 
     66         if (a.hasValue(R.styleable.AppCompatSeekBar_tickMarkTint)) {
     67             mTickMarkTintList = a.getColorStateList(R.styleable.AppCompatSeekBar_tickMarkTint);
     68             mHasTickMarkTint = true;
     69         }
     70 
     71         a.recycle();
     72 
     73         applyTickMarkTint();
     74     }
     75 
     76     void setTickMark(@Nullable Drawable tickMark) {
     77         if (mTickMark != null) {
     78             mTickMark.setCallback(null);
     79         }
     80 
     81         mTickMark = tickMark;
     82 
     83         if (tickMark != null) {
     84             tickMark.setCallback(mView);
     85             DrawableCompat.setLayoutDirection(tickMark, ViewCompat.getLayoutDirection(mView));
     86             if (tickMark.isStateful()) {
     87                 tickMark.setState(mView.getDrawableState());
     88             }
     89             applyTickMarkTint();
     90         }
     91 
     92         mView.invalidate();
     93     }
     94 
     95     @Nullable
     96     Drawable getTickMark() {
     97         return mTickMark;
     98     }
     99 
    100     void setTickMarkTintList(@Nullable ColorStateList tint) {
    101         mTickMarkTintList = tint;
    102         mHasTickMarkTint = true;
    103 
    104         applyTickMarkTint();
    105     }
    106 
    107     @Nullable
    108     ColorStateList getTickMarkTintList() {
    109         return mTickMarkTintList;
    110     }
    111 
    112     void setTickMarkTintMode(@Nullable PorterDuff.Mode tintMode) {
    113         mTickMarkTintMode = tintMode;
    114         mHasTickMarkTintMode = true;
    115 
    116         applyTickMarkTint();
    117     }
    118 
    119     @Nullable
    120     PorterDuff.Mode getTickMarkTintMode() {
    121         return mTickMarkTintMode;
    122     }
    123 
    124     private void applyTickMarkTint() {
    125         if (mTickMark != null && (mHasTickMarkTint || mHasTickMarkTintMode)) {
    126             mTickMark = DrawableCompat.wrap(mTickMark.mutate());
    127 
    128             if (mHasTickMarkTint) {
    129                 DrawableCompat.setTintList(mTickMark, mTickMarkTintList);
    130             }
    131 
    132             if (mHasTickMarkTintMode) {
    133                 DrawableCompat.setTintMode(mTickMark, mTickMarkTintMode);
    134             }
    135 
    136             // The drawable (or one of its children) may not have been
    137             // stateful before applying the tint, so let's try again.
    138             if (mTickMark.isStateful()) {
    139                 mTickMark.setState(mView.getDrawableState());
    140             }
    141         }
    142     }
    143 
    144     void jumpDrawablesToCurrentState() {
    145         if (mTickMark != null) {
    146             mTickMark.jumpToCurrentState();
    147         }
    148     }
    149 
    150     void drawableStateChanged() {
    151         final Drawable tickMark = mTickMark;
    152         if (tickMark != null && tickMark.isStateful()
    153                 && tickMark.setState(mView.getDrawableState())) {
    154             mView.invalidateDrawable(tickMark);
    155         }
    156     }
    157 
    158     /**
    159      * Draw the tick marks.
    160      */
    161     void drawTickMarks(Canvas canvas) {
    162         if (mTickMark != null) {
    163             final int count = mView.getMax();
    164             if (count > 1) {
    165                 final int w = mTickMark.getIntrinsicWidth();
    166                 final int h = mTickMark.getIntrinsicHeight();
    167                 final int halfW = w >= 0 ? w / 2 : 1;
    168                 final int halfH = h >= 0 ? h / 2 : 1;
    169                 mTickMark.setBounds(-halfW, -halfH, halfW, halfH);
    170 
    171                 final float spacing = (mView.getWidth() - mView.getPaddingLeft()
    172                         - mView.getPaddingRight()) / (float) count;
    173                 final int saveCount = canvas.save();
    174                 canvas.translate(mView.getPaddingLeft(), mView.getHeight() / 2);
    175                 for (int i = 0; i <= count; i++) {
    176                     mTickMark.draw(canvas);
    177                     canvas.translate(spacing, 0);
    178                 }
    179                 canvas.restoreToCount(saveCount);
    180             }
    181         }
    182     }
    183 
    184 }
    185