Android一个小知识点: 多个界面同时使用一个资源布局的时候,修改背景需要注意的坑

需求是将系统中底部的白色背景改成黑色背景

  =》

查找工程看底部调用代码是公共代码位于, idh.code\vendor\sprd\platform\frameworks\support\featurebar\*

于是将color由white修改为black,以为就好了.结果发现大部分界面如预期都好了,可是桌面部分应用底部显示还是白色,录音机应用显示为半灰色。查找代码发现这些问题界面中并未对此背景进行二次设置color,再这些界面重新设置color又好了,百思不得其解....... 今天查找了一天原因,终于从这个坑里面拔出来了…… 原因是 android中从同一个资源文件中加载出来的drawable会共享状态,如果你加载出来多个drawable,当改变了其中一个的状态时,其他drawable的状态也会相应改变。 在待机桌面界面的时候,对底部的背景alpha进行了修改:         FeatureBarUtil.setBackgroundAlpha(mFeatureBarHelper,                 Math.round(255 * (visible ? mSoftBaralpha : 1.0f)));        public static void setBackgroundAlpha(FeatureBarHelper fbh, int alpha) {         Drawable bg = getBackground(fbh);         if (bg != null) {             bg.setAlpha(alpha);         }     } 解决方案:

  方案解释: /** * Make this drawable mutable. This operation cannot be reversed. A mutable * drawable is guaranteed to not share its state with any other drawable. * This is especially useful when you need to modify properties of drawables * loaded from resources. By default, all drawables instances loaded from * the same resource share a common state; if you modify the state of one * instance, all the other instances will receive the same modification. * * Calling this method on a mutable Drawable will have no effect. * * @return This drawable. * @see ConstantState * @see #getConstantState() */ public Drawable mutate() { return this; }   翻译过来就是: 使这个drawable变得状态不定。这个操作不能还原(变为不定后就不能变为原来的状态)。一个状态不定的drawable可以保证它不与其他任何一个drawabe共享它的状态。这对于你需要更改从同一资源加载来的drawable的属性时非常有用。 认情况下,所有的从同一资源(R.drawable.XXX)加载来的drawable实例都共享一个共用的状态,如果你更改一个实例的状态,其他所有的实例都会收到相同的通知。 这个方法对于已经是mutable的drawable没有效果。   修改方案和解释参考 https://blog.csdn.net/bzlj2912009596/article/details/79707023  

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...