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.Canvas;
21 import android.graphics.Color;
22 import android.graphics.LinearGradient;
23 import android.graphics.Paint;
24 import android.graphics.RadialGradient;
25 import android.graphics.Shader;
26 import android.util.AttributeSet;
27 import android.util.DisplayMetrics;
28 import android.view.MotionEvent;
29 import android.view.View;
31 import com.android.gallery3d.R;
33 import java.util.ArrayList;
35 public class ColorValueView extends View implements ColorListener {
37 private float mRadius;
39 private Paint mBarPaint1;
40 private Paint mLinePaint1;
41 private Paint mLinePaint2;
42 private float mHeight;
43 private int mBgcolor = 0;
44 private Paint mDotPaint;
45 private float dotRadus;
46 private float mBorder;
48 private float[] mHSVO = new float[4];
49 private int mSliderColor;
51 private float mDotY = mBorder;
52 private final static float DOT_SIZE = ColorRectView.DOT_SIZE;
53 private final static float BORDER_SIZE = ColorRectView.DOT_SIZE;
55 private ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
57 public ColorValueView(Context ctx, AttributeSet attrs) {
59 DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
60 float mDpToPix = metrics.density;
61 dotRadus = DOT_SIZE * mDpToPix;
62 mBorder = BORDER_SIZE * mDpToPix;
64 mBarPaint1 = new Paint();
66 mDotPaint = new Paint();
68 mDotPaint.setStyle(Paint.Style.FILL);
69 mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
71 mBarPaint1.setStyle(Paint.Style.FILL);
73 mLinePaint1 = new Paint();
74 mLinePaint1.setColor(Color.GRAY);
75 mLinePaint2 = new Paint();
76 mSliderColor = ctx.getResources().getColor(R.color.slider_line_color);
77 mLinePaint2.setColor(mSliderColor);
78 mLinePaint2.setStrokeWidth(4);
81 public boolean onDown(MotionEvent e) {
85 public boolean onTouchEvent(MotionEvent event) {
89 float x = event.getX();
90 float y = event.getY();
94 if (mDotY < mBorder) {
98 if (mDotY > mHeight - mBorder) {
99 mDotY = mHeight - mBorder;
101 mHSVO[2] = (mDotY - mBorder) / (mHeight - mBorder * 2);
102 notifyColorListeners(mHSVO);
104 invalidate((int) (ox - dotRadus), (int) (oy - dotRadus), (int) (ox + dotRadus),
105 (int) (oy + dotRadus));
106 invalidate((int) (mDotX - dotRadus), (int) (mDotY - dotRadus), (int) (mDotX + dotRadus),
107 (int) (mDotY + dotRadus));
112 private void setupButton() {
113 float pos = mHSVO[2] * (mHeight - mBorder * 2);
114 mDotY = pos + mBorder;
116 int[] colors3 = new int[] {
117 mSliderColor, mSliderColor, 0x66000000, 0 };
118 RadialGradient g = new RadialGradient(mDotX, mDotY, dotRadus, colors3, new float[] {
119 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP);
120 mDotPaint.setShader(g);
124 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
132 private void updatePaint() {
133 float[] hsv = new float[] {
134 mHSVO[0], mHSVO[1], 0f };
135 int color1 = Color.HSVToColor(hsv);
137 int color2 = Color.HSVToColor(hsv);
139 Shader sg = new LinearGradient(mBorder, mBorder, mBorder, mHeight - mBorder, color1, color2,
140 Shader.TileMode.CLAMP);
141 mBarPaint1.setShader(sg);
145 protected void onDraw(Canvas canvas) {
146 super.onDraw(canvas);
147 canvas.drawColor(mBgcolor);
148 canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mBarPaint1);
149 canvas.drawLine(mDotX, mDotY, mDotX, mHeight - mBorder, mLinePaint2);
150 canvas.drawLine(mDotX, mBorder, mDotX, mDotY, mLinePaint1);
151 if (mDotX != Float.NaN) {
152 canvas.drawCircle(mDotX, mDotY, dotRadus, mDotPaint);
157 public void setColor(float[] hsvo) {
158 System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length);
167 public void notifyColorListeners(float[] hsv) {
168 for (ColorListener l : mColorListeners) {
173 public void addColorListener(ColorListener l) {
174 mColorListeners.add(l);
177 public void removeColorListener(ColorListener l) {
178 mColorListeners.remove(l);