• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 com.android.gallery3d.photoeditor.actions;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.graphics.Bitmap;
22 import android.graphics.Canvas;
23 import android.graphics.Paint;
24 import android.graphics.Region.Op;
25 import android.graphics.drawable.BitmapDrawable;
26 import android.util.AttributeSet;
27 import android.widget.SeekBar;
28 
29 import com.android.gallery3d.R;
30 
31 /**
32  * Seek-bar that has a draggable thumb to set and get the color from predefined color set.
33  */
34 class ColorSeekBar extends AbstractSeekBar {
35 
36     /**
37      * Listens to color changes.
38      */
39     public interface OnColorChangeListener {
40 
onColorChanged(int color, boolean fromUser)41         void onColorChanged(int color, boolean fromUser);
42     }
43 
44     private final int[] colors;
45     private Bitmap progressDrawable;
46 
ColorSeekBar(Context context, AttributeSet attrs)47     public ColorSeekBar(Context context, AttributeSet attrs) {
48         super(context, attrs);
49 
50         // Set up the predefined colors that could be indexed in the seek-bar.
51         TypedArray a = getResources().obtainTypedArray(R.array.color_picker_colors);
52         colors = new int[a.length()];
53         for (int i = 0; i < a.length(); i++) {
54             colors[i] = a.getColor(i, 0x000000);
55         }
56         a.recycle();
57         setMax(colors.length - 1);
58     }
59 
60     @Override
onSizeChanged(int w, int h, int oldw, int oldh)61     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
62         super.onSizeChanged(w, h, oldw, oldh);
63 
64         if (progressDrawable != null) {
65             progressDrawable.recycle();
66         }
67         int width = w - getPaddingLeft() - getPaddingRight();
68         int height = h - getPaddingTop() - getPaddingBottom();
69         progressDrawable = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
70         Canvas canvas = new Canvas(progressDrawable);
71 
72         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
73         paint.setStyle(Paint.Style.FILL);
74 
75         // Draw two half circles in the first and last colors at the left/right ends.
76         int radius = height / 2;
77         float left = radius;
78         float right = width - radius;
79 
80         canvas.save();
81         canvas.clipRect(left, 0, right, height, Op.DIFFERENCE);
82         paint.setColor(colors[0]);
83         canvas.drawCircle(left, radius, radius, paint);
84         paint.setColor(colors[colors.length - 1]);
85         canvas.drawCircle(right, radius, radius, paint);
86         canvas.restore();
87 
88         // Draw color strips that make the thumb stop at every strip's center during seeking.
89         float strip = (right - left) / (colors.length - 1);
90         right = left + strip / 2;
91         paint.setColor(colors[0]);
92         canvas.drawRect(left, 0, right, height, paint);
93         left = right;
94         for (int i = 1; i < colors.length - 1; i++) {
95             right = left + strip;
96             paint.setColor(colors[i]);
97             canvas.drawRect(left, 0, right, height, paint);
98             left = right;
99         }
100         right = left + strip / 2;
101         paint.setColor(colors[colors.length - 1]);
102         canvas.drawRect(left, 0, right, height, paint);
103 
104         setProgressDrawable(new BitmapDrawable(getResources(), progressDrawable));
105     }
106 
setOnColorChangeListener(final OnColorChangeListener listener)107     public void setOnColorChangeListener(final OnColorChangeListener listener) {
108         setOnSeekBarChangeListener((listener == null) ? null : new OnSeekBarChangeListener() {
109 
110             @Override
111             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
112                 listener.onColorChanged(colors[progress], fromUser);
113             }
114 
115             @Override
116             public void onStartTrackingTouch(SeekBar seekBar) {
117             }
118 
119             @Override
120             public void onStopTrackingTouch(SeekBar seekBar) {
121             }
122         });
123     }
124 
setColorIndex(int colorIndex)125     public void setColorIndex(int colorIndex) {
126         setProgress(colorIndex);
127     }
128 
getColor()129     public int getColor() {
130         return colors[getProgress()];
131     }
132 }
133