gpt4 book ai didi

java - 如何在 Android 中更改 Canvas 上颜料的颜色/厚度?

转载 作者:行者123 更新时间:2023-12-01 11:22:18 25 4
gpt4 key购买 nike

当我单击图像按钮时,我无法更改油漆的颜色或厚度,即使我也对其进行了编程。我知道我做错了,我想我知道为什么,但我无法修复它。我觉得我没有调用正确的画笔。这是我的 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();
}
});
}

最佳答案

您正在完全不同的 Paint 上设置值,该 Paint 与您的 CanvasView 没有连接。您必须更改 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);

调用它

关于java - 如何在 Android 中更改 Canvas 上颜料的颜色/厚度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31080826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com