我可以画一个圆,但是我的问题是每次单击时都知道如何绘制多个圆。
圆的中心必须是被单击的X和Y坐标,我需要将该点存储在我的Vector上,并且每次单击View都不能破坏该Vector。
如果在if(图== 1)的末尾没有无效,则根本不会画出该圆。
我的中心是一个存储有int x,int y的Point2D。
package com.example.soalr.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.Vector;
public class MyView extends View {
Paint paint = null;
int figure;
int lados_poly;
int cor;
int deletar;
Circulo cir;
Circulo raio;
int CursorX, CursorY;
int nrCliques;
Vector<Ponto2D> ptsCirc = new Vector<Ponto2D>();
public MyView(Context context) {
super(context);
paint = new Paint();
figure = 0;
cor = 0;
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setStrokeWidth(10);
figure = 0;
cor = 0;
Ponto2D centroCirc = new Ponto2D();
centroCirc.x = CursorX;
centroCirc.y = CursorY;
cir = new Circulo(centroCirc);
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
figure = 0;
cor = 0;
}
public void clickEcra() {
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
nrCliques++;
case MotionEvent.ACTION_UP:
CursorX = (int)event.getX();
CursorY = (int)event.getY();
if (figure == 1) {
Ponto2D centroCirc = new Ponto2D();
centroCirc.x = CursorX;
centroCirc.y = CursorY;
ptsCirc.add(centroCirc);
invalidate();
}
default:
return false;
}
}
});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
clickEcra();
Ponto2D p3 = new Ponto2D();
p3.x = 600;
p3.y = 150;
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#F5F1E0"));
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#3F5866"));
//cores
if (cor == 1) {
paint.setColor(Color.parseColor("#393E46"));
} else if (cor == 2){
paint.setColor(Color.parseColor("#00ADB5"));
} else if (cor == 3) {
paint.setColor(Color.parseColor("#F8B500"));
} else if (cor == 4) {
paint.setColor(Color.parseColor("#FC3C3C"));
}
//figuras
if (figure == 1) {
if (ptsCirc.size() > 0) {
for (int a = 0; a <= ptsCirc.size(); a++) {
Circulo raio = new Circulo(ptsCirc.get(a));
raio.radius = 100;
canvas.drawCircle(ptsCirc.get(a).x, ptsCirc.get(a).y, raio.radius, paint);
}
}
}
else if (figure == 2) {
clickEcra();
Ponto2D p1 = new Ponto2D();
Ponto2D p2 = new Ponto2D();
Reta retinha = new Reta(p1, p2);
if (nrCliques == 1) {
p1.x = CursorX;
p1.y = CursorY;
System.out.println("x1: " + p1.x + " y1: " + p1.y);
} else if (nrCliques == 2) {
p2.x = CursorX;
p2.y = CursorY;
System.out.println("x2: " + p2.x + " y2: " + p2.y);
}
System.out.println("x2_: " + retinha.pfinal.x + " y2_: " + retinha.pfinal.y);
System.out.println("x1_: " + retinha.pinicial.x + " y1_: " + retinha.pinicial.y);
canvas.drawLine(retinha.pinicial.x, retinha.pinicial.y, retinha.pfinal.x, retinha.pfinal.y, paint);
} else if (figure == 3) {
Poligono poly = new Poligono(lados_poly);
if (lados_poly >= 3) {
for (int i = 0; i <= lados_poly - 1; i++) {
Ponto2D ponto4 = new Ponto2D(poly.pontosPolig.get(i).x, poly.pontosPolig.get(i).y);
Ponto2D ponto5 = new Ponto2D();
if (i < lados_poly) {
ponto5.x = poly.pontosPolig.get(i + 1).x;
ponto5.y = poly.pontosPolig.get(i + 1).y;
Reta retaPoli = new Reta(ponto4, ponto5);
int var_lados = lados_poly - 1;
if (i != var_lados) {
canvas.drawLine(retaPoli.pinicial.x, retaPoli.pinicial.y, retaPoli.pfinal.x, retaPoli.pfinal.y, paint);
} else {
canvas.drawLine(poly.pontosPolig.firstElement().x, poly.pontosPolig.firstElement().y, retaPoli.pinicial.x, retaPoli.pinicial.y, paint);
}
}
}
}
}
if (deletar == 2){
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#F5F1E0"));
canvas.drawPaint(paint);
nrCliques = 0;
}
}
public void setfigure(int a) {
this.figure = a;
}
public void Cor1_mudar(int text_cor) {
this.cor = text_cor;
}
public void verLados (int lados){
this.lados_poly = lados;
}
public void Resetar(int delete){
this.deletar = delete;
}
}
这是Circle.java:
打包com.example.soalr.myapplication;
public class Circulo {
int radius;
Ponto2D centro;
public Circulo(Ponto2D c) {
radius = 0;
centro = c;
}
}
最佳答案
您在这里有两个问题:
您只会看到1个圆圈。这可能是因为nrCliques
不是您期望的值。与其从0迭代到nrCliques
,不如考虑从0迭代到ptsCirc.size()
,或者使用for..each循环。对于此类问题,请the debugger is your friend。
除非您在invalidate()
中进行了onDraw()
调用,否则您不会看到任何圈子。从invalidate()
调用onDraw()
会导致框架以最快的速度重复调用onDraw()
。这没有什么坏处,但是没有充分的理由却消耗了大量的处理器功能。最好在其伴随数据发生更改时“提示”您的View
重绘。在这种情况下,这意味着将invalidate()
调用放在对ptsCirc.add()
的调用之后。这样,每次您点击屏幕时,您只会接到大约1个onDraw()
呼叫。
关于java - 如何在 Canvas 上绘制多个圆圈?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53394019/