我正在尝试创建一个可以改变背景 alpha 的 Activity 。
这是我得到的:
布局.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<com.liuguangqiang.swipeback.SwipeBackLayout
android:id="@+id/swipeback_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<FrameLayout
android:layout_gravity="center"
android:id="@+id/previewer_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.liuguangqiang.swipeback.SwipeBackLayout>
</FrameLayout>
样式.xml:
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="CustomStyle" parent="AppTheme">
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
AndroidManifest.xml
<activity
android:name=".activity.TheActivity"
android:screenOrientation="portrait"
android:theme="@style/CustomStyle"/>
在 Activity 的 onCreate
中,我设置了 swipeback
布局,并在拉动时放置了一个监听器来改变背景 alpha:
swipebackLayout.setDragEdge(SwipeBackLayout.DragEdge.BOTTOM);
swipebackLayout.setEnableFlingBack(true);
swipebackLayout.setEnablePullToBack(true);
swipebackLayout.setOnPullToBackListener(new SwipeBackLayout.SwipeBackListener() {
@Override
public void onViewPositionChanged(float fractionAnchor, float fractionScreen) {
float alpha = (1 - fractionAnchor) * 255;
swipebackLayout.getBackground().setAlpha((int) alpha);
}
});
问题是:
在 Kitkat 设备上,这非常有效:当您拉动布局时,它会同时改变 alpha,从全黑变为透明。
但是,在 Lollipop 设备上,此效果仅在第一次有效。第二次打开同一个 Activity 时,alpha 在开始时被重置为 0(透明),我每次都必须在 onCreate
中明确设置背景 alpha(使用下面附带的单行代码).
问题
为什么会这样?由于 Activity 已正确完成并且每次都是从新创建的,为什么第一次创建时 alpha 为 255,然后在第二次创建时它神奇地变为 0?
// Window background is transparent, so we need to set alpha to opaque when creating the activity, without this line, lollipop devices will have a complete transparent background next time you launch the activity
swipebackLayout.getBackground().setAlpha(255);
// ------------
最佳答案
当您第一次获取Drawable
时,Android 资源框架会在进程本地缓存中保留一个引用。因此,您执行的任何修改(例如任何 setZzz()
调用)也会修改缓存中的版本。
为避免意外修改缓存版本,在调用任何 setZzz()
之前,您应该始终在 Drawable
上至少调用一次 mutate()
> 方法。
多次调用 mutate()
是空操作,因此您可以在所有 setter 调用前加上它。
所以在这种特殊情况下,你会想要:
swipebackLayout.getBackground().mutate().setAlpha(...);
注意:无法保证调用mutate()
失败将允许您修改缓存版本,或者调用getDrawable()
将始终返回缓存版本。但是,始终保证调用 mutate()
可以让您安全地修改可绘制对象。
关于Android Lollipop 将布局的 alpha 重置为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29230685/