@Override
  protected void onPrepareDialogBuilder(Builder builder) {
    super.onPrepareDialogBuilder(builder);
    OnColorChangedListener l =
        new OnColorChangedListener() {
          public void colorChanged(int color) {
            mCurrentColor = color;
            onDialogClosed(true);
            getDialog().dismiss();
          }
        };

    LinearLayout layout = new LinearLayout(getContext());
    layout.setPadding(20, 20, 20, 20);
    layout.setOrientation(LinearLayout.VERTICAL);
    mCPView = new ColorPickerView(getContext(), l, mCurrentColor);

    LinearLayout.LayoutParams params1 =
        new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    params1.gravity = Gravity.CENTER;
    mCPView.setLayoutParams(params1);
    layout.addView(this.mCPView);
    layout.setId(android.R.id.widget_frame);
    builder.setView(layout);
  }
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
      // 移动中心
      canvas.translate(mWidth / 2 - 50, mHeight / 2);
      // 画中心圆
      canvas.drawCircle(0, 0, mCenterRadius, mCenterPaint);
      // 是否显示中心圆外的小圆环
      if (mHighlightCenter || mlittleLightCenter) {
        int c = mCenterPaint.getColor();
        mCenterPaint.setStyle(Paint.Style.STROKE);
        if (mHighlightCenter) {
          mCenterPaint.setAlpha(0xFF);
        } else if (mlittleLightCenter) {
          mCenterPaint.setAlpha(0x90);
        }
        canvas.drawCircle(0, 0, mCenterRadius + mCenterPaint.getStrokeWidth(), mCenterPaint);

        mCenterPaint.setStyle(Paint.Style.FILL);
        mCenterPaint.setColor(c);
      }
      // 画色环
      canvas.drawOval(
          new RectF(-mCircleRadius, -mCircleRadius, mCircleRadius, mCircleRadius), mCirclePaint);
      // 画黑白渐变块
      if (mDownInCircle) {
        mRectColors[1] = mCenterPaint.getColor();
      }
      mRectShader =
          new LinearGradient(
              0, mRectTop, 0, mRectBottom, mRectColors, null, Shader.TileMode.MIRROR);
      mRectPaint.setShader(mRectShader);
      canvas.drawRect(mRectLeft, mRectTop, mRectRight, mRectBottom, mRectPaint);
      float offset = mLinePaint.getStrokeWidth() / 2;
      canvas.drawLine(
          mRectLeft - offset,
          mRectTop - offset * 2,
          mRectLeft - offset,
          mRectBottom + offset * 2,
          mLinePaint); // 左
      canvas.drawLine(
          mRectLeft - offset * 2,
          mRectTop - offset,
          mRectRight + offset * 2,
          mRectTop - offset,
          mLinePaint); // 上
      canvas.drawLine(
          mRectRight + offset,
          mRectTop - offset * 2,
          mRectRight + offset,
          mRectBottom + offset * 2,
          mLinePaint); // 右
      canvas.drawLine(
          mRectLeft - offset * 2,
          mRectBottom + offset,
          mRectRight + offset * 2,
          mRectBottom + offset,
          mLinePaint); // 下
      super.onDraw(canvas);
    }
Ejemplo n.º 3
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    colorPickerDisk = new ColorPickerView(this);
    colorstring = "FF3030";
    // find the ID of View
    colorPickerDisk = (ColorPickerView) findViewById(R.id.colorPickerDisk);
    text_ColorDisk = (TextView) findViewById(R.id.text_ColorDisk);
    color_bg = (TextView) findViewById(R.id.color_bg);
    seek_bar = (SeekBar) findViewById(R.id.seek_bar);
    colorPickerDisk.setOnColorChangedListennerD(
        new ColorPickerView.OnColorChangedListenerD() {
          // 获取颜色值和RGB值(六位16进制数)
          @Override
          public void onColorChanged(int color, String hexStrColor) {
            // TODO Auto-generated method stub
            text_ColorDisk.setBackgroundColor(color);
            color_bg.setBackgroundColor(color);
            text_ColorDisk.setText("Color is " + hexStrColor);
            colorstring = hexStrColor;
            seek_bar.setProgress(0xff);
          }
        });
    seek_bar.setOnSeekBarChangeListener(
        new SeekBar.OnSeekBarChangeListener() {

          @Override
          public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

          }

          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

          }
          // 触发操作,拖动
          @Override
          public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            // 将十进制转为十六进制
            if (progress > 15) {
              transparence = Integer.toHexString(progress);
            } else {
              transparence = "0" + Integer.toHexString(progress);
            }
            text_ColorDisk.setText("transparency is :" + transparence);
            String color = "#" + transparence + colorstring;
            if (color.length() == 9) {
              text_ColorDisk.setBackgroundColor(Color.parseColor(color));
            }
          }
        });
  }
 @Override
 protected void onDialogClosed(boolean positiveResult) {
   if (positiveResult) {
     mCurrentColor = mCPView.getColor();
     summary.setTextColor(mCurrentColor);
     SharedPreferences.Editor editor = getEditor();
     editor.putInt(getKey(), mCurrentColor);
     editor.commit();
     callChangeListener(new Integer(mCurrentColor));
     NotePadPlus.SysSettings.BgClr = mCurrentColor;
   }
 }