【问题标题】:Android Button setAlphaAndroid Button setAlpha
【发布时间】:2010-07-16 05:17:21
【问题描述】:

有一组按钮,我想得到结果:

当我点击其中一个时,我首先将它们分成两部分:点击的部分和其他部分。我正在尝试为它们设置不同的颜色或 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);
 }
}

【问题讨论】:

    标签: android button


    【解决方案1】:

    使用 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),这样alpha 变化就会持续存在。

    【讨论】:

      【解决方案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);
      

      【讨论】:

        【解决方案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);  
        }
        

        这对我有用:)

        【讨论】:

          【解决方案4】:

          public void setAlpha (int alpha) - 已弃用

          public void setAlpha (float alpha) (0f

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-12-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多