Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android上绘制多个视图_Android_View_Android Canvas - Fatal编程技术网

在android上绘制多个视图

在android上绘制多个视图,android,view,android-canvas,Android,View,Android Canvas,我发现了类似的问题,但没有一个能正确回答我的问题 我有一个类是一个视图 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

我发现了类似的问题,但没有一个能正确回答我的问题

我有一个类是一个视图

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参数,这样它们就不会重叠。我不想先将视图转换为位图以将其放入画布。

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

然后,使用以下方法:

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

我试图扩展ViewGroup类,使其能够按照您所说的方式进行扩展,但无法正常工作。抱歉,我无法正常工作。为什么这么严重?更新了我的答案。我的意思是我没有一个布局工作,我可以这样做。但我只是在想,也许相对来说你会完成这项工作,我会看看。无论如何,你只能为你的活动设置一个内容视图。框架布局做到了这一点,我不必为自定义视图组和其他东西操心。。。谢谢:)(也许你可以把setContentView(mainLayout)放在你的答案中,并删除第一行,以防其他人想看)