我成功地在画布上添加了一个椭圆形状,但是现在我想添加两个矩形,但由于某些原因它们不会添加到画布中。椭圆形是一个移动的球,矩形是“背景”的静态元素。一个矩形应该是一个地板,另一个矩形应该是运动物体,球的障碍物。将多个元素绘制到画布上
我试图想象它的图像:
这是代码,mBack和小怪我试图添加的矩形。
AnimatedView animatedView = null;
ShapeDrawable mDrawable = new ShapeDrawable();
ShapeDrawable mBack = new ShapeDrawable();
ShapeDrawable mJump = new ShapeDrawable();
public static int x;
public static int y;
public class AnimatedView extends ImageView {
static final int width = 50;
static final int height = 50;
public AnimatedView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mDrawable = new ShapeDrawable(new OvalShape());
mBack = new ShapeDrawable(new RectShape());
mObs = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setColor(0xffffAC23);
//mDrawable.setBounds(x, y, x + width, y + height);
mDrawable.setBounds(y, x, y + width, x + height);
mBack.setBounds(100, 100, 100, 100);
mObs.setBounds(120,120,120,120);
}
@Override
protected void onDraw(Canvas canvas) {
mDrawable.setBounds(y, x, y + width, x + height);
mBack.draw(canvas);
mDrawable.draw(canvas);
invalidate();
}
}
mDrawable将被添加,但mBack或mObs不是。将setBounds添加到onDraw也不会改变事物。
代码中缺少一些上下文; x和y他们在哪里定义?绘图的定义在哪里? – abbath
对不起,现在已修复。 – Banana