开发者

Android Drawing

I am learning how to code with Android but I am still confused bby the way it works, I am able to create simple draws like circles and stuff but now i want to paint the circle multiple times with a delay of 2 seconds.. I would appreciatge if you experts can help me improve my code and to put the stuff in the correct place

public class ColorChanges extends Activity {
DrawCircle dc;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
                drawCircleToCanvas() 

}

void drawCircleToCanvas() 
 { 
    final Handler handler = new Handler() {            
     public void handleMessage(Message msg) {               
          dc.postInvalidate();              
          }            
     };     
     Thread updateUI = new Thread() 
     {              
         public void run() {             
                dc = new DrawCircle(this);  //this line does not work
                dc.setBackgroundColor(Color.WHITE);     


                setContentView(dc);

        开发者_开发知识库     handler.sendEmptyMessage(0);               

             }           
         };           
         updateUI.start(); 
        } 
     }

public class DrawCircle extends View { Paint p1 = new Paint(); Paint p2 = new Paint(); Paint p3 = new Paint();

Paint pAll[] = new Paint[3];


public DrawCircle(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
public DrawCircle(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public DrawCircle(Context context) {
    super(context);

       p1.setStyle(Paint.Style.STROKE);
       p1.setColor(Color.GREEN);
       p1.setStyle(Paint.Style.FILL);

       p2.setStyle(Paint.Style.STROKE);
       p2.setColor(Color.BLUE);
       p2.setStyle(Paint.Style.FILL);

       p3.setStyle(Paint.Style.STROKE);
       p3.setColor(Color.YELLOW);
       p3.setStyle(Paint.Style.FILL);

       pAll[1] = p1;
       pAll[2] = p2;
       pAll[3] = p3;

    // TODO Auto-generated constructor stub
}

@Override  
public void onDraw(Canvas canvas) 
{     
    for (int i = 0; i < pAll.length;i++)
    {
     canvas.drawCircle(200, 200, 100, pAll[i]);
    }

}

}


If you want to draw the same circle with its color changing over the time (each 2 seconds as you mentionned) you should use an Handler

in order to create a timer and to switch the paint at each time it is called. Do not forget to call the invalidate function inside your custom view, as this function ask the system to redraw the screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜