Chinaunix首页 | 论坛 | 博客
  • 博客访问: 135106
  • 博文数量: 38
  • 博客积分: 1605
  • 博客等级: 上尉
  • 技术积分: 370
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-12 14:59
文章分类

全部博文(38)

文章存档

2013年(3)

2012年(8)

2011年(28)

分类: 嵌入式

2011-07-14 12:54:52

    1. import android.content.Context;
    2. import android.graphics.Bitmap;
    3. import android.graphics.BitmapFactory;
    4. import android.graphics.Canvas;
    5. import android.graphics.Color;
    6. import android.graphics.Paint;
    7. import android.graphics.Path;
    8. import android.graphics.Rect;
    9. import android.graphics.drawable.Drawable;
    10. import android.os.Handler;
    11. import android.os.Message;
    12. import android.util.Log;
    13. import android.view.MotionEvent;
    14. import android.view.View;
    15. import android.widget.Toast;

    16. public class TestView extends View implements Runnable
    17. {

    18.     static final int MSG_A = 100;
    19.     
    20.     private Handler m_handle = null;
    21.     private float x, y;
    22.     
    23.     
    24.     public TestView (Context context) {
    25.         super(context);
    26.         
    27.         m_context = context;
    28.                 
    29.         // 创建一个handle接收来自子线程的消息, 非常可靠! 推荐!
    30.         m_handle = new Handler() {
    31.              public void handleMessage(Message msg) {
    32.                  if (msg.what == MSG_A ) {
    33.                   Toast.makeText(m_context, Integer.toString(x) + " " + Integer.toString(y), Toast.LENGTH_SHORT).show();     
    34.                   }
    35.                  super.handleMessage(msg);
    36.              }
    37.          };
    38.             
    39.          // Run thread
    40.          new Thread(this).start();
    41.     }
    42.     
    43.     @Override
    44.     protected void onDraw(Canvas canvas) {
    45.         super.onDraw(canvas);
    46.         canvas.drawLine(0, 0, x, y, null);
    47.     }

    48.      
    49.     @Override
    50.     public void run() {

    51.         while( !Thread.currentThread().isInterrupted() )
    52.         {
    53.             try{
    54.                 
    55.                 Thread.sleep(100);
    56.             }
    57.             catch( InterruptedException e )
    58.             {
    59.                 Thread.currentThread().interrupt();
    60.             }
    61.                         
    62.             x++;
    63.             y++;
                 
                  // 子线程通知主线程重画canvas     
    1.             this.postInvalidate();
    2.             
    3.             if( x > 500.0f && y > 500.0f)
    4.             {
    5.                 // 发送消息给主线程
    6.                 Message msg = new Message();
    7.                 msg.what = MSG_A;
    8.                 msg.arg1 = (int)x;
    9.                 msg.arg2 = (int)y;
    10.                 this.m_handle.sendMessage(msg);
    11.             }

    12.     
    13.            x = 0.0f;
    14.            y = 0.0f;
    15.       
    16.         }
    17.                 
    18.     }

    19. }
阅读(887) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~