我用网上的ColorPickerDialog控件改成了View控件,希望把ColorPicker控件直接嵌入到layout内,在layout设计界面可以看到View已经显示了,但在MainActivity内使用SetContentView调用后就报错,请大侠帮我查下哪里写得不对;
package com.example.colorpicker;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.view.View;
public class ColorPickerView extends View {
private Paint mPaint;//
private Paint mCenterPaint;//
private Paint mLinePaint;//
private Paint mRectPaint;//
private Shader rectShader;//
private float rectLeft;//
private float rectTop;//
private float rectRight;//
private float rectBottom;//
private final boolean debug = true;
private final String TAG = "ColorPicker";
Context context;
private String title;//
private int mInitialColor;//
// private OnColorChangedListener mListener;
private final int[] mCircleColors;//
private final int[] mRectColors;//
private int mHeight;//
private int mWidth;//
private float r;//
private float centerRadius;//
private boolean downInCircle = true;//
private boolean downInRect;//
private boolean highlightCenter;//
private boolean highlightCenterLittle;//
// public ColorPickerView(Context context, int height, int width) {
public ColorPickerView(Context context) {
super(context);
int height = 400;
int width = 500;
this.mHeight = height - 36;
this.mWidth = width;
setMinimumHeight(height - 36);
setMinimumWidth(width);
mCircleColors = new int[] {0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,
0xFF00FFFF, 0xFF00FF00,0xFFFFFF00, 0xFFFF0000};
Shader s = new SweepGradient(0, 0, mCircleColors, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setShader(s);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(50);
r = width / 2 * 0.7f - mPaint.getStrokeWidth() * 0.5f;
mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCenterPaint.setColor(mInitialColor);
mCenterPaint.setStrokeWidth(5);
centerRadius = (r - mPaint.getStrokeWidth() / 2 ) * 0.7f;
mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLinePaint.setColor(Color.parseColor("#72A1D1"));
mLinePaint.setStrokeWidth(4);
mRectColors = new int[]{0xFF000000, mCenterPaint.getColor(), 0xFFFFFFFF};
mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRectPaint.setStrokeWidth(5);
rectLeft = -r - mPaint.getStrokeWidth() * 0.5f;
rectTop = r + mPaint.getStrokeWidth() * 0.5f +
mLinePaint.getStrokeMiter() * 0.5f + 15;
rectRight = r + mPaint.getStrokeWidth() * 0.5f;
rectBottom = rectTop + 50;
}
private void init()
{
}
@Override
protected void onDraw(Canvas canvas) {
canvas.translate(mWidth / 2, mHeight / 2 - 50);
canvas.drawCircle(0, 0, centerRadius, mCenterPaint);
if (highlightCenter || highlightCenterLittle) {
int c = mCenterPaint.getColor();
mCenterPaint.setStyle(Paint.Style.STROKE);
if(highlightCenter) {