在带有画布的单个活动中使用多个视图

发布于 2024-10-01 04:40:36 字数 1391 浏览 4 评论 0原文

基本上我想要完成的是我想要一个可以传递位图的画布,与其余的视图对象(按钮、文本视图等)一起显示在屏幕上。

我为画布视图创建了一个类,如下所示(它还没有做太多事情):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不可一世的女人 2024-10-08 04:40:36

您可以在“主”布局中引用您自己的视图

...
<xxx.yyy.Foo
        android:background="@drawable/red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

you could refer to you own view in your "main"-layout

...
<xxx.yyy.Foo
        android:background="@drawable/red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文