当按钮点击第二次颜色piker监听器不工作android?

时间:2017-06-22 19:41:19

标签: android

这里的人是我使用颜色选择器绘制的代码。但是,当我第一次点击它工作正常但当我试图再次点击时,颜色选择器监听器不工作?

@Override
public void onClick(View v) {
    mDrawingPad.setVisibility(View.VISIBLE);
    BitmapDrawable ob = new BitmapDrawable(getResources(),
            bitmapconv);
    mDrawingView = new DrawingView(Previewimage.this);
    colorPicker.setOnColorChangeListener(new VerticalSlideColorPicker.OnColorChangeListener() {
        @Override
        public void onColorChange(int selectedColor) {
            mDrawingView.mPaint.setColor(selectedColor);
        }
    });


    mDrawingPad.addView(mDrawingView);
    mDrawingView.setBackground(ob);
    Toast.makeText(Previewimage.this, "clicked", Toast.LENGTH_SHORT).show();
}

这是Draw视图类

    class DrawingView extends View  {
     Paint mPaint;

     Bitmap mBitmap;
     Canvas mCanvas;
     Path mPath;
     float x,y;
Paint mBitmapPaint;

public DrawingView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFFFF0000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(20);

    mPath = new Path();
    mBitmapPaint = new Paint();
    /*mBitmapPaint.setColor(Color.RED);*/
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

 if (w==0)
{
w=1080;
}
    if (mBitmap==null)
    {
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    }
    else
    {
      h=  mBitmap.getHeight();
        w=mBitmap.getWidth();
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    }
   if (mCanvas==null)

   {
       mCanvas = new Canvas(mBitmap);
      }
  else
  {
mCanvas=null;
mCanvas = new Canvas(mBitmap);
   }

}

@Override
public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.draw(canvas);
    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
    canvas.drawPath(mPath, mPaint);
    canvas.drawCircle(x,y, (float) 0.2,mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
    //mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
}

private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
        mX = x;
        mY = y;
    }
}

private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN));
    // kill this so we don't double draw
    mPath.reset();
    // mPath= new Path();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
  x = event.getX();
    y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
}

}

0 个答案:

没有答案