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