2011-12-28 74 views
0

我发现了类似的问题,但没有一个给出我的问题的正确答案。在android上绘制多个视图

我有一个类是一个视图。

public class Actor extends View { 
private ShapeDrawable drawable; 

int x = 0; 
int y = 0; 
int width = 100; 
int height = 40; 

public Actor(Context context) { 
    super(context); 

    drawable = new ShapeDrawable(new OvalShape()); 
    drawable.getPaint().setColor(0xff74AC23); 
    drawable.setBounds(x, y, x + width, y + height); 
} 

@Override 
public void onDraw(Canvas canvas){ 
    super.onDraw(canvas); 
    drawable.draw(canvas); 
} 

}

我想现在要做的是吸取了Android屏幕上的这个观点的多个实例。我可以在活动中做此画一个:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Dimension actor1 = new Actor(this); 
    setContentView(actor1); 
} 

我的目标是绘制它的多个实例,当然与其他X和Y参数,以便它们不重叠。我不想先将视图转换为位图,才能将其放入画布中。

回答

0

首先 - 创建一些布局,然后调用setContentView(yourlayout)。

然后,使用这样的方法:

setContentView(R.layout.main); 
FrameLayout mainLayout=(FrameLayout) findViewById(R.id.mainLayout); 
Dimension actor1 = new Actor(this); 
mainLayout.addView(actor1); 
+0

我试图扩大在这样一种方式,我能做到这一点,你说的方式的ViewGroup类,但我不能得到这个工作。 – 2011-12-28 17:36:53

+0

对不起,但我没有得到。为什么那么认真?更新了我的答案。 – 2011-12-28 17:43:33

+0

我的意思是我没有得到一个布局,我可以做到这一点。但我只是想,可能由RelativeLayout来完成这项工作,我会看看它。 – 2011-12-28 17:47:01