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 41 void onColorChanged(int color, boolean fromUser); 42 } 43 44 private final int[] colors; 45 private Bitmap background; 46 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 61 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 62 super.onSizeChanged(w, h, oldw, oldh); 63 64 if (background != null) { 65 background.recycle(); 66 } 67 background = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 68 Canvas canvas = new Canvas(background); 69 70 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 71 paint.setStyle(Paint.Style.FILL); 72 73 // Draw two half circles in the first and last colors at seek-bar left/right ends. 74 int radius = getThumbOffset(); 75 float left = radius; 76 float right = w - radius; 77 float cy = h / 2; 78 79 canvas.save(); 80 canvas.clipRect(left, 0, right, h, Op.DIFFERENCE); 81 paint.setColor(colors[0]); 82 canvas.drawCircle(left, cy, radius, paint); 83 paint.setColor(colors[colors.length - 1]); 84 canvas.drawCircle(right, cy, radius, paint); 85 canvas.restore(); 86 87 // Draw color strips that make the thumb stop at every strip's center during seeking. 88 float strip = (right - left) / (colors.length - 1); 89 right = left + strip / 2; 90 paint.setColor(colors[0]); 91 canvas.drawRect(left, 0, right, h, paint); 92 left = right; 93 for (int i = 1; i < colors.length - 1; i++) { 94 right = left + strip; 95 paint.setColor(colors[i]); 96 canvas.drawRect(left, 0, right, h, paint); 97 left = right; 98 } 99 right = left + strip / 2; 100 paint.setColor(colors[colors.length - 1]); 101 canvas.drawRect(left, 0, right, h, paint); 102 103 setBackgroundDrawable(new BitmapDrawable(getResources(), background)); 104 } 105 106 public void setOnColorChangeListener(final OnColorChangeListener listener) { 107 setOnSeekBarChangeListener((listener == null) ? null : new OnSeekBarChangeListener() { 108 109 @Override 110 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 111 listener.onColorChanged(colors[progress], fromUser); 112 } 113 114 @Override 115 public void onStartTrackingTouch(SeekBar seekBar) { 116 } 117 118 @Override 119 public void onStopTrackingTouch(SeekBar seekBar) { 120 } 121 }); 122 } 123 124 public void setColorIndex(int colorIndex) { 125 setProgress(colorIndex); 126 } 127 128 public int getColor() { 129 return colors[getProgress()]; 130 } 131 } 132