OSDN Git Service

am 9095869c: am 87f067fd: am 708569b4: Merge "fix draw issues on tablet" into gb...
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / colorpicker / ColorOpacityView.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.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;
32
33 import com.android.gallery3d.R;
34
35 import java.util.ArrayList;
36
37 public class ColorOpacityView extends View implements ColorListener {
38
39     private float mRadius;
40     private float mWidth;
41     private Paint mBarPaint1;
42     private Paint mLinePaint1;
43     private Paint mLinePaint2;
44     private Paint mCheckPaint;
45
46     private float mHeight;
47     private Paint mDotPaint;
48     private int mBgcolor = 0;
49
50     private float mDotRadius;
51     private float mBorder;
52
53     private float[] mHSVO = new float[4];
54     private int mSliderColor;
55     private float mDotX = mBorder;
56     private float mDotY = mBorder;
57     private final static float DOT_SIZE = ColorHueView.DOT_SIZE;
58     public final static float BORDER_SIZE = 20;;
59
60     public ColorOpacityView(Context ctx, AttributeSet attrs) {
61         super(ctx, attrs);
62         DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
63         float mDpToPix = metrics.density;
64         mDotRadius = DOT_SIZE * mDpToPix;
65         mBorder = BORDER_SIZE * mDpToPix;
66         mBarPaint1 = new Paint();
67
68         mDotPaint = new Paint();
69
70         mDotPaint.setStyle(Paint.Style.FILL);
71         mDotPaint.setColor(ctx.getResources().getColor(R.color.slider_dot_color));
72         mSliderColor = ctx.getResources().getColor(R.color.slider_line_color);
73
74         mBarPaint1.setStyle(Paint.Style.FILL);
75
76         mLinePaint1 = new Paint();
77         mLinePaint1.setColor(Color.GRAY);
78         mLinePaint2 = new Paint();
79         mLinePaint2.setColor(mSliderColor);
80         mLinePaint2.setStrokeWidth(4);
81
82         makeCheckPaint();
83     }
84
85     private void makeCheckPaint(){
86         int[] colors = new int[16 * 16];
87         for (int i = 0; i < colors.length; i++) {
88             int y = i / (16 * 8);
89             int x = (i / 8) % 2;
90             colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444;
91         }
92         Bitmap bitmap = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.ARGB_8888);
93         BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
94         mCheckPaint = new Paint();
95         mCheckPaint.setShader(bs);
96     }
97
98     public boolean onDown(MotionEvent e) {
99         return true;
100     }
101
102     @Override
103     public boolean onTouchEvent(MotionEvent event) {
104         float ox = mDotX;
105         float oy = mDotY;
106
107         float x = event.getX();
108         float y = event.getY();
109
110         mDotX = x;
111
112         if (mDotX < mBorder) {
113             mDotX = mBorder;
114         }
115
116         if (mDotX > mWidth - mBorder) {
117             mDotX = mWidth - mBorder;
118         }
119         mHSVO[3] = (mDotX - mBorder) / (mWidth - mBorder * 2);
120         notifyColorListeners(mHSVO);
121         setupButton();
122         invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius),
123                 (int) (oy + mDotRadius));
124         invalidate(
125                 (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius),
126                 (int) (mDotY + mDotRadius));
127
128         return true;
129     }
130
131     private void setupButton() {
132         float pos = mHSVO[3] * (mWidth - mBorder * 2);
133         mDotX = pos + mBorder;
134
135         int[] colors3 = new int[] {
136                 mSliderColor, mSliderColor, 0x66000000, 0 };
137         RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadius, colors3, new float[] {
138                 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP);
139         mDotPaint.setShader(g);
140     }
141
142     @Override
143     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
144         mWidth = w;
145         mHeight = h;
146         mDotY = mHeight / 2;
147         updatePaint();
148         setupButton();
149     }
150
151     private void updatePaint() {
152
153         int color2 = Color.HSVToColor(mHSVO);
154         int color1 = color2 & 0xFFFFFF;
155
156         Shader sg = new LinearGradient(
157                 mBorder, mBorder, mWidth - mBorder, mBorder, color1, color2, Shader.TileMode.CLAMP);
158         mBarPaint1.setShader(sg);
159
160     }
161
162     @Override
163     protected void onDraw(Canvas canvas) {
164         super.onDraw(canvas);
165         canvas.drawColor(mBgcolor);
166         canvas.drawRect(mBorder, 0, mWidth - mBorder, mHeight, mCheckPaint);
167         canvas.drawRect(mBorder, 0, mWidth - mBorder, mHeight, mBarPaint1);
168         canvas.drawLine(mDotX, mDotY, mWidth - mBorder, mDotY, mLinePaint1);
169         canvas.drawLine(mBorder, mDotY, mDotX, mDotY, mLinePaint2);
170         if (mDotX != Float.NaN) {
171             canvas.drawCircle(mDotX, mDotY, mDotRadius, mDotPaint);
172         }
173     }
174
175     @Override
176     public void setColor(float[] hsv) {
177         System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length);
178
179         updatePaint();
180         setupButton();
181         invalidate();
182     }
183
184     ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
185
186     public void notifyColorListeners(float[] hsvo) {
187         for (ColorListener l : mColorListeners) {
188             l.setColor(hsvo);
189         }
190     }
191
192     public void addColorListener(ColorListener l) {
193         mColorListeners.add(l);
194     }
195
196     public void removeColorListener(ColorListener l) {
197         mColorListeners.remove(l);
198     }
199 }