2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.colorpicker;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.BitmapShader;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.LinearGradient;
25 import android.graphics.Paint;
26 import android.graphics.RadialGradient;
27 import android.graphics.Shader;
28 import android.util.AttributeSet;
29 import android.util.DisplayMetrics;
30 import android.view.MotionEvent;
31 import android.view.View;
33 import com.android.gallery3d.R;
35 import java.util.ArrayList;
36 import java.util.Arrays;
38 public class ColorSaturationView extends View implements ColorListener {
40 private float mRadius;
42 private Paint mBarPaint1;
43 private Paint mLinePaint1;
44 private Paint mLinePaint2;
45 private Paint mCheckPaint;
47 private float mHeight;
48 private Paint mDotPaint;
49 private int mBgcolor = 0;
51 private float mDotRadius;
52 private float mBorder;
54 private float[] mHSVO = new float[4];
55 private int mSliderColor;
56 private float mDotX = mBorder;
57 private float mDotY = mBorder;
58 private final static float DOT_SIZE = ColorRectView.DOT_SIZE;
59 public final static float BORDER_SIZE = 20;;
61 private ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
63 public ColorSaturationView(Context ctx, AttributeSet attrs) {
65 DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
66 float mDpToPix = metrics.density;
67 mDotRadius = DOT_SIZE * mDpToPix;
68 mBorder = BORDER_SIZE * mDpToPix;
69 mBarPaint1 = new Paint();
71 mDotPaint = new Paint();
73 mDotPaint.setStyle(Paint.Style.FILL);
74 mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
75 mSliderColor = ctx.getResources().getColor(R.color.slider_line_color);
77 mBarPaint1.setStyle(Paint.Style.FILL);
79 mLinePaint1 = new Paint();
80 mLinePaint1.setColor(Color.GRAY);
81 mLinePaint2 = new Paint();
82 mLinePaint2.setColor(mSliderColor);
83 mLinePaint2.setStrokeWidth(4);
85 int[] colors = new int[16 * 16];
86 for (int i = 0; i < colors.length; i++) {
89 colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444;
91 Bitmap bitmap = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.ARGB_8888);
92 BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
93 mCheckPaint = new Paint();
94 mCheckPaint.setShader(bs);
97 public boolean onDown(MotionEvent e) {
102 public boolean onTouchEvent(MotionEvent event) {
106 float x = event.getX();
107 float y = event.getY();
111 if (mDotX < mBorder) {
115 if (mDotX > mWidth - mBorder) {
116 mDotX = mWidth - mBorder;
118 mHSVO[3] = (mDotX - mBorder) / (mWidth - mBorder * 2);
119 notifyColorListeners(mHSVO);
121 invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius),
122 (int) (oy + mDotRadius));
124 (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius),
125 (int) (mDotY + mDotRadius));
130 private void setupButton() {
131 float pos = mHSVO[3] * (mWidth - mBorder * 2);
132 mDotX = pos + mBorder;
134 int[] colors3 = new int[] {
135 mSliderColor, mSliderColor, 0x66000000, 0 };
136 RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadius, colors3, new float[] {
137 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP);
138 mDotPaint.setShader(g);
142 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
150 private void updatePaint() {
151 float[]hsvo = Arrays.copyOf(mHSVO, 4);
155 int color2 = Color.HSVToColor(hsvo);
157 int color1 = Color.HSVToColor(hsvo);
158 Shader sg = new LinearGradient(
159 mBorder, mBorder, mWidth - mBorder, mBorder,
160 color1, color2, Shader.TileMode.CLAMP);
161 mBarPaint1.setShader(sg);
166 protected void onDraw(Canvas canvas) {
167 super.onDraw(canvas);
168 canvas.drawColor(mBgcolor);
169 canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mCheckPaint);
170 canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mBarPaint1);
171 canvas.drawLine(mDotX, mDotY, mWidth - mBorder, mDotY, mLinePaint1);
172 canvas.drawLine(mBorder, mDotY, mDotX, mDotY, mLinePaint2);
173 if (mDotX != Float.NaN) {
174 canvas.drawCircle(mDotX, mDotY, mDotRadius, mDotPaint);
179 public void setColor(float[] hsv) {
180 System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length);
189 public void notifyColorListeners(float[] hsvo) {
190 for (ColorListener l : mColorListeners) {
195 public void addColorListener(ColorListener l) {
196 mColorListeners.add(l);
199 public void removeColorListener(ColorListener l) {
200 mColorListeners.remove(l);