android button setalpha,Android Button setAlpha

在Android应用中,当点击按钮时,作者遇到一个问题:将按钮背景的透明度从255变为0无效,而从0变为255有效。问题的答案指出,使用AlphaAnimation可以解决这个问题,并提供了设置动画时长为0来实现即时效果的代码示例。此外,其他回答也提供了使用AlphaAnimation的不同用法和场景。
摘要由CSDN通过智能技术生成

问题

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 set different color or alpha value to different them.

Now I use setAlpha, but when I change the value from 0 to 255, it works, but when I change the value from 255 to 0 , it doesnot work. I don't know why.

Maybe after I invoke the methodButton.setAlpha(), I need invoke another method?

my code:

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

}

}

回答1:

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.

回答2:

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

回答3:

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

回答4:

public void setAlpha (int alpha) - deprecated

public void setAlpha (float alpha) (0f < alpha < 1f)

Added in API level 11

来源:https://stackoverflow.com/questions/3262118/android-button-setalpha

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值