Android自定义采颜色对话框

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

@SuppressLint({ “DrawAllocation” })
public class ColorPickerDialog extends Dialog {
private int mInitialColor;
private OnColorChangedListener mListener;

public ColorPickerDialog(Context paramContext,
        OnColorChangedListener paramOnColorChangedListener, int paramInt) {
    super(paramContext);
    this.mListener = paramOnColorChangedListener;
    this.mInitialColor = paramInt;
}

protected void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    mListener = new OnColorChangedListener() {
        public void colorChanged(int paramAnonymousInt) {
            ColorPickerDialog.this.mListener
                    .colorChanged(paramAnonymousInt);
            ColorPickerDialog.this.dismiss();
        }
    };
    setContentView(new ColorPickerView(getContext(), mListener,
            this.mInitialColor));
    setTitle("Pick a Color");
}

private static class ColorPickerView extends View {
    private static final int CENTER_RADIUS = 32;
    private static int CENTER_X = 120;
    private static int CENTER_Y = 120;
    private static final float PI = 3.1415925F;
    private Paint mCenterPaint;
    private final int[] mColors;
    private boolean mHighlightCenter;
    private ColorPickerDialog.OnColorChangedListener mListener;
    private Paint mPaint;
    private boolean mTrackingCenter;

    ColorPickerView(
            Context paramContext,
            ColorPickerDialog.OnColorChangedListener paramOnColorChangedListener,
            int paramInt) {
        super(paramContext);
        this.mListener = paramOnColorChangedListener;
        this.mColors = new int[] { -65536, -65281, -16776961, -16711681,
                -16711936, 65280, -65536 };
        SweepGradient gradient = new SweepGradient(0.0F, 0.0F,
                this.mColors, null);
        this.mPaint = new Paint(1);
        this.mPaint.setShader(gradient);
        this.mPaint.setStyle(Paint.Style.STROKE);
        this.mPaint.setStrokeWidth(32.0F);
        this.mCenterPaint = new Paint(1);
        this.mCenterPaint.setColor(paramInt);
        this.mCenterPaint.setStrokeWidth(5.0F);
    }

    private int ave(int paramInt1, int paramInt2, float paramFloat) {
        return Math.round((paramInt2 - paramInt1) * paramFloat) + paramInt1;
    }

    private int interpColor(int[] paramArrayOfInt, float paramFloat) {
        if (paramFloat <= 0.0F) {
            return paramArrayOfInt[0];
        }
        if (paramFloat >= 1.0F) {
            return paramArrayOfInt[(paramArrayOfInt.length - 1)];
        }
        paramFloat *= (paramArrayOfInt.length - 1);
        int j = (int) paramFloat;
        paramFloat -= j;
        int i = paramArrayOfInt[j];
        j = paramArrayOfInt[(j + 1)];
        return Color.argb(ave(Color.alpha(i), Color.alpha(j), paramFloat),
                ave(Color.red(i), Color.red(j), paramFloat),
                ave(Color.green(i), Color.green(j), paramFloat),
                ave(Color.blue(i), Color.blue(j), paramFloat));
    }

    protected void onDraw(Canvas paramCanvas) {
        float f = CENTER_X - this.mPaint.getStrokeWidth() * 0.5F;
        paramCanvas.translate(CENTER_X, CENTER_X);
        paramCanvas.drawOval(new RectF(-f, -f, f, f), this.mPaint);
        paramCanvas.drawCircle(0.0F, 0.0F, 32.0F, this.mCenterPaint);
        int i;
        if (this.mTrackingCenter) {
            i = this.mCenterPaint.getColor();
            this.mCenterPaint.setStyle(Paint.Style.STROKE);
            if (this.mHighlightCenter) {
                this.mCenterPaint.setAlpha(255);
            } else {
                this.mCenterPaint.setAlpha(128);
            }
            paramCanvas.drawCircle(0.0F, 0.0F,
                    this.mCenterPaint.getStrokeWidth() + 32.0F,
                    this.mCenterPaint);
            this.mCenterPaint.setStyle(Paint.Style.FILL);
            this.mCenterPaint.setColor(i);

        }
    }

    protected void onMeasure(int paramInt1, int paramInt2) {
        setMeasuredDimension(CENTER_X * 2, CENTER_X * 2);
    }

    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        float f1 = paramMotionEvent.getX() - CENTER_X;
        float f2 = paramMotionEvent.getY() - CENTER_Y;
        boolean bool;
        if (Math.sqrt(f1 * f1 + f2 * f2) <= 32.0D) {
            bool = true;
        } else {
            bool = false;
        }
        switch (paramMotionEvent.getAction()) {
        case 0:
            return true;

        case 1:

            if (mTrackingCenter) {
                if (mTrackingCenter != bool) {
                    this.mHighlightCenter = bool;
                    invalidate();
                    return true;
                } else {
                    f2 = (float) Math.atan2(f2, f1) / 6.283185F;
                    f1 = f2;
                    if (f2 < 0.0F) {
                        f1 = f2 + 1.0F;
                    }
                    this.mCenterPaint
                            .setColor(interpColor(this.mColors, f1));
                    invalidate();
                    return true;
                }
            }
            break;

        case 2:

            if (mTrackingCenter) {
                if (bool) {
                    this.mListener.colorChanged(this.mCenterPaint
                            .getColor());
                }
                this.mTrackingCenter = false;
                invalidate();
                return true;
            }
        }

        return false;
    }
}

public static abstract interface OnColorChangedListener {
    public abstract void colorChanged(int paramInt);
}

}

关注公众号“康波之道”回复“小程序”获取1000个小程序打包源码。回复“大数据”获取多本大数据电子书

发表评论

邮箱地址不会被公开。 必填项已用*标注