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 = ColorRectView.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 int[] colors = new int[16 * 16]; 83 for (int i = 0; i < colors.length; i++) { 84 int y = i / (16 * 8); 85 int x = (i / 8) % 2; 86 colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444; 87 } 88 Bitmap bitmap = Bitmap.createBitmap(colors, 16, 16, Bitmap.Config.ARGB_8888); 89 BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 90 mCheckPaint = new Paint(); 91 mCheckPaint.setShader(bs); 92 } 93 94 public boolean onDown(MotionEvent e) { 95 return true; 96 } 97 98 @Override 99 public boolean onTouchEvent(MotionEvent event) { 100 float ox = mDotX; 101 float oy = mDotY; 102 103 float x = event.getX(); 104 float y = event.getY(); 105 106 mDotX = x; 107 108 if (mDotX < mBorder) { 109 mDotX = mBorder; 110 } 111 112 if (mDotX > mWidth - mBorder) { 113 mDotX = mWidth - mBorder; 114 } 115 mHSVO[3] = (mDotX - mBorder) / (mWidth - mBorder * 2); 116 notifyColorListeners(mHSVO); 117 setupButton(); 118 invalidate((int) (ox - mDotRadius), (int) (oy - mDotRadius), (int) (ox + mDotRadius), 119 (int) (oy + mDotRadius)); 120 invalidate( 121 (int) (mDotX - mDotRadius), (int) (mDotY - mDotRadius), (int) (mDotX + mDotRadius), 122 (int) (mDotY + mDotRadius)); 123 124 return true; 125 } 126 127 private void setupButton() { 128 float pos = mHSVO[3] * (mWidth - mBorder * 2); 129 mDotX = pos + mBorder; 130 131 int[] colors3 = new int[] { 132 mSliderColor, mSliderColor, 0x66000000, 0 }; 133 RadialGradient g = new RadialGradient(mDotX, mDotY, mDotRadius, colors3, new float[] { 134 0, .3f, .31f, 1 }, Shader.TileMode.CLAMP); 135 mDotPaint.setShader(g); 136 } 137 138 @Override 139 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 140 mWidth = w; 141 mHeight = h; 142 mDotY = mHeight / 2; 143 updatePaint(); 144 setupButton(); 145 } 146 147 private void updatePaint() { 148 149 int color2 = Color.HSVToColor(mHSVO); 150 int color1 = color2 & 0xFFFFFF; 151 152 Shader sg = new LinearGradient( 153 mBorder, mBorder, mWidth - mBorder, mBorder, color1, color2, Shader.TileMode.CLAMP); 154 mBarPaint1.setShader(sg); 155 156 } 157 158 @Override 159 protected void onDraw(Canvas canvas) { 160 super.onDraw(canvas); 161 canvas.drawColor(mBgcolor); 162 canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mCheckPaint); 163 canvas.drawRect(mBorder, mBorder, mWidth - mBorder, mHeight - mBorder, mBarPaint1); 164 canvas.drawLine(mDotX, mDotY, mWidth - mBorder, mDotY, mLinePaint1); 165 canvas.drawLine(mBorder, mDotY, mDotX, mDotY, mLinePaint2); 166 if (mDotX != Float.NaN) { 167 canvas.drawCircle(mDotX, mDotY, mDotRadius, mDotPaint); 168 } 169 } 170 171 @Override 172 public void setColor(float[] hsv) { 173 System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length); 174 175 float oy = mDotY; 176 177 updatePaint(); 178 setupButton(); 179 invalidate(); 180 } 181 182 ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>(); 183 184 public void notifyColorListeners(float[] hsvo) { 185 for (ColorListener l : mColorListeners) { 186 l.setColor(hsvo); 187 } 188 } 189 190 public void addColorListener(ColorListener l) { 191 mColorListeners.add(l); 192 } 193 194 public void removeColorListener(ColorListener l) { 195 mColorListeners.remove(l); 196 } 197 } 198