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.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapShader;
23 import android.graphics.Canvas;
24 import android.graphics.Color;
25 import android.graphics.LinearGradient;
26 import android.graphics.Paint;
27 import android.graphics.RadialGradient;
28 import android.graphics.Shader;
29 import android.util.AttributeSet;
30 import android.util.DisplayMetrics;
31 import android.view.MotionEvent;
32 import android.view.View;
34 import com.android.gallery3d.R;
36 import java.util.ArrayList;
38 public class ColorOpacityView 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 = ColorHueView.DOT_SIZE;
59 public final static float BORDER_SIZE = 20;;
60 private int mCheckDim = 8;
61 public ColorOpacityView(Context ctx, AttributeSet attrs) {
63 DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
64 float mDpToPix = metrics.density;
65 mDotRadius = DOT_SIZE * mDpToPix;
66 mBorder = BORDER_SIZE * mDpToPix;
67 mBarPaint1 = new Paint();
69 mDotPaint = new Paint();
71 mDotPaint.setStyle(Paint.Style.FILL);
72 Resources res = ctx.getResources();
73 mCheckDim = res.getDimensionPixelSize(R.dimen.draw_color_check_dim);
74 mDotPaint.setColor(res.getColor(R.color.slider_dot_color));
75 mSliderColor = res.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);
88 private void makeCheckPaint(){
89 int imgdim = mCheckDim*2;
90 int[] colors = new int[imgdim * imgdim];
91 for (int i = 0; i < colors.length; i++) {
92 int y = i / (imgdim * mCheckDim);
93 int x = (i / mCheckDim) % 2;
94 colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444;
96 Bitmap bitmap = Bitmap.createBitmap(colors, imgdim, imgdim, Bitmap.Config.ARGB_8888);
97 BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
98 mCheckPaint = new Paint();
99 mCheckPaint.setShader(bs);
102 public boolean onDown(MotionEvent e) {
107 public boolean onTouchEvent(MotionEvent event) {
111 float x = event.getX();
112 float y = event.getY();
116 if (mDotX < mBorder) {
120 if (mDotX > mWidth - mBorder) {
121 mDotX = mWidth - mBorder;
123 mHSVO[3] = (mDotX - mBorder) / (mWidth - mBorder * 2);
124 notifyColorListeners(mHSVO);
126 invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius),
127 (int) (oy + mDotRadius));
129 (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius),
130 (int) (mDotY + mDotRadius));
135 private void setupButton() {
136 float pos = mHSVO[3] * (mWidth - mBorder * 2);
137 mDotX = pos + mBorder;
139 int[] colors3 = new int[] {
140 mSliderColor, mSliderColor, 0x66000000, 0 };
141 RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadius, colors3, new float[] {
142 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP);
143 mDotPaint.setShader(g);
147 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
155 private void updatePaint() {
157 int color2 = Color.HSVToColor(mHSVO);
158 int color1 = color2 & 0xFFFFFF;
160 Shader sg = new LinearGradient(
161 mBorder, mBorder, mWidth - mBorder, mBorder, color1, color2, Shader.TileMode.CLAMP);
162 mBarPaint1.setShader(sg);
167 protected void onDraw(Canvas canvas) {
168 super.onDraw(canvas);
169 canvas.drawColor(mBgcolor);
170 canvas.drawRect(mBorder, 0, mWidth - mBorder, mHeight, mCheckPaint);
171 canvas.drawRect(mBorder, 0, mWidth - mBorder, mHeight, mBarPaint1);
172 canvas.drawLine(mDotX, mDotY, mWidth - mBorder, mDotY, mLinePaint1);
173 canvas.drawLine(mBorder, mDotY, mDotX, mDotY, mLinePaint2);
174 if (!Float.isNaN(mDotX)) {
175 canvas.drawCircle(mDotX, mDotY, mDotRadius, mDotPaint);
180 public void setColor(float[] hsv) {
181 System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length);
188 ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>();
190 public void notifyColorListeners(float[] hsvo) {
191 for (ColorListener l : mColorListeners) {
196 public void addColorListener(ColorListener l) {
197 mColorListeners.add(l);
200 public void removeColorListener(ColorListener l) {
201 mColorListeners.remove(l);