Android开发(14)-- 多线程中实现利用自定义控件绘制小球并完成小球自动下落的功能
1、布局界面
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GameActivity" >
<www.csdn.net.tetris.view.GameView
android:id="@+id/gameview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/gameview"
android:layout_alignParentBottom="true"
android:text="@string/btn_text" />
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/gameview"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="@string/btn_right" />
</RelativeLayout>
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GameActivity" >
<www.csdn.net.tetris.view.GameView
android:id="@+id/gameview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/gameview"
android:layout_alignParentBottom="true"
android:text="@string/btn_text" />
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/gameview"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="@string/btn_right" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GameActivity" >
<www.csdn.net.tetris.view.GameView
android:id="@+id/gameview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/gameview"
android:layout_alignParentBottom="true"
android:text="@string/btn_text" />
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/gameview"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="@string/btn_right" />
</RelativeLayout>
2、封装的一个绘制的图的类
[java]
package www.csdn.net.tetris.domain;
import www.csdn.net.tetris.view.GameView;
public class Block {
private int x=20,y=20;
public GameView gameView;
public Block(GameView gameView){
this.gameView=gameView;
}
//向左移动
public void moveLeft(){
x-=10;
gameView.invalidate();
}
//向右移动
public void moveRight(){
x+=10;
gameView.invalidate();
}
//下落方法
public void downLoad(){
y+=10;
gameView.invalidate();//重新绘制
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
[java]
package www.csdn.net.tetris.domain;
import www.csdn.net.tetris.view.GameView;
public class Block {
private int x=20,y=20;
&nb
补充:移动开发 , Android ,