使用Timer Class更新delaye canvas

时间:2012-12-27 14:27:54

标签: android android-canvas android-view

我创建了一个视图类型类,其中onDraw()方法我正在绘制一些框。我没有成功的事情是,我想在3-5秒后消失这些盒子。为此,我使用计时器和timerTask。在TimerTask中,我重写了方法run(),它将Paint对象的颜色更改为白色。背景颜色也是白色,因此它会产生擦除框的效果。你能帮助我吗?

 public class PlayView extends View
{
private float width,height;
private int touchatX, touchatY;
private boolean isanyBox, clearCanvas;
private Point  points[];
private Paint  box;
Timer timer;
TimerTask task;


  //  Set the number of points to be generated so we print that number of boxes on the board
public void nPoints(int n)
{
    points = new Point[n];
    box = new Paint();
    box.setColor(Color.BLUE);
}

public void init()
{
        isanyBox = false;
        clearCanvas = true;
        timer = new Timer();
        task = new TimerTask()
        {
            @Override
            public void run()
            {
                box.setColor(Color.WHITE);
            }
        };
}


      @Override
     protected void onSizeChanged(int w, int h, int oldw, int oldh) 
 {
// TODO Auto-generated method stub

   width = w/6f;
   height = h/6f;

   Log.d("playview", getWidth()+" "+getHeight());
    super.onSizeChanged(w, h, oldw, oldh);
}

public PlayView(Context context)
   {
       super(context);
       setFocusable(true);
       setFocusableInTouchMode(true);
       init();
   }


// Randomly generate the points and draw boxes on these points
public void generatePoints(int np)
{
    Time sec = new Time();

    Random random_Xpoints = new Random();
    Random random_Ypoints = new Random();

    random_Xpoints.setSeed(sec.second);
    random_Ypoints.setSeed(sec.second);
    nPoints(np); // set the number of points to be generated

    for(int i=0; i<np; i++)
    {
        points[i] = new Point();

        points[i].setX( ((random_Xpoints.nextInt(getWidth())/(int)width)*(int)width));
        points[i].setY( ((random_Ypoints.nextInt(getHeight())/(int)height)*(int)height));

        Log.d("Point "+1, points[i].getX()+" "+points[i].getY());
    }
}




     @Override
    public boolean onTouchEvent(MotionEvent event) 
     {
      // TODO Auto-generated method stub

   invalidate();
   isanyBox = true;
   touchatX = (int) ((int) (event.getX()/width)*width);
   touchatY = (int) ((int) (event.getY()/height)*height);
   Log.d("onTouchEvent", event.getX()+" "+event.getY()+" "+touchatX+" "+touchatY);
      invalidate(); 
    return super.onTouchEvent(event);
}


    public void onDraw(Canvas canvas)
  {
   Paint lineColor = new Paint();
   lineColor.setColor(Color.BLACK);

   //Box property
   Paint boxColor = new Paint();
   boxColor.setColor(Color.BLUE);

   //Draw horizontal lines
   for(int i=0; i<6; i++)
   {
      canvas.drawLine(0, i*height, getWidth(), i*height, lineColor);
   }

   //Draw vertical lines
   for(int j=0; j<6; j++)
   {
       canvas.drawLine(j*width, 0, j*width, getHeight(), lineColor);
   }

   if(isanyBox)
   {
   canvas.drawRect(touchatX+2, touchatY+2, touchatX+width-1, touchatY+height-2, boxColor);
   }


       generatePoints(5);
       for(int j=0; j<5; j++)
       {
           canvas.drawRect(points[j].getX()+2, points[j].getY()+2, points[j].getX()+width-1, points[j].getY()+height-2, box);
           Log.d("BoxColor", ""+box);
       }

       if(clearCanvas)
       {

           timer.schedule(task, 3000);
           clearCanvas = false;
           invalidate();
       }

   }
 }

1 个答案:

答案 0 :(得分:1)

更改颜色后调用invalidate();。这将强制系统再次呼叫onDraw()

        @Override
        public void run()
        {
            box.setColor(Color.WHITE);
            invalidate();
        }

修改

我从来都不喜欢计时器,现在我现在为什么,这就是原因,也是因为某些原因Android团队建议人们不要使用它们,因为它可以在这里阅读:http://developer.android.com/reference/java/util/Timer.html

因为你在一个扩展View的类上,所以你应该只调用postDelayed();

  if(clearCanvas)
   {
       clearCanvas = false;
       postDelayed(new Runnable{
        @Override
        public void run(){
            box.setColor(Color.WHITE);
            invalidate();
        }
       }, 3000);
   }