在android不工作的情况下创建弹跳球

在android不工作的情况下创建弹跳球,android,Android,我正在尝试在android中创建一个简单的球杆游戏,并为android创建以下xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://sche

我正在尝试在android中创建一个简单的球杆游戏,并为android创建以下xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".BallActivity">
<learn.com.application.BallView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ball"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left"
            android:onClick="moveleft"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Right"
            android:onClick="moveright"/>

    </LinearLayout>

</LinearLayout>

包含按钮的LinearLayout不可见,因为BallView是match_parent。但若我将BallView的高度声明为wrap_内容,它将不起作用。如何显示底部的左右按钮。我还可以使用什么来向左或向右移动斗杆

BallView的代码

public class BallView extends android.support.v7.widget.AppCompatImageView {

    private Context mContext;
    int x = -1;
    int y = -1;
    boolean first=true;
    private int xVelocity = 10;
    private int yVelocity = 5;
    private Handler h;
    public static int FRAME_RATE = 50;
    public static int stickx=0,sticky=0;
     public static int width=0;
     public static int height=0;
     boolean gameover=false;
    public BallView(Context context,AttributeSet s) {
        super(context,s);
        mContext=context;
        h=new Handler();

    }

    private Runnable r=new Runnable() {
        @Override
        public void run() {
            invalidate();
        }
    };

    @Override
    protected void onDraw(Canvas canvas) {

if(first) {
    stickx = getWidth() / 2 - 40;
    sticky = getHeight() - 60;
    width = getWidth();
    height = getHeight();
first=false;
}
        Bitmap bmp= BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ball1);
        if(x<0 && y<0)
        {
            x=getWidth()/2;
            y=getHeight()/2;
        }
        else {
            x += xVelocity;
            y += yVelocity;


            if (x + bmp.getWidth() > getWidth() || (x < 0))
                xVelocity = xVelocity * -1;


            if (y < 0)
                yVelocity = yVelocity * -1;

            if (y + bmp.getHeight() >= sticky) {
                if (x + bmp.getWidth() > stickx && x + bmp.getWidth() < (stickx + 180)) {
                    yVelocity = yVelocity * -1;
                 Log.v("ball","collision");
                }
                else {
                    Log.v("ball", "game over");

              gameover=true;
                }
            }
        }
        RectF rr=new RectF();
        int right=stickx+180;
        int bottom=sticky+30;

        rr.set(stickx,sticky,right,bottom);
        Paint p=new Paint();
        canvas.drawRoundRect(rr,5,5,p);
        canvas.drawBitmap(bmp,x,y,null);

        if(!gameover)
        h.postDelayed(r,FRAME_RATE);
    }
}
公共类BallView扩展了android.support.v7.widget.AppCompatImageView{
私有上下文;
int x=-1;
int y=-1;
布尔值优先=真;
私有int xVelocity=10;
私人住宅单位面积=5;
私有处理器h;
公共静态整数帧速率=50;
公共静态int stickx=0,sticky=0;
公共静态整型宽度=0;
公共静态整数高度=0;
布尔gameover=false;
公共BallView(上下文、属性集){
超级(上下文,s);
mContext=上下文;
h=新处理程序();
}
private Runnable r=new Runnable(){
@凌驾
公开募捐{
使无效();
}
};
@凌驾
受保护的void onDraw(画布){
如果(第一){
stickx=getWidth()/2-40;
粘性=getHeight()-60;
宽度=getWidth();
高度=getHeight();
第一个=假;
}
位图bmp=BitmapFactory.decodeResource(mContext.getResources(),R.drawable.ball1);
如果(x=粘性){
如果(x+bmp.getWidth()>stickx&&x+bmp.getWidth()<(stickx+180)){
yVelocity=yVelocity*-1;
对数v(“球”、“碰撞”);
}
否则{
Log.v(“球”,“比赛结束”);
gameover=true;
}
}
}
RectF rr=新的RectF();
int right=stickx+180;
int-bottom=sticky+30;
rr.set(粘滞,粘滞,右侧,底部);
油漆p=新油漆();
canvas.drawRoundRect(rr,5,5,p);
drawBitmap(bmp,x,y,null);
如果(!gameover)
h、 后延迟(r,帧速率);
}
}


我宁愿使用RelativeLayout和TableRow来定义球视图的高度和宽度,而不是使用LinearLayout,如图所示。由于无法使用
match\u parent
,因此需要
layout\u width
layout\u height
的特定像素,因为
wrap\u content
不知道要包装哪些内容。在
Canvas
中调用
getWidth
getHeight
,我假设如果设置
wrap\u content
,这些方法将返回0。我们如何指定像素宽度,因为对于不同的屏幕,它们会有所不同。基于getWidth和getHeight,我能够知道球的极限。否则,在这种情况下,球反弹的检查是什么。尝试使用
wrap\u content
并调试
onDraw
方法,是否
getWidth
getHeight
返回非零值?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BallActivity">

    <learn.com.application.BallView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/tablerow"
        android:id="@+id/ball"/>

    <TableRow
        android:id="@+id/tablerow"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Left"
            android:onClick="moveleft"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Right"
            android:onClick="moveright"/>
    </TableRow>

</RelativeLayout>