package com.example.circle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;
public class CircleView extends View{
int diameter;
int x;
int y;
private ShapeDrawable circle;
public CircleView(Context context) {
super(context);
createCircle();
// TODO Auto-generated constructor stub
}
private void createCircle() {
// TODO Auto-generated method stub
x = 20;
y = 20;
diameter = 100;
circle = new ShapeDrawable(new OvalShape());
circle.setBounds(x, y, x + diameter, y + diameter);
circle.getPaint().setColor(0xff00cccc);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
circle.draw(canvas);
}
}
这是我正在经历的现有代码。 请进一步建议我,我应该怎么做才能使用这个代码使这个球在水平方向上移动。
也许像下面这样?
public void moveX(int amount) {
x += amount;
invalidate(); //repaint the screen
}
然后在按键或仅循环时调用该方法。