单击imageButton时,我无法更改颜料的颜色或厚度,即使我也对其进行了编程。我知道我做错了,我想我知道为什么,但是我无法解决。我觉得我没有在说正确的画笔。这是我的Java类代码:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
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 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()
{
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
}
这是我的MainActivity代码:
public class MainActivity extends Activity {
private CanvasView canvasView;
private Paint paint;
private int orange;
private int purple;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
canvasView = (CanvasView) findViewById(R.id.canvasView);
paint = new Paint();
orange = Color.rgb(255, 245, 238);
purple = Color.rgb(128, 0, 128);
// 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) {
paint.setStrokeWidth(-5);
}
});
plusIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setStrokeWidth(5);
}
});
eraseIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(Color.TRANSPARENT);
}
});
orangeIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(orange);
}
});
purpleIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(purple);
}
});
blackIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(Color.BLACK);
}
});
yellowIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(Color.YELLOW);
}
});
greenIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(Color.GREEN);
}
});
blueIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(Color.BLUE);
}
});
redIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.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();
// canvasView.clearCanvas2();
}
});
}
最佳答案
您正在与您的CanvasView没有连接的整个不同的Paint上设置值。您将不得不更改CanvasView的Paint并对其调用invalid以进行任何更改。
在CanvasView中实现类似这样的操作以更改笔触宽度:
public void setStrokeWidth(int strokeWidth) {
paint.setStrokeWidth(strokeWidth);
invalidate();
}
并用
onClick()
在您的canvasView.setStrokeWidth(5);
中调用它颜色也一样:
public void setColor(int color) {
paint.setColor(color);
invalidate();
}
并用
onClick()
在您的canvasView.setColor(Color.BLUE);
中调用它