当我改变任何关于油漆的东西时,它会改变所有的油漆,甚至现有的油漆

时间:2015-06-27 20:39:11

标签: java android canvas

所以我能够很好地改变颜色和画笔厚度,但问题是每次我更改它时,它也会改变画布上现有的颜色。我理解我的问题,就是我要把画布改为油漆本身(我很确定),但问题是我不知道如何解决它。

这是我的Java(画布)代码:

public class CanvasView extends View
{
private Paint paint = new Paint();
private Path path = new Path();
private Canvas canvas = new Canvas();
private Paint canvasPaint = new Paint();
private float brushSize, pixelAmount;
private Bitmap canvasBitmap;

public CanvasView(Context context, AttributeSet attrs)
{
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(5);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(canvasBitmap);
}

@Override
protected void onDraw(Canvas drawCanvas)
{
    drawCanvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    drawCanvas.drawPath(path, paint);
}

@Override
public boolean onTouchEvent(MotionEvent e)
{
    // get the coords of the touch event
    float eventX = e.getX();
    float eventY = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // set a new starting point
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            // connect the points
            path.lineTo(eventX, eventY);
            break;
        default:
            return false;
    }

    // makes you view repaint and call ondraw
    invalidate();
    return true;
}

public void clearCanvas()
{
    path.reset();
    invalidate();
}

public void setStrokeWidth(float f)
{
    pixelAmount = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, f, getResources().getDisplayMetrics());
    brushSize = pixelAmount;
    paint.setStrokeWidth(brushSize);
    invalidate();
}

public void setColor(int p)
{
    invalidate();
    paint.setColor(p);
}

}

这是我的MainActivity代码:

public class MainActivity extends Activity {

private CanvasView canvasView;
private int orange;
private int purple;
private float strokeWidth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    canvasView = (CanvasView) findViewById(R.id.canvasView);
    orange = Color.rgb(250, 128, 0);
    purple = Color.rgb(128, 0, 128);
    strokeWidth = 0;

    // REGULAR BUTTONS: save, about, reset
    Button saveB = (Button) findViewById(R.id.saveButton);
    Button aboutB = (Button) findViewById(R.id.aboutButton);
    Button resetB = (Button) findViewById(R.id.resetButton);

    // IMAGE BUTTONS: red, blue, green, yellow, black, purple, orange, erase, brush thickness plus, brush thickness minus
    ImageButton redIb = (ImageButton) findViewById(R.id.redButton);
    ImageButton blueIb = (ImageButton) findViewById(R.id.blueButton);
    ImageButton greenIb = (ImageButton) findViewById(R.id.greenButton);
    ImageButton yellowIb = (ImageButton) findViewById(R.id.yellowButton);
    ImageButton blackIb = (ImageButton) findViewById(R.id.blackButton);
    ImageButton purpleIb = (ImageButton) findViewById(R.id.purpleButton);
    ImageButton orangeIb = (ImageButton) findViewById(R.id.orangeButton);
    ImageButton eraseIb = (ImageButton) findViewById(R.id.eraseButton);
    ImageButton plusIb = (ImageButton) findViewById(R.id.plusButton);
    ImageButton minusIb = (ImageButton) findViewById(R.id.minusButton);

    minusIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            strokeWidth -= 2;
            canvasView.setStrokeWidth(strokeWidth);
        }
    });

    plusIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                strokeWidth += 2;
                canvasView.setStrokeWidth(strokeWidth);
            }
    });

    eraseIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(Color.TRANSPARENT);
        }
    });

    orangeIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(orange);
        }
    });

    purpleIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(purple);
        }
    });

    blackIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(Color.BLACK);
        }
    });

    yellowIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(Color.YELLOW);
        }
    });

    greenIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(Color.GREEN);
        }
    });

    blueIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(Color.BLUE);
        }
    });

    redIb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.setColor(Color.RED);
        }
    });

    saveB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    });

    aboutB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), AboutActivity.class);

            startActivity(intent);
        }
    });

    resetB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            canvasView.clearCanvas();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

0 个答案:

没有答案