如何在Android中将不透明度更改为布局?

如何在Android中将不透明度更改为布局?,android,android-linearlayout,opacity,Android,Android Linearlayout,Opacity,现在我将继续改变我的屏幕背景颜色,同时在(x,y)坐标中拖动我的图像。布局也应根据它改变屏幕的不透明度级别 我使用了下面的教程: 然后通过以下编程设置尝试: layout.getBackground().setAlpha(30) 代码如下: public class MyActivity extends Activity { public boolean status = false; Button first,second; LinearLayout layout; @Override p

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

我使用了下面的教程:

然后通过以下编程设置尝试:

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

请输入您的代码。请注意,setAlpha的浮点值介于0和1之间,而不是0-255我应该设置什么值才能回到原始位置。如何显示像android:theme=“@style/theme.Transparent”在alphaAnimation alphaAnimation alphaAnimation=新alphaAnimation(1.0f,0.3f)中那样的完全透明;第一个参数是“开始不透明度”,第二个参数是“结束不透明度”。(1.0f表示纯色,0.0f表示TransParent)我应该设置什么参数以返回原始位置。