Android Lollipop 将布局的 alpha 重置为透明

标签 android android-layout android-5.0-lollipop android-4.4-kitkat alpha

我正在尝试创建一个可以改变背景 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/

相关文章:

android - SDK更新失败

Android 应用程序保持黑色但继续工作

android - Lollipop 上 Genymotion 中的 Google Play 服务(Android 5.0)

android-webview - Android WebView 在 Android-L 上以编程方式设置代理

Android + CardView 为非 L 版本增加边距?

android - 更新 Robolectric 2.4 : Getting application tag error for library projects in eclipse

android - 使用 GoogleServices 插件时忽略 Gradle ArchivesBaseName

Android 短信按号码阅读

android - 如何高效地实现gridview布局

android - 如果由 android :layout_width ="match_parent" 设置 getWidth() 返回 0