Android 用户界面的位图问题

发布于 2024-10-18 05:18:11 字数 1431 浏览 5 评论 0原文

我有一个扩展 View 的 Ball 类。在里面我给出了一些特性并实现了 onTouchEvent(),这样我就可以处理运动。而且我使用 onDraw,这样我就可以绘制球的位图。在我的活动类中,我创建了一个新的布局,并向其中添加了视图,以便可以显示它。一切工作正常,除了当我尝试向布局中添加更多球时,它们没有出现!始终显示布局中第一个添加的球! 这是活动类中的 onCreate 代码:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
    int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;

    Point point1 = new Point();
    point1.x = 50;
    point1.y = 20;
    Point point2 = new Point();
    point2.x = 100;
    point2.y = 20;
    Point point3 = new Point();
    point3.x = 150;
    point3.y = 20;

    ColorBall ball1 = new ColorBall(this,R.drawable.bol_groen, point1);
    ll.addView(ball1, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball2 = new ColorBall(this,R.drawable.bol_rood, point2);
    ll.addView(ball2, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball3 = new ColorBall(this,R.drawable.bol_blauw, point3);

    ll.addView(ball3, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);        

}

可能是什么问题?我也尝试过在最后只使用一个 setContentView() 。我想我不能使用布局,所以我可以绘制位于自定义视图!我说得对吗? 我是否应该更改我的代码并创建一个视图,并在其中创建一个包含我想要显示的所有球的数组,然后设置此视图从我的主活动类中显示?(就像这样 setContentView(customview) )。

I have a class Ball which extents View.Inside there i give some characteristics and implements onTouchEvent() so i can handle the movement.Also i use onDraw so i can draw the bitmap of the ball. in my activity class i crate a new Layout and i add the view to it so it can be displayed. Everything works fine except when, i try to add more balls to my layout they don't appear!Always the first added in layout ball is displayed!
Here's the onCreate code from activity class:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.HORIZONTAL);
    int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
    int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;

    Point point1 = new Point();
    point1.x = 50;
    point1.y = 20;
    Point point2 = new Point();
    point2.x = 100;
    point2.y = 20;
    Point point3 = new Point();
    point3.x = 150;
    point3.y = 20;

    ColorBall ball1 = new ColorBall(this,R.drawable.bol_groen, point1);
    ll.addView(ball1, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball2 = new ColorBall(this,R.drawable.bol_rood, point2);
    ll.addView(ball2, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);

    ColorBall ball3 = new ColorBall(this,R.drawable.bol_blauw, point3);

    ll.addView(ball3, new LinearLayout.LayoutParams(lHeight, lWidth));
    setContentView(ll);        

}

What could possibly be the problem?I have try it also with only one setContentView() at the end.I am thinking that i can't use a Layout so i can draw bitmaps which are in a custom View!Am i right?
Should i change my code and create a View and inside there make an array with all the balls i want to be displayed and then set this View to be displayed from my main class of activity?(like this setContentView(customview)).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

霞映澄塘 2024-10-25 05:18:11

您多次调用 setContentView,而预计每个 Activity 初始化仅调用一次。

更新

您可以使用此布局 xml 而不是您使用的编程方式吗?这只是为了 100% 确定您要添加 ColorBalls 的容器是否正常。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/container"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />

以防万一,以下是将其包含在活动中的代码:

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

    setContentView(R.layout.your_name_of_layout);

    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    ..
    container.addView(ball1);
    container.addView(ball2);
    ..
}

You are calling setContentView several times, while it is expected to call only once per activity initialization.

UPDATE:

Could you use this layout xml instead of programmatic way you use? This is just to be 100% sure the container to which you will add the ColorBalls is Ok.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/container"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" />

Just in case, here is the code to include it in the activity:

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

    setContentView(R.layout.your_name_of_layout);

    LinearLayout container = (LinearLayout) findViewById(R.id.container);
    ..
    container.addView(ball1);
    container.addView(ball2);
    ..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文