2015-02-06 3 views
3

클릭하면 두 개의 배경색을 앞뒤로 전환하여 번 깜박이는 버튼을 표시하고 싶습니다.Android, 애니메이션으로 버튼 배경색을 설정 하시겠습니까?

This 대답은 깜박이는 버튼을 만들기 위해 AlphaAnimation을 사용

final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible 
    animation.setDuration(500); // duration - half a second 
    animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate 
    animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely 
    animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in 
    final Button btn = (Button) findViewById(R.id.your_btn); 
    btn.startAnimation(animation); 

을하지만 난 그게 배경색으로 작동시킬 수 없습니다.

안드로이드 Studio는 다음을 자동으로 완료되지 않습니다 :

animation = new Animation() { 
    @Override 
    public void setBackgroundColor(int bg) { 
     super.setBackgroundColor(bg); 
    } 
}; 

하지만 (bg = Color.parseColor("#ffff9434")와) 버튼에 적용하려하지만 주사위.

미리 감사드립니다.

편집 또한 다음을 시도했지만이되지 않으며 대답을 참조

Button btn = (Button)this.findViewById(R.id.btn1); 
    //Let's change background's color from blue to red. 
    ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)}; 
    TransitionDrawable trans = new TransitionDrawable(color); 
    //This will work also on old devices. The latest API says you have to use setBackground instead. 
    btn.setBackgroundDrawable(trans); 
    trans.startTransition(5000); 

ETID 2

가 작동있어 (here에서) 작동하지 않았다

아래

+0

솔루션을 찾고 답변을 게시했지만 다른 사람들이 3 대신 4 가지 솔루션을 제공하도록 남겨 두겠습니다. – Birrel

+0

좋습니다. 선생님, 잘 ....... – Elltz

답변

4

작동했습니다! 게시물 this에 감사드립니다!

final AnimationDrawable drawable = new AnimationDrawable(); 
final Handler handler = new Handler(); 

drawable.addFrame(new ColorDrawable(Color.RED), 400); 
drawable.addFrame(new ColorDrawable(Color.GREEN), 400); 
drawable.setOneShot(false); 

btn = (Button) view.findViewById(R.id.flashBtn); 

btn.setBackgroundDrawable(drawable); 
handler.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     drawable.start(); 
    } 
    }, 100); 

매력처럼 작동합니다.

관련 문제