单父视图中的多个子视图

发布于 2024-11-03 02:46:21 字数 4031 浏览 1 评论 0原文

我在第一个视图类中创建了两个视图类

,我正在创建多个第二个视图的实例

并添加到第一个视图中,但它只显示一个视图,

请帮助我..

这是父视图

package com.game.AntSmash;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.SurfaceView;
import android.widget.AbsoluteLayout;
import android.widget.LinearLayout;

public class surface extends LinearLayout {


    public surface(Context context) {
        super(context);
        this.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));
        this.setBackgroundDrawable(getResources().getDrawable(R.drawable.grass));
        Drawable d = getResources().getDrawable(R.drawable.ant_big);
        d.setBounds(160, 0, 160 + d.getIntrinsicWidth(), d.getIntrinsicHeight());
        this.addView(new antView(context, d, 0.2f, 0, 0, 0, 0, 0));
        d = getResources().getDrawable(R.drawable.ant_big);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            this.addView(new antView(context, d, 0.2f, 0, 0, 160, 0, 0));

    }
}

这是子视图

package com.game.AntSmash;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.text.BoringLayout.Metrics;
import android.view.View;
import android.widget.ImageView;

public class antView extends View {
    int screenW = 320;
    int screenH = 480;
    int X;
    int Y;
    int initialY ;
    int ballW;
    int ballH;
    int angle;
    float dY;
    float dX;
    float acc;
    int initialX;
    Drawable ball;


    public antView(Context context, Drawable bmp, float speed, int dx, int dy, int initialX, int initialY, int angle) {
        super(context); 
        this.ball = bmp;
        this.acc = speed;
        this.dX = dx;
        this.dY = dy;
        this.initialX = initialX;
        this.initialY = initialY;
        this.angle = angle;
    }

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

        //compute roughly ball speed and location
        Y+= (int) dY; //increase or decrease vertical position  
        if (Y > (screenH - ballH)) {
            dY=(-1)*dY; //reverse speed when bottom hit 
        }
        dY+= acc; //increase or decrease speed

        //compute roughly ball speed and location
        X+= (int) dX; //increase or decrease vertical position  
        if (X > (screenW - ballW)) {
            dX=(-1)*dX; //reverse speed when bottom hit 
        }
        dX+= acc / 5; //increase or decrease speed


        //increase rotating angle
        if ((angle = angle + 10) >360) angle =0;

        //draw ball
        canvas.save(); //save the position of the canvas
        canvas.rotate(angle, X + (ballW / 2), Y + (ballH / 2)); //rotate the canvas
        canvas.translate(X, Y);
        ball.draw(canvas);
        canvas.restore(); //rotate the canvas back so that it looks like ball has rotated


        //call the next frame
        if(X > screenW - ballW && Y > screenH - ballH) {

        } else {
            invalidate();
        }

    }

}

这是活动

package com.game.AntSmash;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

public class AntSmashActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout v = new surface(this);
        setContentView(v);

    }
}

i have made two view classes

in first view class i am making more than one instance of second view

and adding into first view but it displaying only one view

please help me..

This is Parent View

package com.game.AntSmash;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.SurfaceView;
import android.widget.AbsoluteLayout;
import android.widget.LinearLayout;

public class surface extends LinearLayout {


    public surface(Context context) {
        super(context);
        this.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));
        this.setBackgroundDrawable(getResources().getDrawable(R.drawable.grass));
        Drawable d = getResources().getDrawable(R.drawable.ant_big);
        d.setBounds(160, 0, 160 + d.getIntrinsicWidth(), d.getIntrinsicHeight());
        this.addView(new antView(context, d, 0.2f, 0, 0, 0, 0, 0));
        d = getResources().getDrawable(R.drawable.ant_big);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            this.addView(new antView(context, d, 0.2f, 0, 0, 160, 0, 0));

    }
}

This is child view

package com.game.AntSmash;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.text.BoringLayout.Metrics;
import android.view.View;
import android.widget.ImageView;

public class antView extends View {
    int screenW = 320;
    int screenH = 480;
    int X;
    int Y;
    int initialY ;
    int ballW;
    int ballH;
    int angle;
    float dY;
    float dX;
    float acc;
    int initialX;
    Drawable ball;


    public antView(Context context, Drawable bmp, float speed, int dx, int dy, int initialX, int initialY, int angle) {
        super(context); 
        this.ball = bmp;
        this.acc = speed;
        this.dX = dx;
        this.dY = dy;
        this.initialX = initialX;
        this.initialY = initialY;
        this.angle = angle;
    }

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

        //compute roughly ball speed and location
        Y+= (int) dY; //increase or decrease vertical position  
        if (Y > (screenH - ballH)) {
            dY=(-1)*dY; //reverse speed when bottom hit 
        }
        dY+= acc; //increase or decrease speed

        //compute roughly ball speed and location
        X+= (int) dX; //increase or decrease vertical position  
        if (X > (screenW - ballW)) {
            dX=(-1)*dX; //reverse speed when bottom hit 
        }
        dX+= acc / 5; //increase or decrease speed


        //increase rotating angle
        if ((angle = angle + 10) >360) angle =0;

        //draw ball
        canvas.save(); //save the position of the canvas
        canvas.rotate(angle, X + (ballW / 2), Y + (ballH / 2)); //rotate the canvas
        canvas.translate(X, Y);
        ball.draw(canvas);
        canvas.restore(); //rotate the canvas back so that it looks like ball has rotated


        //call the next frame
        if(X > screenW - ballW && Y > screenH - ballH) {

        } else {
            invalidate();
        }

    }

}

And this is activity

package com.game.AntSmash;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

public class AntSmashActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout v = new surface(this);
        setContentView(v);

    }
}

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

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

发布评论

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

评论(1

尬尬 2024-11-10 02:46:21

您需要为 LinearLayout 父级设置一些布局参数,还需要使用 setOrientation() 来定义子视图的显示方式。

孩子的尺寸可能太大了。

如果你想使用 2D 绘图,我推荐我编写的教程系列。使用 SurfaceView 进行 2D 绘图比使用视图和布局更容易。

最后提示:类名应该以大写字母开头。 SurfaceAntView 而不是 surfaceantView

You need to set some layout params for your LinearLayout parent, also use setOrientation() to define how the child views should be displayed.

The dimension of the child is probably just too big.

If you want to use 2D drawing, I recommend a tutorial series I have written. Its easier to use a SurfaceView for 2D drawing than using Views and Layouts.

A tip at the end: A class name should start with an upper case. Surface and AntView instead of surface and antView.

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