2016-10-22 94 views
1

我在下面發佈的所有代碼都像我想要的那樣工作,只是將其作爲參考,也可能是希望實現類似功能的人。它很簡單,評論很好。Android動畫副作用:按鈕在動畫顏色時更改其形狀

所以有,我正在開發一個應用程序,我有動態創建的幾個圓形按鈕基於這樣定義的XML繪製資源:

<?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)如下所示。 (正如你所看到的,我將隨機放置在屏幕上)。它工作正常:

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(); 
} 

感謝幫助反正