用Java绘画

时间:2018-11-10 14:12:37

标签: java android xml android-studio

所以我需要在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();
}
}

我需要保存用户单击以绘制一个直线段的点。 我有segment和Point的代码。

public class Segmento_de_Reta {

Ponto2D pinicial;
Ponto2D pfinal;
String cor;

public Segmento_de_Reta(Ponto2D a, Ponto2D b) {

    pinicial = a;

    pfinal = b;

}

0 个答案:

没有答案