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;
37 public class ColorOpacityView extends View implements ColorListener {
39 private float mRadius;
41 private Paint mBarPaint1;
42 private Paint mLinePaint1;
43 private Paint mLinePaint2;
44 private Paint mCheckPaint;
46 private float mHeight;
47 private Paint mDotPaint;
48 private int mBgcolor = 0;
50 private float mDotRadius;
51 private float mBorder;
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;;
60 public ColorOpacityView(Context ctx, AttributeSet 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();
68 mDotPaint = new Paint();
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);
74 mBarPaint1.setStyle(Paint.Style.FILL);
76 mLinePaint1 = new Paint();
77 mLinePaint1.setColor(Color.GRAY);
78 mLinePaint2 = new Paint();
79 mLinePaint2.setColor(mSliderColor);
80 mLinePaint2.setStrokeWidth(4);
85 private void makeCheckPaint(){
86 int[] colors = new int[16 * 16];
87 for (int i = 0; i < colors.length; i++) {
90 colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444;
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);
98 public boolean onDown(MotionEvent e) {
103 public boolean onTouchEvent(MotionEvent event) {
107 float x = event.getX();
108 float y = event.getY();
112 if (mDotX < mBorder) {
116 if (mDotX > mWidth - mBorder) {
117 mDotX = mWidth - mBorder;
119 mHSVO[3] = (mDotX - mBorder) / (mWidth - mBorder * 2);
120 notifyColorListeners(mHSVO);
122 invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius),
123 (int) (oy + mDotRadius));
125 (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius),
126 (int) (mDotY + mDotRadius));
131 private void setupButton() {
132 float pos = mHSVO[3] * (mWidth - mBorder * 2);
133 mDotX = pos + mBorder;
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);
143 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
151 private void updatePaint() {
153 int color2 = Color.HSVToColor(mHSVO);
154 int color1 = color2 & 0xFFFFFF;
156 Shader sg = new LinearGradient(
157 mBorder, mBorder, mWidth - mBorder, mBorder, color1, color2, Shader.TileMode.CLAMP);
158 mBarPaint1.setShader(sg);
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);
176 public void setColor(float[] hsv) {
177 System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length);
184 ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
186 public void notifyColorListeners(float[] hsvo) {
187 for (ColorListener l : mColorListeners) {
192 public void addColorListener(ColorListener l) {
193 mColorListeners.add(l);
196 public void removeColorListener(ColorListener l) {
197 mColorListeners.remove(l);