2013-05-16 4 views
0

나는 흰색으로 화면을 표시하고 많은 색상으로 퇴색하기로되어있는 클래스가 있습니다.프로그래밍 방식으로 안드로이드 레이아웃 색상 변경

public class SecondActivity extends Activity implements Runnable { 

protected PowerManager.WakeLock mWakeLock; 

boolean isrunning = true; 
int red = 0, blue = 0, green = 0; 

@Override 
public void onCreate(Bundle savedInstanceState) .... etc 

TextView btnstart = (TextView) findViewById(R.id.btnstart); 
    btnstart.setTypeface(font1); 
    btnstart.setOnClickListener(new OnClickListener() { 
@Override 
     public void onClick(View v) { 

      MediaPlayer mediaPlayer = MediaPlayer.create(
        getApplicationContext(), R.raw.button); 
      try { 
       mediaPlayer.start(); 
       mediaPlayer.setLooping(false); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      { 

       run(); 
      } 
      ; 
     } 
    }); 

다음 내 실행 가능한

@Override 
public void run() { 
    while (isrunning) { 


     try { 

      red = red + 1; 
      blue = blue + 2; 
      green = green + 3; 
      if (green > 240){ 

       red = 0; 
       blue = 0; 
       green = 0; 
      } 

      LinearLayout colorchanger = (LinearLayout) findViewById(R.id.colorchange); 
      colorchanger.setBackgroundColor(Color.argb(255, red, blue, 
        green)); 
      Thread.sleep(100); 
       } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 



} 

내가 버튼 응용 프로그램 강제 닫히고을 누를 때 나는 다음과 같은 문제가있다. "while (isrunning)"을 꺼내서 if (isrunning)로 바꾸면 모두 작동하지만 버튼을 클릭 한 후에도 계속 색상을 변경하고 싶습니다. 뭔가 잘못 ....

는 아마도 가장 좋은 방법이 아닌 레이아웃 그 일을 내가 사각형 (크기 화면)와 함께해야 할 일을

미안 확인 미안? onDraw 중에 색상을 변경 하시겠습니까?

답변

2

처리기가 이에 적합합니다.

// Initialize your handler: 
Handler h = new Handler(); 

// Start fading: 
h.postDelayed(color_fade, 0); 


// Set up your runnable:  
private Runnable color_fade = new Runnable(){ 
    public void run(){ 
     /* 
     * Do your color and layout changes here. 
     */ 
     h.postDelayed(this, 100); // loop with 100 ms delay. 
    } 
}; 
+0

매력적인 작품! 감사! – Rodrigofantino

관련 문제