gpt4 book ai didi

java - 每次我尝试更改绘画的属性(厚度或颜色)时,它都会更改 Canvas 上已有的现有绘画(android)(java)

转载 作者:行者123 更新时间:2023-11-29 03:02:16 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的绘图应用程序,让您可以在 Canvas 上绘图,并且可以更改 Canvas 的颜色和粗细。出现的问题是,每次我尝试更改油漆的颜色或厚度时,它都会更改 Canvas 上已经存在的油漆。我明白为什么它不起作用,但我只是不知道如何解决它以及问题出在哪里。

CanvasView 类

package samkough.com.painter;

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.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class CanvasView extends View
{
/*
* When the user touches the screen and moves their finger to draw,
* we will use a Path to trace their drawing action on the canvas.
* Both the canvas and the drawing on top of it are represented by Paint
* objects. The initial paint color corresponds to the first color
* in the palette we created last time, which will be initially selected
* when the app launches. Finally we declare variables for the canvas
* and bitmap - the user paths drawn with drawPaint will be drawn onto
* the canvas, which is drawn with canvasPaint.
* */
//drawing paint
private Paint paint = new Paint();
// canvas paint
private Paint canvasPaint = new Paint();
//drawing path
private Path path = new Path();
// canvas
private Canvas canvas = new Canvas();
//canvas bitmap
private Bitmap canvasBitmap;
// brush size and pixel size
private float brushSize, pixelAmount;

public CanvasView(Context context, AttributeSet attrs)
{
// Setting the anti-alias, stroke join and cap styles will make the user's drawings appear smoother.
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);
path.reset();
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.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(brushSize);
invalidate();
}

public void setColor(int p)
{
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setColor(p);
invalidate();
}
}

主要 Activity 类

package samkough.com.painter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.samkough.painter.R;

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);
}
}

最佳答案

您已经创建了 canvas,但是您没有使用它在上面绘制任何东西。所以本质上,行 drawCanvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); 没有做任何事情。

您需要做的是在 canvas 上绘制 path。然后使用 drawCanvas 绘制 canvasBitmap。这样您就可以维护单个 path 和单个 paint 而不是多个。

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

当您执行上述操作时,每次用户绘制新路径时,canvasBitmap 都会在其中保存所有旧路径。所以每次调用 onDraw 时,只会绘制新的 path

永远记住:创建多个PAINT 对象 是一种不好的做法。尽量重用 Paint 对象。

关于java - 每次我尝试更改绘画的属性(厚度或颜色)时,它都会更改 Canvas 上已有的现有绘画(android)(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34112657/

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