[ANDROID]记Activity之间的背景getBackground设置alpha会相互影响的bug

这是一个系统的问题(bug ?)

简述现象

项目里发现有时候一些页面设置的白色背景会突然没有,没有找到复现步骤。一旦出现就很难恢复,也没有找到复现步骤。
后来发现是因为某些页面里对一个view这样操作导致:

bg.getBackground().setAlpha(0);

但出现的前提是,这个对应的bg是在xml里设置的背景color,且背景color值和受影响的页面color值相同。比如我们的项目里出问题的都是白色背景,都是在xml里这样设置:

    android:background="@color/white"

解决办法

对需要设置alpha变化的view,不在xml写android:background="@color/white"。
而是在需要用到alpha变化之前设置:

        bar.setBackgroundColor(getResources().getColor(R.color.white));
        bar.getBackground().setAlpha(0);

验证实验

第一个Activity设置一个蓝色的背景:
在这里插入图片描述
点击中间按钮进入Activity B。Activity B的layout xml里设置了相同的蓝色背景:
在这里插入图片描述
点击Activity B的按钮改变背景色的alpha:

baseView.getBackground().setAlpha(0);

在这里插入图片描述
退出Activity B,返回Activity A发现背景也没了。
在这里插入图片描述
进一步试验,在Activity B调用setAlpha(100),返回Activity A,背景的变化也是一样的:
在这里插入图片描述
返回后背景一样是半透明的蓝色:
在这里插入图片描述

最后

这个问题,测试发现,华为,小米,OV都一致。所以应该是android系统本身的设计导致。

Activity A布局xml源码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/blue"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/bg_alpha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bg alpha" />
</LinearLayout>

Activity B布局xml源码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/base_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    android:orientation="vertical"
    tools:context=".BgAlphaActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Activity B" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="click to change" />
</LinearLayout>

Activity B改变背景alpha源码:

public class BgAlphaActivity extends AppCompatActivity {

    private View baseView;
    private int a = 25;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bg_alpha);
        baseView = findViewById(R.id.base_layout);
        baseView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAlpha(v);
            }
        });
    }

    public void setAlpha(View view) {
        if (++a > 25) {
            a = 0;
        }
        baseView.getBackground().setAlpha(a*10);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值