在Android中,Activity设置透明背景只要在清单文件中使用
android:theme=”@android:style/Theme.Translucent”
即可。
然,当Activity继承于AppCompatActivity时,只能使用Theme.AppCompat下的主题。而这些主题并没有Translucent.提示错误:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
跟踪进Theme.Translucent,发现该主题其实是
<item name="windowBackground">@color/transparent</item>
<item name="colorBackgroundCacheHint">@null</item>
<item name="windowIsTranslucent">true</item>
<!-- Note that we use the base animation style here (that is no
animations) because we really have no idea how this kind of
activity will be used. -->
<item name="windowAnimationStyle">@style/Animation</item>
直接丢到layout里,不起任何作用(没有设置windowAnimationStyle,因为没有Animation,但这是设置动画的应该没什么影响)
继续查源码,发现这些属性是修改Window的。查源码,先查Window(android.view),没有windowIsTranslucent关键字,比较失望.
那就查Activity吧。
搜索windowIsTranslucent,只找到注释有两个,一个转为全屏,一个转为非透明。
搜索Translucent,倒是发现了个有意思的东西– TranslucentConversionListener (透明转化监听?)接口,有一个方法onTranslucentConversionComplete。搜索,发现其实现为:
void onTranslucentConversionComplete(boolean drawComplete) {
if (mTranslucentCallback != null) {
mTranslucentCallback.onTranslucentConversionComplete(drawComplete);
mTranslucentCallback = null;
}
if (mChangeCanvasToTranslucent) {
WindowManagerGlobal.getInstance().changeCanvasOpacity(mToken, false);
}
}
mTranslucentCallback为一个 TranslucentConversionListener ,该对象只在 convertToTranslucent 的参数列表中出现,也没有过任何创建的地方。阅读该方法的注释,发现他是将不透明转换回透明,貌似找对头了
public void convertToTranslucent(TranslucentConversionListener callback) {
try {
mTranslucentCallback = callback;
mChangeCanvasToTranslucent =
ActivityManagerNative.getDefault().convertToTranslucent(mToken);
} catch (RemoteException e) {
// pass
}
}
ActivityManagerNative是什么鬼???只知道有ActivityManager…查ActivityManager吧。
发现了一堆ActivityManagerNative.getDefault().XXX,就是没有ActivityManagerNative.getDefault()。在网上查发现这是一个隐藏类,会获得一个IActivityManager接口,然而没看源码不知道怎么实现…看有人使用反射,但又不知道参数…
既然发现Activity有对应的功能,那就查AppCompatActivity怎么把它弄没了吧..
AppCompatActivity继承关系:
AppCompatActivity<-FragmentActivity<-BaseFragmentActivityJB<-BaseFragmentActivityHoneycomb<-BaseFragmentActivityEclair<-BaseFragmentActivityDonut<-Activity
从后往前挨个找convertToTranslucent,没有…找Translucent,没有…
…什么鬼…先用着Activity吧…或许以后更深入学习 才能实现,难道透明主题与AppCompatActivity一些新特性有冲突?为何取消?
希望有高手来帮忙解惑