【问题标题】:How to change opacity to Layout in Android?如何在Android中将不透明度更改为布局?
【发布时间】:2014-09-13 18:38:51
【问题描述】:

现在我将继续更改屏幕背景颜色,同时将图像拖动到 (x,y) 坐标中,布局也应该根据它更改屏幕的不透明度级别。

我已经学习了下面的教程:

http://android-er.blogspot.in/2009/08/change-background-color-by-seekbar.html

http://android-er.blogspot.in/2012/02/animate-fade-infade-out-by-changing.html

然后通过以下程序设置进行尝试:

layout.getBackground().setAlpha(30);

代码如下:

public class MyActivity extends Activity {
public boolean status = false;
Button first,second;
LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
layout = (LinearLayout) findViewById(R.id.trans);
first = (Button) findViewById(R.id.first_theme);
second = (Button) findViewById(R.id.second_theme);
// if(status==true)
// {setTheme( android.R.style.ThemeFirst );
// }
// else
{
//            setTheme( android.R.style.ThemeSecond );
//        }
        first.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               status = true;
                layout.getBackground().setAlpha(30);
            }
        });

        second.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                second.getBackground().setAlpha(00);
            }
        });
    }
}

但不,它会导致我 NullPointerException 错误如果有人对此有任何想法,请帮助我的朋友。

【问题讨论】:

  • 请输入您的代码。
  • 请注意,setAlpha 采用介于 0 和 1 之间的浮点值,而不是 0-255

标签: android android-linearlayout opacity


【解决方案1】:

setAlpha 工作,api +11 你可以为每个版本使用这样的东西:

 first.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
           status = true;
            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.3f);
            alphaAnimation.setFillAfter(true);
            alphaAnimation.setDuration(10);//duration in millisecond
            layout.startAnimation(alphaAnimation);
        }
 });  

更新:
要恢复原始不透明度,您可以这样做:

AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f,1.0f);
alphaAnimation.setFillAfter(true);
alphaAnimation.setDuration(10);
layout.startAnimation(alphaAnimation);

【讨论】:

  • 我应该设置什么值才能回到原来的位置。如何在 alphaAnimation 中显示完整的透明度,如 android:theme="@style/Theme.Transparent"
  • AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.3f);第一个参数是开始不透明度,第二个参数是结束不透明度。 (1.0f 表示纯色,0.0f 表示透明)
  • 我应该设置什么参数才能回到原来的位置。
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 2011-01-22
  • 2012-02-08
  • 1970-01-01
  • 2013-03-04
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多