Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Android开发(14)-- 多线程中实现利用自定义控件绘制小球并完成小球自动下落的功能

$
0
0

1、布局界面

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".GameActivity" >
  10. <www.csdn.net.tetris.view.GameView
  11. android:id="@+id/gameview"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"/>
  14. <Button
  15. android:id="@+id/btn_left"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_alignLeft="@+id/gameview"
  19. android:layout_alignParentBottom="true"
  20. android:text="@string/btn_text" />
  21. <Button
  22. android:id="@+id/btn_right"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_alignBottom="@+id/gameview"
  26. android:layout_centerHorizontal="true"
  27. android:layout_alignParentBottom="true"
  28. android:text="@string/btn_right" />
  29. </RelativeLayout>


2、封装的一个绘制的图的类

  1. package www.csdn.net.tetris.domain;
  2. import www.csdn.net.tetris.view.GameView;
  3. public class Block {
  4. private int x=20,y=20;
  5. public GameView gameView;
  6. public Block(GameView gameView){
  7. this.gameView=gameView;
  8. }
  9. //向左移动
  10. public void moveLeft(){
  11. x-=10;
  12. gameView.invalidate();
  13. }
  14. //向右移动
  15. public void moveRight(){
  16. x+=10;
  17. gameView.invalidate();
  18. }
  19. //下落方法
  20. public void downLoad(){
  21. y+=10;
  22. gameView.invalidate();//重新绘制
  23. }
  24. public int getX() {
  25. return x;
  26. }
  27. public void setX(int x) {
  28. this.x = x;
  29. }
  30. public int getY() {
  31. return y;
  32. }
  33. public void setY(int y) {
  34. this.y = y;
  35. }
  36. }


3、创建一个画布和线程的操作

  1. package www.csdn.net.tetris.view;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9. import www.csdn.net.tetris.domain.Block;
  10. public class GameView extends View {
  11. public static Block block;
  12. public Handler handler;
  13. //定义方向
  14. public static int dir=-1;
  15. //上下左右
  16. public static final int DIRUP=1;
  17. public static final int DIRDOWN=2;
  18. public static final int DIRLEFT=3;
  19. public static final int DIRRIGHT=4;
  20. public GameView(Context context,AttributeSet attrs) {
  21. super(context);
  22. //创建俄罗斯方块对象
  23. this.block=new Block(this);
  24. handler=new Handler(){
  25. public void handleMessage(Message msg) {
  26. super.handleMessage(msg);
  27. switch (msg.what) {
  28. case DIRLEFT:
  29. GameView.block.moveLeft();
  30. break;
  31. case DIRRIGHT:
  32. GameView.block.moveRight();
  33. break;
  34. default:
  35. GameView.block.downLoad();
  36. break;
  37. }
  38. }
  39. };
  40. //创建线程
  41. new Thread (new Runnable(){
  42. @Override
  43. public void run() {
  44. while(true){
  45. try{
  46. System.out.println("子线程名称:::"+Thread.currentThread().getName());
  47. //block.downLoad();
  48. Thread.sleep(1000);
  49. handler.sendEmptyMessage(dir);
  50. }catch(Exception e){
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }).start();
  56. }
  57. @Override
  58. protected void onDraw(Canvas canvas) {
  59. super.onDraw(canvas);
  60. //设置了画布的颜色
  61. canvas.drawARGB(255, 0, 0, 255);
  62. //设置一个画笔
  63. Paint paint=new Paint();
  64. paint.setARGB(255, 255, 0, 0);
  65. canvas.drawCircle(block.getX(), block.getY(), 10, paint);
  66. }
  67. }


4、在MainActivity中的操作

  1. package com.example.tetris;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;
  10. import www.csdn.net.tetris.domain.Block;
  11. import www.csdn.net.tetris.view.GameView;
  12. public class GameActivity extends Activity {
  13. //声明按钮控件
  14. public Button btn_start;
  15. public Button btn_right;
  16. public Button btn_left;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_game);
  21. //获取按钮控件
  22. btn_start=(Button) findViewById(R.id.btn_start);
  23. btn_start.setOnClickListener(new MyOnClickListener());
  24. System.out.println("主线程:::"+Thread.currentThread().getName());
  25. }
  26. @Override
  27. public boolean onCreateOptionsMenu(Menu menu) {
  28. // Inflate the menu; this adds items to the action bar if it is present.
  29. getMenuInflater().inflate(R.menu.main, menu);
  30. return true;
  31. }
  32. class MyOnClickListener implements View.OnClickListener{
  33. @Override
  34. public void onClick(View v) {
  35. int id=v.getId();
  36. switch (id) {
  37. case R.id.btn_start:
  38. //GameView gameView=new GameView(GameActivity.this,null);
  39. GameActivity.this.setContentView(R.layout.activity_layout);
  40. //获取控件
  41. btn_right=(Button) findViewById(R.id.btn_right);
  42. btn_left=(Button) findViewById(R.id.btn_left);
  43. btn_left.setOnClickListener(new MyOnClickListener());
  44. btn_right.setOnClickListener(new MyOnClickListener());
  45. break;
  46. case R.id.btn_left:
  47. Toast.makeText(GameActivity.this, "向左移动", Toast.LENGTH_LONG).show();
  48. GameView.block.moveLeft();
  49. //GameView.dir=GameView.DIRLEFT;
  50. break;
  51. case R.id.btn_right:
  52. Toast.makeText(GameActivity.this, "向右移动", Toast.LENGTH_LONG).show();
  53. GameView.block.moveRight();
  54. //GameView.dir=GameView.DIRRIGHT;
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. }
  61. }
作者:sgx425021234 发表于2013-6-7 1:11:42 原文链接
阅读:125 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>