Back to project page lamp.
The source code is released under:
GNU General Public License
If you think the Android project lamp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * Copyright (C) 2007 The Android Open Source Project */*from ww w. j a v a2s. c om*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package cz.tomsuch.lampicka.util; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.SweepGradient; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import cz.tomsuch.lampicka.interfaces.OnColorChangedListener; public class ColorPickerView extends View { private Paint mPaint; private Paint mCenterPaint; private final int[] mColors; private OnColorChangedListener mListener; /** * Dummy listener interface, to overcome possible NPE on wrong usage of * class * */ private static final OnColorChangedListener dummyListener = new OnColorChangedListener() { @Override public void colorChanged(int color, boolean up, boolean center) { } }; public ColorPickerView(Context c, AttributeSet attr) { this(c, attr, dummyListener, Color.BLACK); } public ColorPickerView(Context c) { this(c, null, dummyListener, Color.BLACK); } public ColorPickerView(Context c, AttributeSet attr, OnColorChangedListener l, int color) { super(c, attr); mListener = l; mColors = new int[] { 0xFF000000, 0xFFFFFFFF, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000, 0xFF000000 }; Shader s = new SweepGradient(0, 0, mColors, null); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setShader(s); mPaint.setStyle(Paint.Style.STROKE); mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mCenterPaint.setColor(color); mCenterPaint.setStrokeWidth(5); } private boolean mTrackingCenter; private boolean mShouldChangeContinually = false; @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { mPaint.setStrokeWidth(_getWidth() / 2); CENTER_RADIUS = _getWidth() / 4; float r = _getWidth() - mPaint.getStrokeWidth() * 0.7f; canvas.translate(_getWidth(), _getHeight()); canvas.drawOval(new RectF(-r, -r, r, r), mPaint); canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint); if (mTrackingCenter) { int c = mCenterPaint.getColor(); mCenterPaint.setStyle(Paint.Style.STROKE); mCenterPaint.setAlpha(0x80); canvas.drawCircle(0, 0, CENTER_RADIUS + mCenterPaint.getStrokeWidth(), mCenterPaint); mCenterPaint.setStyle(Paint.Style.FILL); mCenterPaint.setColor(c); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, widthMeasureSpec); } public void setColor(int color) { mCenterPaint.setColor(color); invalidate(); } public int getColor() { return mCenterPaint.getColor(); } public void setOnColorChangedListener(OnColorChangedListener listener) { mListener = listener; } private int CENTER_RADIUS = 64; private int ave(int s, int d, float p) { return s + java.lang.Math.round(p * (d - s)); } private int interpColor(int colors[], float unit) { if (unit <= 0) { return colors[0]; } if (unit >= 1) { return colors[colors.length - 1]; } float p = unit * (colors.length - 1); int i = (int) p; p -= i; // now p is just the fractional part [0...1) and i is the index int c0 = colors[i]; int c1 = colors[i + 1]; int a = ave(Color.alpha(c0), Color.alpha(c1), p); int r = ave(Color.red(c0), Color.red(c1), p); int g = ave(Color.green(c0), Color.green(c1), p); int b = ave(Color.blue(c0), Color.blue(c1), p); return Color.argb(a, r, g, b); } private static final float PI = 3.1415926f; private int _getWidth() { return getWidth() / 2; } private int _getHeight() { return getHeight() / 2; } @Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) return false; if (getParent() != null) getParent().requestDisallowInterceptTouchEvent(true); float x = event.getX() - _getWidth(); float y = event.getY() - _getHeight(); boolean inCenter = java.lang.Math.sqrt(x * x + y * y) <= CENTER_RADIUS; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mTrackingCenter = inCenter; case MotionEvent.ACTION_MOVE: if (!mTrackingCenter) { float angle = (float) java.lang.Math.atan2(y, x); // need to turn angle [-PI ... PI] into unit [0....1] float unit = angle / (2 * PI); if (unit < 0) { unit += 1; } mCenterPaint.setColor(interpColor(mColors, unit)); if (mShouldChangeContinually) { mListener.colorChanged(mCenterPaint.getColor(), false, false); } invalidate(); } break; case MotionEvent.ACTION_UP: mListener.colorChanged(mCenterPaint.getColor(), true, inCenter); if (mTrackingCenter) { mTrackingCenter = false; // so we draw w/o halo invalidate(); } break; } return true; } public void setChangeColorContinually(boolean should) { mShouldChangeContinually = should; } }