2010-07-16 27 views
5

有一組按鈕,我想要得到的結果是:的Android按鍵setAlpha

當我點擊其中的一個,首先我將它們分爲兩個部分:點擊一個和其他人。我試圖爲不同的顏色或Alpha值設置不同的顏色。

現在我用setAlpha,但是當我改變從0到255的價值,它的工作原理,但是當我從255至0更改值,它亙古不變的工作。我不知道爲什麼。

也許在我調用方法Button.setAlpha()後,我需要調用另一個方法?

我的代碼:

public class MainActivity extends Activity { 
// button alpha value: minimize value 
public static int BUTTON_ALPHA_MIN = 0; 

// button alpha value: maximize value 
public static int BUTTON_ALPHA_MAX = 255; 

private LinearLayout centerRegion; 
private LinearLayout bottomRegion; 

private Button btnCheckIn; 
private Button btnReview; 
private Button btnMyCircles; 
private Button btnSettings; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // get all the widgets 
    getAllWidgets(); 

    // set buttons click response function 
    btnCheckIn.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
    centerRegion.setBackgroundColor(android.graphics.Color.RED); 

    btnReview.getBackground().setAlpha(BUTTON_ALPHA_MIN); 
    btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN); 
    btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN); 

    btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX); 
    } 
    }); 

    btnReview.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
    centerRegion.setBackgroundColor(android.graphics.Color.BLUE); 

    btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MIN); 
    btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MIN); 
    btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MIN); 

    btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX); 
    } 
    }); 

    btnMyCircles.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
    centerRegion.setBackgroundColor(android.graphics.Color.YELLOW); 

    btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX); 
    btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX); 
    btnSettings.getBackground().setAlpha(BUTTON_ALPHA_MAX); 

    v.getBackground().setAlpha(BUTTON_ALPHA_MIN); 
    } 
    }); 

    btnSettings.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
    centerRegion.setBackgroundColor(android.graphics.Color.MAGENTA); 

    btnCheckIn.getBackground().setAlpha(BUTTON_ALPHA_MAX); 
    btnReview.getBackground().setAlpha(BUTTON_ALPHA_MAX); 
    btnMyCircles.getBackground().setAlpha(BUTTON_ALPHA_MAX); 

    v.getBackground().setAlpha(BUTTON_ALPHA_MIN); 
    } 
    }); 
} 

/** 
    * get all the widgets 
    */ 
public void getAllWidgets() { 
    this.centerRegion = (LinearLayout) this.findViewById(R.id.center_region); 
    this.bottomRegion = (LinearLayout) this.findViewById(R.id.bottom_region); 

    this.btnCheckIn = (Button) this.findViewById(R.id.button_check_in); 
    this.btnReview = (Button) this.findViewById(R.id.button_review); 
    this.btnMyCircles = (Button) this.findViewById(R.id.button_my_circles); 
    this.btnSettings = (Button) this.findViewById(R.id.button_setting); 
} 
} 

回答

10

使用AlphaAnimation應該工作;在我的設備上驗證。

public class Test extends Activity implements OnClickListener { 

    private AlphaAnimation alphaDown; 
    private AlphaAnimation alphaUp; 
    private Button b1; 
    private Button b2; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LinearLayout ll = (LinearLayout) findViewById(R.id.linear_layout); 

     b1 = new Button(this); 
     b1.setText("Button 1"); 
     b1.setOnClickListener(this); 
     ll.addView(b1); 

     b2 = new Button(this); 
     b2.setText("Button 2"); 
     b2.setOnClickListener(this); 
     ll.addView(b2); 

     alphaDown = new AlphaAnimation(1.0f, 0.3f); 
     alphaUp = new AlphaAnimation(0.3f, 1.0f); 
     alphaDown.setDuration(1000); 
     alphaUp.setDuration(1000); 
     alphaDown.setFillAfter(true); 
     alphaUp.setFillAfter(true); 
    } 

    public void onClick(View v) { 
     if (v == b1) { 
      b1.startAnimation(alphaUp); 
      b2.startAnimation(alphaDown); 
     } else { 
      b1.startAnimation(alphaDown); 
      b2.startAnimation(alphaUp); 
     } 
    } 
} 

的關鍵是呼籲setFillAfter(true)使得α變化仍然存在。

+0

我試一下,但很遺憾它不起作用 – 2010-07-16 06:17:34

+0

@ZHAO我找到了一個解決方案 - 在上面編輯了我的代碼。祝你好運! – 2010-07-16 06:55:03

+0

非常感謝你。它運作良好,謝謝你! – 2010-07-16 12:14:08

2

感謝您的問題和答案。這真的幫了我。

對於我的解決方案,我需要設置按鈕的alpha而不會看到任何動畫效果,但button.setAlpha(x)偶爾會失敗。使用動畫代替了訣竅,但我必須將持續時間設置爲零才能獲得自動效果。

alphaDown = new AlphaAnimation(1.0f, 0.3f); 
alphaUp = new AlphaAnimation(0.3f, 1.0f); 
alphaDown.setDuration(0); 
alphaUp.setDuration(0); 
alphaDown.setFillAfter(true); 
alphaUp.setFillAfter(true); 

我用這個在媒體應用程序播放器控制,所以我有這樣的事情:

boolean bInitPrevEnabled = m_btPrev.isEnabled(); 
    boolean bInitNextEnabled = m_btNext.isEnabled(); 
    boolean bInitPlayEnabled = m_btPlay.isEnabled(); 

    m_btPrev.setEnabled(true); 
    m_btNext.setEnabled(true); 
    m_btPlay.setEnabled(true); 

    // Process enabling of the specific buttons depending on the state 

    if (bInitPrevEnabled != m_btPrev.isEnabled()) 
      m_btPrev.startAnimation((m_btPrev.isEnabled()) ? alphaUp : alphaDown); 

    if (bInitNextEnabled != m_btNext.isEnabled()) 
      m_btNext.startAnimation((m_btNext.isEnabled()) ? alphaUp : alphaDown); 

    if (bInitPlayEnabled != m_btPlay.isEnabled()) 
      m_btPlay.startAnimation((m_btPlay.isEnabled()) ? alphaUp : alphaDown); 
0
Button btn; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    btn = (Button) findViewById(R.id.main_btn); 
    Drawable d = getResources().getDrawable(R.drawable.imagen); 
    d.setAlpha(60); 
    btn.setBackgroundDrawable(d); 
} 

這對我的作品:)

0

公共無效setAlpha(INT alpha) - 不推薦使用

public void setAlpha(float alpha)(0f < alpha < 1f) 在API級別11中添加