我一直试图在Android中绘制多个圈子,但它不起作用,我看不出我做错了什么。当我运行它时,一个圆圈工作得很好。我试图找到类似的问题,但我发现的问题似乎比我在这个简单的应用程序中的要求更高级。
public class DrawCharsActivity extends Activity {
Paint[] paint;
double lon = 30;
double lat = 20;
int scrWidth;
int scrHeight;
int x;
int y;
//String latitudeString[] = new String[]{"30", "20"};
//String longitudeString[]= new String[]{"30", "20"};
String name[] = new String[]{"joseph", "jj"};
Double longitude[] = new Double[]{30.2, 30.2};
Double latitude[] =new Double[]{30.2, 45.2};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
// x = (int) ((scrWidth / 360.0) * (180 + lon));
//y = (int) ((scrHeight / 180.0) * (90 - lat));
scrWidth = getWindowManager().getDefaultDisplay().getWidth();
scrHeight = getWindowManager().getDefaultDisplay().getHeight();
}
class Panel extends View {
public Panel(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
for (int i = 0; i < name.length; i++) {
paint = new Paint[i];
paint[i].setColor(Color.BLACK);
paint[i].setStrokeWidth(1);
paint[i].setTextSize(20);
//longitude[i] = Double.parseDouble(latitudeString[i]);
//latitude[i] = Double.parseDouble(longitudeString[i]);
x = (int) ((scrWidth / 360.0) * (180 + longitude[i]));
y = (int) ((scrHeight / 180.0) * (90 - latitude[i]));
canvas.drawColor(Color.WHITE);
canvas.drawCircle(x, y, 3, paint[i]);
System.out.println(x + "x" + name[i]);
System.out.println(y + "y" + name[i]);
}
// canvas.drawLine(80, 80, 80, 200, paint);
// canvas.drawText(""+canvas.getWidth()+", "+canvas.getHeight(), 0,
// 200,paint);
}
}
}
答案 0 :(得分:0)
怎么样:
// Create an array list
ArrayList name = new ArrayList();
// add elements to the array list
name.add("Joseph");
name.add("jj");
for (String s : name) {
paint = new Paint[i];
paint[i].setColor(Color.BLACK);
paint[i].setStrokeWidth(1);
paint[i].setTextSize(20);
//longitude[i] = Double.parseDouble(latitudeString[i]);
//latitude[i] = Double.parseDouble(longitudeString[i]);
x = (int) ((scrWidth / 360.0) * (180 + longitude[i]));
y = (int) ((scrHeight / 180.0) * (90 - latitude[i]));
canvas.drawColor(Color.WHITE);
canvas.drawCircle(x, y, 3, paint[i]);
System.out.println(x + "x" + name[i]);
System.out.println(y + "y" + name[i]);
}
答案 1 :(得分:0)
我解决了这个问题,我在DrawCharsActivity中将类Panel设置为Panel面板。然后而不是写:
setContentView(new Panel(this));
我写道:
panel = new Panel(this);
panel.setBackgroundColor(Color.WHITE);
setContentView(panel);