我无法在画布上绘制两个对象

时间:2015-09-09 11:08:06

标签: java android canvas geometry rect

我想通过从客户端创建params来创建rect和circle。我只能画其中一个。但我只能画出其中一个。在下面的代码我得到默认圈和我的矩形。       MainActivity.java

 public class MainActivity extends AppCompatActivity implements   View.OnClickListener {

String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};
EditText xc;
EditText yc;
EditText xr;
EditText yr;
EditText hr;
EditText wr;
EditText rc;
Spinner ColorC;
Spinner ColorR;
RelativeLayout display;
DrawShapes circle;
DrawShapes rect;
Button draw;



@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//circle
    ColorC = (Spinner) findViewById(R.id.ColorSpinner0);
    xc = (EditText) findViewById(R.id.xc);
    yc = (EditText) findViewById(R.id.yc);
    rc = (EditText) findViewById(R.id.rc);
//rect
    ColorR = (Spinner) findViewById(R.id.ColorSpinner);
    xr = (EditText) findViewById(R.id.xr);
    yr = (EditText) findViewById(R.id.yr);
    hr = (EditText) findViewById(R.id.hr);
    wr = (EditText) findViewById(R.id.wr);


    draw = (Button) findViewById(R.id.draw);
    display = (RelativeLayout) findViewById(R.id.display);
    draw.setOnClickListener(this);

    ArrayAdapter<String> spinnerArrayAdapter0 = new ArrayAdapter<>(this,   android.R.layout.simple_spinner_item, colors);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(this,   android.R.layout.simple_spinner_item, colors);
    spinnerArrayAdapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ColorC.setAdapter(spinnerArrayAdapter0);
    ColorR.setAdapter(spinnerArrayAdapter);





}



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

@Override
public void onClick(View v) {

           if (    xc.getText().toString().equals("") ||
                    yc.getText().toString().equals("") ||
                    rc.getText().toString().equals("") ||
                    xr.getText().toString().equals("") ||
                    yr.getText().toString().equals("") ||
                    wr.getText().toString().equals("") ||
                    hr.getText().toString().equals("")) {

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.errMsg);
                builder.setTitle("Fields Require!");
                builder.setPositiveButton("OK", null);
                builder.create();
                builder.show();
            } else
                createShapes();

  }

  //create shapes
public void createShapes(){
//circle
int x = Integer.parseInt(xc.getText().toString());
int y = Integer.parseInt(yc.getText().toString());
int rrr = Integer.parseInt(rc.getText().toString());
int cc =Color.parseColor(ColorC.getSelectedItem().toString());
circle = new DrawShapes(x,y,rrr,cc,this);
display.addView(circle);
//rect
int xx = Integer.parseInt(xr.getText().toString());
int yy = Integer.parseInt(yr.getText().toString());
int hh = Integer.parseInt(hr.getText().toString());
int ww = Integer.parseInt(wr.getText().toString());
int cr =Color.parseColor(ColorR.getSelectedItem().toString());
rect = new DrawShapes(xx,yy,xx+hh,yy+ww,cr,this);
display.addView(rect);
}

}

ShapesConst.java

package com.example.alexa.shapes;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

public class ShapesConst extends View {
private int xc;
private int yc;
private int rc;
private int xr;
private int yr;
private int hr;
private int wr;

private int colorC;
private int colorR;

//default shapes
public ShapesConst(Context d) {
    super(d);
    xc = 200;
    yc = 200;
    rc = 150;
    xr = 300;
    yr = 300;
    hr = 150;
    wr = 250;
    colorC = Color.BLUE;
    colorR = Color.RED;
 }
 // circle shape
 public ShapesConst(int xc, int yc, int r, int colorC, Context c) {
    super(c);
    this.xc = xc;
    this.yc = yc;
    this.rc = r;
    this.colorC = colorC;

 }
 //rect shape
 public ShapesConst(int xr, int yr, int hr, int wr, int colorR, Context rec)      {
    super(rec);
    this.xr = xr;
    this.yr = yr;
    this.hr = hr;
    this.wr = wr;
    this.colorR = colorR;

}

//circle get
public int GetXC() {
    return xc;
}

public int GetYC() {
    return yc;
}

public int GetR() {
    return rc;
}

public int GetColorC() {
    return colorC;
}

//rect get
public int GetXR() {
    return xr;
}

public int GetYR() {
    return yr;
}

public int GetH() {
    return hr;
}

public int GetW() {
    return wr;
}

public int GetColorR() {
    return colorR;
}

//circle set
public void setXC(int xc) {
    this.xc = xc;
}
public void setYC(int yc) {
    this.yc = yc;
}
public void setR(int r) {
    this.rc = r;
}
public void setColorC(int colorC)
{
    this.colorC = colorC;
}

//rect set
public void setXR(int xr) {
    this.xr = xr;
}
public void setYR(int yr) {
    this.yr = yr;
} public void setH(int h) {
    this.hr = h;
} public void setW(int w) {
    this.wr = w;

} public void setColorR(int colorR) {
    this.colorR = colorR;
}



}

DrawShapes.java

public class DrawShapes extends ShapesConst {

//circle
public DrawShapes(int xc, int yc, int rc,int colorC,Context c) {
    super(c);
    setXC(xc);
    setYC(yc);
    setColorC(colorC);
    setR(rc);
 }
//rect
public DrawShapes(int xr, int yr, int hr, int wr, int colorR,Context r) {
    super(r);
    setXR(xr);
    setYR(yr);
    setH(hr);
    setW(wr);
    setColorR(colorR);

 }
Paint paintC = new Paint();
Paint paintR = new Paint();

    @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    paintC.setColor(GetColorC());
    paintR.setColor(GetColorR());
    canvas.drawCircle(GetXC(), GetYC(), GetR(), paintC);
    canvas.drawRect(GetXR(),GetYR(),GetXR()+GetYR(),GetW()+GetH(),paintR);

}
}

1 个答案:

答案 0 :(得分:0)

非常糟糕的代码!

你有一个onDraw方法,它绘制一个矩形和一个圆圈?严重?

你有问题,也许你在另一个后面添加一个形状,你不使用canvas元素作为基础你使用RelativeLayout,为什么?