Рисовать с Java - PullRequest
       7

Рисовать с Java

0 голосов
/ 10 ноября 2018

Так что мне нужно сделать одно приложение в Android Studio, как Paint У меня есть это в Myview.Java. Я получаю координаты, но не могу сохранить и нарисовать отрезок. Я пытался с Vector, и я думаю, что Vector - это решение, но оно не работает так, как я

public class MyView extends View {
    Paint paint = null;
    int figure;

public MyView(Context context) {
    super(context);
    paint = new Paint();
    figure = 0;
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint = new Paint();
    figure = 0;
}


public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    paint = new Paint();
    figure = 0;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int x = getWidth();
    int y = getHeight();
    int radius;
    radius = 100;
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);
    // Use Color.parseColor to define HTML colors
    paint.setColor(Color.parseColor("#CD5C5C"));
    if (figure == 1)
        canvas.drawCircle(x / 2, y / 2, radius, paint);
}

public void setfigure(int a) {
    this.figure = a;
}
}

В MainActivity у меня есть этот код

 public class MainActivity extends AppCompatActivity {
MyView v;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    v = findViewById(R.id.teste);


}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();


    //String x1 = "" + x;
    TextView textView = (TextView) findViewById(R.id.testecord);
    TextView textView2 = (TextView) findViewById(R.id.testecord2);

    Ponto2D pinicial = new Ponto2D(Math.round(x), Math.round(y));
    Ponto2D pfinal = new Ponto2D(Math.round(x), Math.round(y));

    String x1 = "" + pinicial.x;
    String x2 = "" + pfinal.x;
    textView.setText(x1);
    textView2.setText(x2);

    int n = 4; // tamanho do vetor
    int v[] = new int[n]; // declaração e alocação de espaço para o vetor "v"
    int i; // índice ou posição

    // processando os "n" elementos do vetor "v"
    for (i = 0; i < n; i++) {
        v[i] = pinicial.x; // na i-ésima posição do vetor "v" armazena o valor da variável "i"
    }


    return true;


    }

public void f(View vs) {
    v.setfigure(1);
    v.invalidate();
}
}

Мне нужно сохранить точки, которые пользователь нажимает, чтобы нарисовать один прямой сегмент. У меня есть код сегмента и точки.

public class Segmento_de_Reta {

Ponto2D pinicial;
Ponto2D pfinal;
String cor;

public Segmento_de_Reta(Ponto2D a, Ponto2D b) {

    pinicial = a;

    pfinal = b;

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...