編輯:關於Android編程
本文實例講述了Android開發之多線程中實現利用自定義控件繪制小球並完成小球自動下落功能的方法。分享給大家供大家參考,具體如下:
1、布局界面
<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、封裝的一個繪制的圖的類
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; } }
3、創建一個畫布和線程的操作
package www.csdn.net.tetris.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; import www.csdn.net.tetris.domain.Block; public class GameView extends View { public static Block block; public Handler handler; //定義方向 public static int dir=-1; //上下左右 public static final int DIRUP=1; public static final int DIRDOWN=2; public static final int DIRLEFT=3; public static final int DIRRIGHT=4; public GameView(Context context,AttributeSet attrs) { super(context); //創建俄羅斯方塊對象 this.block=new Block(this); handler=new Handler(){ public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case DIRLEFT: GameView.block.moveLeft(); break; case DIRRIGHT: GameView.block.moveRight(); break; default: GameView.block.downLoad(); break; } } }; //創建線程 new Thread (new Runnable(){ @Override public void run() { while(true){ try{ System.out.println("子線程名稱:::"+Thread.currentThread().getName()); //block.downLoad(); Thread.sleep(1000); handler.sendEmptyMessage(dir); }catch(Exception e){ e.printStackTrace(); } } } }).start(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //設置了畫布的顏色 canvas.drawARGB(255, 0, 0, 255); //設置一個畫筆 Paint paint=new Paint(); paint.setARGB(255, 255, 0, 0); canvas.drawCircle(block.getX(), block.getY(), 10, paint); } }
4、在MainActivity中的操作
package com.example.tetris; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; import www.csdn.net.tetris.domain.Block; import www.csdn.net.tetris.view.GameView; public class GameActivity extends Activity { //聲明按鈕控件 public Button btn_start; public Button btn_right; public Button btn_left; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); //獲取按鈕控件 btn_start=(Button) findViewById(R.id.btn_start); btn_start.setOnClickListener(new MyOnClickListener()); System.out.println("主線程:::"+Thread.currentThread().getName()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class MyOnClickListener implements View.OnClickListener{ @Override public void onClick(View v) { int id=v.getId(); switch (id) { case R.id.btn_start: //GameView gameView=new GameView(GameActivity.this,null); GameActivity.this.setContentView(R.layout.activity_layout); //獲取控件 btn_right=(Button) findViewById(R.id.btn_right); btn_left=(Button) findViewById(R.id.btn_left); btn_left.setOnClickListener(new MyOnClickListener()); btn_right.setOnClickListener(new MyOnClickListener()); break; case R.id.btn_left: Toast.makeText(GameActivity.this, "向左移動", Toast.LENGTH_LONG).show(); GameView.block.moveLeft(); //GameView.dir=GameView.DIRLEFT; break; case R.id.btn_right: Toast.makeText(GameActivity.this, "向右移動", Toast.LENGTH_LONG).show(); GameView.block.moveRight(); //GameView.dir=GameView.DIRRIGHT; break; default: break; } } } }
希望本文所述對大家Android程序設計有所幫助。
Android安全加密專題文章索引 Android安全加密:對稱加密 Android安全加密:非對稱加密 Android安全加密:消
先上效果圖: 先寫Layout文件:
你是否覺得手機QQ上的好友列表那個控件非常棒? 不是..... 那也沒關系,學多一點知識對自己也有益無害。那麼我們就開始吧。展開型列表控件, 原名ExpandableLi
Android客戶端和PHP、MySQL搭建的服務器之間的簡單交互,實現登錄功能 。實現原理圖:Handler消息機制原理:Handler機制主要包括4個關鍵對象,分別是