1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.ui; 6 7 import android.content.Context; 8 import android.graphics.drawable.GradientDrawable; 9 import android.graphics.drawable.GradientDrawable.Orientation; 10 import android.os.Build; 11 import android.view.View; 12 import android.widget.SeekBar; 13 import android.widget.SeekBar.OnSeekBarChangeListener; 14 import android.widget.TextView; 15 16 import org.chromium.base.ApiCompatibilityUtils; 17 18 /** 19 * Encapsulates a single gradient view of the HSV color display, including its label, gradient 20 * view and seek bar. 21 * 22 * Mirrors a "color_picker_advanced_component" layout. 23 */ 24 public class ColorPickerAdvancedComponent { 25 // The view that displays the gradient. 26 private final View mGradientView; 27 // The seek bar that allows the user to change the value of this component. 28 private final SeekBar mSeekBar; 29 // The set of colors to interpolate the gradient through. 30 private int[] mGradientColors; 31 // The Drawable that represents the gradient. 32 private GradientDrawable mGradientDrawable; 33 // The text label for the component. 34 private final TextView mText; 35 36 /** 37 * Initializes the views. 38 * 39 * @param rootView View that contains all the content, such as the label, gradient view, etc. 40 * @param textResourceId The resource ID of the text to show on the label. 41 * @param seekBarMax The range of the seek bar. 42 * @param seekBarListener The listener for when the seek bar value changes. 43 */ 44 ColorPickerAdvancedComponent(final View rootView, 45 final int textResourceId, 46 final int seekBarMax, 47 final OnSeekBarChangeListener seekBarListener) { 48 mGradientView = rootView.findViewById(R.id.gradient); 49 mText = (TextView) rootView.findViewById(R.id.text); 50 mText.setText(textResourceId); 51 mGradientDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, null); 52 mSeekBar = (SeekBar) rootView.findViewById(R.id.seek_bar); 53 mSeekBar.setOnSeekBarChangeListener(seekBarListener); 54 mSeekBar.setMax(seekBarMax); 55 // Setting the thumb offset means the seek bar thumb can move all the way to each end 56 // of the gradient view. 57 Context context = rootView.getContext(); 58 int offset = context.getResources() 59 .getDrawable(R.drawable.color_picker_advanced_select_handle) 60 .getIntrinsicWidth(); 61 mSeekBar.setThumbOffset(offset / 2); 62 } 63 64 /** 65 * @return The value represented by this component, maintained by the seek bar progress. 66 */ 67 public float getValue() { 68 return mSeekBar.getProgress(); 69 } 70 71 /** 72 * Sets the value of the component (by setting the seek bar value). 73 * 74 * @param newValue The value to give the component. 75 */ 76 public void setValue(float newValue) { 77 mSeekBar.setProgress((int) newValue); 78 } 79 80 /** 81 * Sets the colors for the gradient view to interpolate through. 82 * 83 * @param newColors The set of colors representing the interpolation points for the gradient. 84 */ 85 public void setGradientColors(int[] newColors) { 86 mGradientColors = newColors.clone(); 87 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 88 Orientation currentOrientation = Orientation.LEFT_RIGHT; 89 mGradientDrawable = new GradientDrawable(currentOrientation, mGradientColors); 90 } else { 91 mGradientDrawable.setColors(mGradientColors); 92 } 93 ApiCompatibilityUtils.setBackgroundForView(mGradientView, mGradientDrawable); 94 } 95 } 96