使用Runnable产生新线程
使用postInvalidate进行刷新
1 package com.hu.move; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 import android.view.View; 8 9 public class MoveView extends View implements Runnable{ 10 11 private int y; 12 private Paint paint; 13 14 public MoveView(Context context) { 15 super(context); 16 paint=new Paint(); 17 paint.setColor(Color.GREEN); 18 paint.setAntiAlias(true); 19 new Thread(this).start();//启动线程 20 } 21 22 @Override 23 protected void onDraw(Canvas canvas) {//画图 24 super.onDraw(canvas); 25 canvas.drawLine(0, 0, 300, y, paint); 26 } 27 28 public void run() {//新线程 29 while(y<400){ 30 y++; 31 try { 32 Thread.sleep(100); 33 } catch (InterruptedException e) { 34 e.printStackTrace(); 35 } 36 MoveView.this.postInvalidate();//非UI线程进行刷新 37 } 38 } 39 40 }