OSDN Git Service

bbfd829b306465ae5b6243a9087d733cfb6a8225
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / colorpicker / ColorValueView.java
1 /*
2  * Copyright (C) 2013 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.filtershow.colorpicker;
18
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;
30
31 import com.android.gallery3d.R;
32
33 import java.util.ArrayList;
34
35 public class ColorValueView extends View implements ColorListener {
36
37     private float mRadius;
38     private float mWidth;
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;
47
48     private float[] mHSVO = new float[4];
49     private int mSliderColor;
50     private float mDotX;
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;
54
55     private ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
56
57     public ColorValueView(Context ctx, AttributeSet attrs) {
58         super(ctx, attrs);
59         DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
60         float mDpToPix = metrics.density;
61         dotRadus = DOT_SIZE * mDpToPix;
62         mBorder = BORDER_SIZE * mDpToPix;
63
64         mBarPaint1 = new Paint();
65
66         mDotPaint = new Paint();
67
68         mDotPaint.setStyle(Paint.Style.FILL);
69         mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
70
71         mBarPaint1.setStyle(Paint.Style.FILL);
72
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);
79     }
80
81     public boolean onDown(MotionEvent e) {
82         return true;
83     }
84
85     public boolean onTouchEvent(MotionEvent event) {
86         float ox = mDotX;
87         float oy = mDotY;
88
89         float x = event.getX();
90         float y = event.getY();
91
92         mDotY = y;
93
94         if (mDotY < mBorder) {
95             mDotY = mBorder;
96         }
97
98         if (mDotY > mHeight - mBorder) {
99             mDotY = mHeight - mBorder;
100         }
101         mHSVO[2] = (mDotY - mBorder) / (mHeight - mBorder * 2);
102         notifyColorListeners(mHSVO);
103         setupButton();
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));
108
109         return true;
110     }
111
112     private void setupButton() {
113         float pos = mHSVO[2] * (mHeight - mBorder * 2);
114         mDotY = pos + mBorder;
115
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);
121     }
122
123     @Override
124     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
125         mWidth = w;
126         mHeight = h;
127         mDotX = mWidth / 2;
128         updatePaint();
129         setupButton();
130     }
131
132     private void updatePaint() {
133         float[] hsv = new float[] {
134                 mHSVO[0], mHSVO[1], 0f };
135         int color1 = Color.HSVToColor(hsv);
136         hsv[2] = 1;
137         int color2 = Color.HSVToColor(hsv);
138
139         Shader sg = new LinearGradient(mBorder, mBorder, mBorder, mHeight - mBorder, color1, color2,
140                 Shader.TileMode.CLAMP);
141         mBarPaint1.setShader(sg);
142     }
143
144     @Override
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);
153         }
154     }
155
156     @Override
157     public void setColor(float[] hsvo) {
158         System.arraycopy(hsvo, 0, mHSVO, 0, mHSVO.length);
159
160         float oy = mDotY;
161         updatePaint();
162         setupButton();
163         invalidate();
164
165     }
166
167     public void notifyColorListeners(float[] hsv) {
168         for (ColorListener l : mColorListeners) {
169             l.setColor(hsv);
170         }
171     }
172
173     public void addColorListener(ColorListener l) {
174         mColorListeners.add(l);
175     }
176
177     public void removeColorListener(ColorListener l) {
178         mColorListeners.remove(l);
179     }
180 }