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 17package com.android.gallery3d.filtershow.colorpicker; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.graphics.Bitmap; 22import android.graphics.BitmapShader; 23import android.graphics.Canvas; 24import android.graphics.Color; 25import android.graphics.Paint; 26import android.graphics.Path; 27import android.graphics.Shader; 28import android.util.AttributeSet; 29import android.util.DisplayMetrics; 30import android.view.MotionEvent; 31import android.view.View; 32 33import com.android.gallery3d.R; 34 35import java.util.ArrayList; 36 37public class ColorCompareView extends View implements ColorListener { 38 39 private float mRadius; 40 private float mWidth; 41 private Paint mBarPaint1; 42 private Paint mOrigBarPaint1; 43 private Paint mCheckPaint; 44 45 private float mHeight; 46 47 private int mBgcolor = 0; 48 49 private float mBorder; 50 51 private float[] mHSVO = new float[4]; 52 private float[] mOrigHSVO = new float[4]; 53 private Path mRegion; 54 private Path mOrigRegion; 55 56 public final static float BORDER_SIZE = 0; 57 private int mCheckDim = 8; 58 59 public ColorCompareView(Context ctx, AttributeSet attrs) { 60 super(ctx, attrs); 61 DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); 62 float mDpToPix = metrics.density; 63 mBorder = BORDER_SIZE * mDpToPix; 64 mBarPaint1 = new Paint(); 65 mOrigBarPaint1 = new Paint(); 66 Resources res = ctx.getResources(); 67 mCheckDim = res.getDimensionPixelSize(R.dimen.draw_color_check_dim); 68 mBarPaint1.setStyle(Paint.Style.FILL); 69 mOrigBarPaint1.setStyle(Paint.Style.FILL); 70 71 makeCheckPaint(); 72 } 73 74 private void makeCheckPaint() { 75 int imgdim = mCheckDim * 2; 76 int[] colors = new int[imgdim * imgdim]; 77 for (int i = 0; i < colors.length; i++) { 78 int y = i / (imgdim * mCheckDim); 79 int x = (i / mCheckDim) % 2; 80 colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444; 81 } 82 Bitmap bitmap = Bitmap.createBitmap(colors, imgdim, imgdim, Bitmap.Config.ARGB_8888); 83 BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 84 mCheckPaint = new Paint(); 85 mCheckPaint.setShader(bs); 86 } 87 88 public boolean onDown(MotionEvent e) { 89 return true; 90 } 91 92 @Override 93 public boolean onTouchEvent(MotionEvent event) { 94 if (event.getAction() != MotionEvent.ACTION_UP) { 95 return true; 96 } 97 float x = event.getX(); 98 float y = event.getY(); 99 if (x> mWidth-2*mHeight) { 100 resetToOriginal(); 101 } 102 return true; 103 } 104 105 public void resetToOriginal(){ 106 System.arraycopy(mOrigHSVO, 0, mHSVO, 0, mOrigHSVO.length); 107 updatePaint(); 108 notifyColorListeners(mHSVO); 109 invalidate(); 110 } 111 112 @Override 113 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 114 mWidth = w; 115 mHeight = h; 116 updatePaint(); 117 } 118 119 private void updatePaint() { 120 int color = Color.HSVToColor((int) (mHSVO[3] * 255), mHSVO); 121 mBarPaint1.setColor(color); 122 int origColor = Color.HSVToColor((int) (mOrigHSVO[3] * 255), mOrigHSVO); 123 mOrigBarPaint1.setColor(origColor); 124 mOrigRegion = new Path(); 125 mOrigRegion.moveTo(mWidth, 0); 126 mOrigRegion.lineTo(mWidth, mHeight); 127 mOrigRegion.lineTo(mWidth - mHeight * 2, mHeight); 128 mOrigRegion.lineTo(mWidth - mHeight, 0); 129 130 mRegion = new Path(); 131 mRegion.moveTo(0, 0); 132 mRegion.lineTo(mWidth - mHeight, 0); 133 mRegion.lineTo(mWidth - mHeight * 2, mHeight); 134 mRegion.lineTo(0, mHeight); 135 136 } 137 138 @Override 139 protected void onDraw(Canvas canvas) { 140 super.onDraw(canvas); 141 canvas.drawColor(mBgcolor); 142 canvas.drawRect(mBorder, 0, mWidth, mHeight, mCheckPaint); 143 canvas.drawPath(mRegion, mBarPaint1); 144 canvas.drawPath(mOrigRegion, mOrigBarPaint1); 145 } 146 147 public void setOrigColor(float[] hsv) { 148 System.arraycopy(hsv, 0, mOrigHSVO, 0, mOrigHSVO.length); 149 int color2 = Color.HSVToColor((int) (mOrigHSVO[3] * 255), mOrigHSVO); 150 mOrigBarPaint1.setColor(color2); 151 updatePaint(); 152 } 153 154 @Override 155 public void setColor(float[] hsv) { 156 System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length); 157 updatePaint(); 158 invalidate(); 159 } 160 161 ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>(); 162 163 public void notifyColorListeners(float[] hsvo) { 164 for (ColorListener l : mColorListeners) { 165 l.setColor(hsvo); 166 } 167 } 168 169 public void addColorListener(ColorListener l) { 170 mColorListeners.add(l); 171 } 172 173 public void removeColorListener(ColorListener l) { 174 mColorListeners.remove(l); 175 } 176} 177 178