android解決設置getBackground().setAlpha()後其他頁面background也隨之改變問題

項目中使用到滑動漸變效果,動態設置控件setAlpha()但出現了一個問題

A控件引用color1  B控件也引用了color1 改變A的Alpha B控件也隨之改變;

解決方案:

第一種:重新創建一個color2,兩個控件引用不同資源

第二種:A.getBackground().mutate().setAlpha() 加上mutate()

    /**
     * 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 @NonNull Drawable mutate() {
        return this;
    }

google翻譯一下

使這個drawable可變。 此操作無法逆轉。 一個可變的drawable保證不與任何其他drawable共享其狀態。 當您需要修改從資源加載的drawable的屬性時,這尤其有用。 默認情況下,從同一資源加載的所有drawables實例共享一個公共狀態; 如果修改一個實例的狀態,則所有其他實例將收到相同的修改。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章