2010-07-16 9 views
5

Ci sono una serie di pulsanti, voglio ottenere il risultato:Pulsante Android setAlpha

Quando scatto una di loro, prima li divido in due parti: la cliccato uno e gli altri. Sto provando a impostare un colore diverso o un valore alfa diverso.

ora uso setAlpha, ma quando cambio il valore da 0 a 255 , funziona, ma quando cambio il valore da 255-0, doesnot lavoro. Non so perché.

Forse dopo invoco il metodo Button.setAlpha(), ho bisogno di richiamare un altro metodo?

il mio codice:

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

risposta

10

Utilizzando AlphaAnimation dovrebbe funzionare; verificato sul mio dispositivo.

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

La chiave sta chiamando setFillAfter(true) modo che il cambiamento alpha persiste.

+0

Ci provo, ma è un peccato che non funzioni –

+0

@ZHAO Ho trovato una soluzione - ha modificato il mio codice sopra. In bocca al lupo! –

+0

grazie mille. funziona bene. Grazie! –

2

Grazie per la domanda e la risposta. Questo mi ha davvero aiutato.

Per la mia soluzione, avevo bisogno di impostare l'alfa di un pulsante senza vedere alcun effetto di animazione, ma il button.setAlpha (x) non funzionava sporadicamente. Usare le animazioni invece ha fatto il trucco, ma ho dovuto impostare la durata a zero per ottenere l'effetto automatico.

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

Io lo uso per controlli del lettore multimediale in un'applicazione, così ho avuto qualcosa di simile:

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

Questo funziona per me :)

0

pubblico setAlpha void (int alfa) - deprecato

public void setAlpha (float alfa) (0f < alpha < 1f) Aggiunto in livello API 11