当我点击一个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();
}
});
}
答案 0 :(得分:1)
您要在与CanvasView无关的整个不同Paint上设置值。您必须更改CanvasView的Paint并在其上调用invalidate以进行任何更改。
在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);
中调用它
答案 1 :(得分:0)
如果你想在 Kotlin 中实现;
paint.strokeWidth = 3.0f