以下是从网上搜到的方法,我的程序是要给一个activity加背景图片,activity上有一个GLSurfaceview,只有使GLSurfaceview透明之后才能看到背景图片。
参考:http://www.bangchui.org/read.php?tid=14982
http://www.bangchui.org/read.php?tid=16672
如何实现一个透明的GLSurfaceview,Surfaceview与GLSurfaceview原理类似。
一 .将surfaceview 的格式设置为8888.默认是565的,不透明
public
class
TranslucentGLSurfaceViewActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
// Create our Preview view and set it as the content of our
// Activity
mGLSurfaceView =
new
GLSurfaceView(
this
);
// We want an 8888 pixel format because that's required for
// a translucent window.
// And we want a depth buffer.
mGLSurfaceView.setZOrderOnTop(true);//将view放到顶端
mGLSurfaceView.setEGLConfigChooser(
8
,
8
,
8
,
8
,
16
,
0
);
// Tell the cube renderer that we want to render a translucent version
// of the cube:
mGLSurfaceView.setRenderer(
new
CubeRenderer(
true
));
// Use a surface format with an Alpha channel:
mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
//设置透明
setContentView(mGLSurfaceView);
}
@Override
protected
void
onResume() {
super
.onResume();
mGLSurfaceView.onResume();
}
@Override
protected
void
onPause() {
super
.onPause();
mGLSurfaceView.onPause();
}
private
GLSurfaceView mGLSurfaceView;
}
<
activity
android:name
=
".graphics.TranslucentGLSurfaceViewActivity"
android:label
=
"Graphics/OpenGL ES/Translucent GLSurfaceView"
android:theme
=
"@style/Theme.Translucent"
android:configChanges
=
"orientation|keyboardHidden"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.SAMPLE_CODE"
/>
</
intent-filter
>
</
activity
>
<
style
name
=
"Theme.Translucent"
parent
=
"android:style/Theme.Translucent"
>
<
item
name
=
"android:windowBackground"
>@drawable/transparent_background</
item
>
<
item
name
=
"android:windowNoTitle"
>true</
item
>
<
item
name
=
"android:colorForeground"
>#fff</
item
>
</
style
>
<
resources
>
<
drawable
name
=
"screen_background_black"
>#ff000000</
drawable
>
<
drawable
name
=
"translucent_background"
>#e0000000</
drawable
>
<
drawable
name
=
"transparent_background"
>#00000000</
drawable
>
</
resources
>
android设置背景色为透明
方法一:
只要在配置文件内activity属性配置内加上
android:theme="@android:style/Theme.Translucent"
就好了。
这样就调用了android的透明样式!
方法二:
先在res/values下建colors.xml文件,写入:
<?xmlversion="1.0"encoding="UTF-8"?>
<resources>
<colorname="transparent">#9000</color>
</resources>
这个值设定了整个界面的透明度,为了看得见效果,现在设为透明度为56%(9/16)左右。
再在res/values/下建styles.xml,设置程序的风格
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stylename="Transparent">
<itemname="android:windowBackground">@color/transparent</item>
<itemname="android:windowIsTranslucent">true</item>
<itemname="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>
</style>
</resources>
最后一步,把这个styles.xml用在相应的Activity上。即在AndroidManifest.xml中的任意<activity>标签中添加
android:theme="@style/transparent"
如果想设置所有的activity都使用这个风格,可以把这句标签语句添加在<application>中。
最后运行程序,哈哈,是不是发现整个界面都被蒙上一层半透明了。最后可以把背景色#9000换成#0000,运行程序后,就全透明了,看得见背景下的所有东西可以却都操作无效。