2016-10-22 2 views
1

아래에 게시 한 모든 코드는 원했던 것처럼 작동합니다. 참조 용으로 설정하고 비슷한 것을 얻으려는 사람이있을 수 있습니다. 그것은 간단하고 아주 잘 주석 처리되었습니다.Android 애니메이션 부작용 : 색상을 애니메이션 할 때 버튼의 모양이 바뀝니다.

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<corners 
    android:radius="25dp" 
    /> 
<solid 
    android:color="#0000FF" 
    /> 
<padding 
    android:left="0dp" 
    android:top="0dp" 
    android:right="0dp" 
    android:bottom="0dp" 
    /> 
<size 
    android:width="50dp" 
    android:height="50dp" 
    /> 
</shape> 

그리고 배열에 저장 버튼을 만드는 데 사용됩니다 (gameButtons) : 그래서이

, 나는 내가 다음과 같이 정의 XML 당김 자원을 기반으로 동적으로 생성 몇 라운드 버튼이 응용 프로그램을 개발하고 아래처럼. (보시다시피 스크린에 버튼을 임의로 배치하고 있습니다.) 그것은 잘 작동합니다 :

for(int i = 0; i < (numberOfButtons); i++) 
     { 
     //create a button: 
     Button oneBtn = new Button(this); 

     //get layout reference: 
     RelativeLayout rl = (RelativeLayout) findViewById(R.id.game_window); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(btnSize, btnSize); 

     //get screen size: 
     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     int width = size.x; 
     int height = size.y; 

     //randomize button's position: 
     Random r = new Random(); 
     int randX = r.nextInt(height - btnSize); 
     int randY = r.nextInt(width - btnSize); 

     params.leftMargin = randY; 
     params.topMargin = randX; 

     //set button's parameteres: 
     oneBtn.setId(i); 
     oneBtn.setText(String.valueOf(i)); 

     //make the button round, based on drawable/buttonshape.xml: 
     oneBtn.setBackgroundResource(R.drawable.buttonshape); 

     //add button to the view: 
     gameButtons[i] = oneBtn; 
     rl.addView(oneBtn, params); 


     } 

그런 다음이 단추에 애니메이션을 적용하려고합니다. 애니메이션은 색 몇 번 (파란색 -> 노란색 -> 블루 -> ...) 이 애니메이션뿐만 아니라 (내가 원하는 않습니다)

private void changeButtonColor(final Button button){ 

    int animationTime = 800; 

    //set colors rgb->int 
    int blueInt = Color.rgb(0,0,255); 
    int yellowInt = Color.rgb(255,255,0); 

    //craete an animation: 
    ValueAnimator anim = new ValueAnimator(); 
    anim.setIntValues(blueInt, yellowInt, blueInt); 
    anim.setEvaluator(new ArgbEvaluator()); 

    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 

     //animate button's color: 
     button.setBackgroundColor((Integer)valueAnimator.getAnimatedValue()); 
     } 
    }); 

    //final settings to animation an start: 
    anim.setInterpolator(new LinearInterpolator()); 
    anim.setDuration(animationTime); 
    anim.start(); 
} 

그리고 버튼이 깜박 잘 작동을 변경하여 깜박이는 버튼을 만든다 사실, 내가 호출 할 때 changeButtonColor() 버튼이 모양이 사각형으로 바뀌는 것을 제외하고 싶었던 것처럼.

위 애니메이션이 setBackgroundResource()보다 우선 적용되는 것으로 보이지만, 애니메이션 내에서 setBackgroundResource()을 넣어 여러 방법으로 방지하려고 시도했지만 결과가 없습니다.

이 부작용을 제거하는 방법은 어떤 것이 있습니까?

답변

0

실제로이 동작의 원인을 알 수는 없지만 해결 방법을 구현했습니다. 색상 변경에 애니메이션을 적용하는 대신 배경 자원을 변경하고 있습니다.

은 그래서 파란색 버튼과 같은 다른 리소스를 정의하지만, 노란 색 :

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<corners 
    android:radius="25dp" 
    /> 
<solid 
    android:color="#FFFF00" 
    /> 
<padding 
    android:left="0dp" 
    android:top="0dp" 
    android:right="0dp" 
    android:bottom="0dp" 
    /> 
<size 
    android:width="50dp" 
    android:height="50dp" 
    /> 
</shape> 

하고 난 그냥 자원 변경 애니메이션 오전 : 어쨌든

을 돕는

private void changeButtonColor(final Button button){ 

    int animationTime = 800; 

    int yellowResource = R.drawable.buttonshape_yellow; 
    int blueResource = R.drawable.buttonshape; 

    ValueAnimator anim = new ValueAnimator(); 
    anim.setIntValues(blueResource, yellowResource, blueResource, yellowResource, blueResource); 
    anim.setEvaluator(new ArgbEvaluator()); 

    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator valueAnimator) { 
     button.setBackgroundResource((Integer)valueAnimator.getAnimatedValue()); 
     } 
    }); 
    anim.setInterpolator(new LinearInterpolator()); 
    anim.setDuration(animationTime); 
    anim.start(); 
} 

감사

관련 문제