Android Button setAlpha

前端 未结 4 1328
太阳男子
太阳男子 2021-01-13 06:47

There are a set of buttons, I want to get the result:

When I click one of them, first I divide them into two parts: the clicked one and the others. I\'m trying to se

相关标签:
4条回答
  • 2021-01-13 07:25
    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);  
    }
    

    This works for me :)

    0 讨论(0)
  • 2021-01-13 07:41

    Thanks for the question and the answer. This really helped me out.

    For my solution, I needed to set the alpha of a button without seeing any animation effect, but the button.setAlpha(x) was failing sporadically. Using animations instead did the trick, but I had to set the duration to zero to get the automatic effect.

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

    I use this for player controls in a media application, so I had something like this:

        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 讨论(0)
  • 2021-01-13 07:44

    public void setAlpha (int alpha) - deprecated

    public void setAlpha (float alpha) (0f < alpha < 1f) Added in API level 11

    0 讨论(0)
  • 2021-01-13 07:48

    Using AlphaAnimation should work; verified on my device.

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

    The key is calling setFillAfter(true) so that the alpha change persists.

    0 讨论(0)
提交回复
热议问题