在带有画布的单个活动中使用多个视图
基本上我想要完成的是我想要一个可以传递位图的画布,与其余的视图对象(按钮、文本视图等)一起显示在屏幕上。
我为画布视图创建了一个类,如下所示(它还没有做太多事情):
public class Foo extends View {
public Foo(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// draw a solid blue circle
paint.setColor(Color.BLUE);
canvas.drawCircle(20, 20, 15, paint);
}
}
现在这是我的主要活动:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button switchLeft = (Button) findViewById(R.id.switch_left);
switchLeft.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO
}
});
Button switchRight = (Button) findViewById(R.id.switch_left);
switchRight.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO
}
});
Foo myCanvas = new Foo(this);
setContentView(myCanvas);
}
}
当我对画布视图对象调用 setContentView 时,其余的视图将从屏幕上消失。如何在不丢失其余视图的情况下调用此视图对象?
谢谢。
Basically what I am trying to accomplish is I want a canvas I can pass bitmaps to, to be displayed on the screen with the rest of my view objects (buttons, textviews, etc).
I created a class for the canvas view like so (it doesn't do much yet):
public class Foo extends View {
public Foo(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
// draw a solid blue circle
paint.setColor(Color.BLUE);
canvas.drawCircle(20, 20, 15, paint);
}
}
Now here is my main activity:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button switchLeft = (Button) findViewById(R.id.switch_left);
switchLeft.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO
}
});
Button switchRight = (Button) findViewById(R.id.switch_left);
switchRight.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TODO
}
});
Foo myCanvas = new Foo(this);
setContentView(myCanvas);
}
}
When I call setContentView to the canvas view object, the rest of my views disappear from the screen. How can I call up this view object without losing the rest of my views?
THanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在“主”布局中引用您自己的视图
you could refer to you own view in your "main"-layout