版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zcc9618/article/details/80632320
https://blog.csdn.net/jasoncol_521/article/details/12556473
https://ask.csdn.net/questions/11296
***********************************************************
Play.java
private View view;
LinearLayout linearLayout;
final GameView gameView=new GameView(this);
setContentView(R.layout.play);
//1
linearLayout=(LinearLayout)findViewById(R.id.play_view1);
LayoutInflater inflater= (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);
//view=inflater.inflate(R.layout.play,null);
linearLayout.addView(gameView);
//1
class GameView extends View{
Paint paint=new Paint();
public GameView(Context context){
super(context);
setFocusable(true);
}
public void onDraw(Canvas canvas){
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
if(isLose){
paint.setColor(Color.BLACK);
paint.setTextSize(40);
canvas.drawText("游戏已结束", tableWidth/2-100, 200, paint);
}
else{
paint.setColor(Color.rgb(255, 246, 191));
canvas.drawCircle(ballX, ballY, BALL_SIZE, paint);
paint.setColor(Color.rgb(0, 0, 0));
canvas.drawRect(racketX, racketY, racketX+RACKET_WIDTH, racketY+RACKET_HEIGHT, paint);
}
}
}
play.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg1" >
<LinearLayout
android:id="@+id/play_view1"
android:layout_width="match_parent"
android:layout_height="440dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/play_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="3px"
android:layout_marginTop="1dp"
android:background="#000000" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:columnCount="3"
android:rowCount="2" >
<TextView
android:id="@+id/play_txtV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:text="Hi,xxx!你在第x关!"
android:textSize="20dp" />
<Button
android:id="@+id/play_btn1"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:background="@drawable/btnp1_1"
android:text="重 玩"
android:textSize="20dp" />
<TextView
android:id="@+id/play_txtV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="分数:xxx"
android:textSize="20dp" />
<TextView
android:id="@+id/play_txtV3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="倒计时:xxx"
android:textSize="20dp" />
<Button
android:id="@+id/play_btn2"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:background="@drawable/btnp1_1"
android:text="返 回"
android:textSize="20dp" />
</GridLayout>
</LinearLayout>
</LinearLayout>