开发者

Android Changing Colors with Thread

I am a bit frustrated as to how to do this.., Can anyone please help me or guide me in the right direction?, with the code below. I just need to make a circle change colors in 2 second intervals, and i cant find anything out there.. basically if I could understand the following it would be enough

1.I have a class call DrawCircle that Exatends View 2.I have the Activity program where I instantiate the DrawCircle Class. Questions:

1 - Where Do I create the Thread? in Activity or in the DrawCircle class?, and if I do, what should i put in the run();

2 - Where would I put the "Thread.sleep(2000)"? 3. and Finally, what do i need to make sure I have in the DrawCircle class and what should I make sure I have in the Activity or main program?

I just need to make a circle change colors in intervals of 2 seconds

Thank you very much for all the help ( I have spent 12 hours already trying to make this work and maybe I just shouldnt do it anymore)

Here is the code

public class ColorChanges extends Activity { 
  DrawCircle dc;   
  Paint pa = new Paint();


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

              //this line does not work

            dc = new DrawCircle(this,pa); 
            dc.setBackgroundColor(Color.WHITE);     
            drawCircleToCanvas();
            setContentView(dc);    
          }  

   void drawCircleToCanvas()   
    {      

       final Handler handler = new Handler() 
          {                  
            public void handleMessage(Message msg) {
                pa.setColor(Color.BLUE);
                 dc.setBackgroundColor(Color.RED);

                 dc.postInvalidate();                         
                 }                 
            };          
            Thread updateUI = new Thread()       
            {                        
                public void run() {                              

                    try {
                        for (int i=0;i<20;i++){
                        Thread.sleep(2000);
                        handler.sendMessage(handler.obtainMessage());
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }                     
                };                     
                updateUI.start();       

                }  

}

That is the Activity and here is my Class

public class DrawC开发者_StackOverflow社区ircle 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, Paint p) {
    super(context);
       p1 = p;
       p1.setStyle(Paint.Style.STROKE);
       p1.setColor(Color.GREEN);
       p1.setStyle(Paint.Style.FILL);



    // TODO Auto-generated constructor stub
}

@Override  
public void onDraw(Canvas canvas) 
{     

             canvas.drawCircle(200, 200, 100, p1); 
}

}


Do it in the UI thread. Instead of Thread.sleep, use Handler.postDelayed() with a two-second delay.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜