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

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  

标签:知识点,界面,状态,state,mutable,Android,drawable,加载
来源: https://blog.csdn.net/mickeymousemei123/article/details/111871729