Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何修复零星的可拉伸着色?_Android_Android Support Library_Android Drawable - Fatal编程技术网

Android 如何修复零星的可拉伸着色?

Android 如何修复零星的可拉伸着色?,android,android-support-library,android-drawable,Android,Android Support Library,Android Drawable,我正在使用android支持库,除了使用主题中定义的accentColor着色的有限数量的小部件外,我还想着色我在整个应用程序中使用的一些其他绘图工具。 我通过以下代码执行此操作: public class ActivityTest extends AppCompatActivity { protected void tintDrawable(int color, int drawableId) { Drawable drawable = DrawableCompat

我正在使用android支持库,除了使用主题中定义的accentColor着色的有限数量的小部件外,我还想着色我在整个应用程序中使用的一些其他绘图工具。 我通过以下代码执行此操作:

public class ActivityTest extends AppCompatActivity {

    protected void tintDrawable(int color, int drawableId) { 
        Drawable drawable = DrawableCompat.wrap(ContextCompat.getDrawable(this, drawableId));
        DrawableCompat.setTint(drawable, color);
    }

    protected void tintDrawables() {
        int colorPrimary = getColorFromAttributeId(this, R.attr.colorPrimary);
        tintDrawable(colorPrimary, R.drawable.drawable_1);
        tintDrawable(colorPrimary, R.drawable.drawable_2);
        tintDrawable(colorPrimary, R.drawable.drawable_3);
        tintDrawable(colorPrimary, R.drawable.drawable_4);
        tintDrawable(colorPrimary, R.drawable.drawable_5);
        tintDrawable(colorPrimary, R.drawable.drawable_6);
    }


    public static int getColorFromAttributeId(Context context, int resourceAttributeId) {
        int[] attributes = new int[] { resourceAttributeId }; 
        TypedArray typedArray = context.obtainStyledAttributes(attributes);     
        int color = typedArray.getColor(0, Color.TRANSPARENT);
        typedArray.recycle();
        return color;
    }

    protected void doSetPreferencedThemeOnCreateSetContentView(Bundle savedInstanceState, int layoutResourceId) {
        /* Catch 22: 
         * This order must be used otherwise an exception is thrown or theme is not applied.
         *  1. setTheme()
         *  1.b tintDrawables only after the theme is known we can get the color
         *  2. onCreate()
         *  3. setContentView()      *  
         *  4. setSupportActionBar() */
        // Apply theme. This must be done before super.onCreate()
        setTheme(R.style.Theme_Custom_Theme);   // in real case theme is retrieved from preferences
        tintDrawables();
        super.onCreate(savedInstanceState);
        setContentView(layoutResourceId);
    }


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        doSetPreferencedThemeOnCreateSetContentView(savedInstanceState, R.layout.activity_test);
    }

}
它看起来很复杂,但很容易理解。问题是,我的抽屉是“随机”着色的。有些是,有些不是,并保留原始png中设置的颜色。更糟糕的是,如果我离开活动回来,结果可能会不同。可能会有更多正确着色或更少着色的抽绳。 我猜着色是异步的,并且在着色应用于所有可绘制对象之前创建布局。但这只是一个猜测。 这个问题很烦人,因为我的UI依赖于正确的色调,白色背景上的白色图标没有多大用处。两者都不是黑对黑。
如何对可拉丝进行正确着色?

我知道这很旧,但我遇到了同样的问题。对drawable调用mutate()可确保它不会与任何其他drawable共享其状态。从文档中:

默认情况下,从同一资源加载的所有drawables实例共享一个公共状态;如果修改一个实例的状态,所有其他实例都将收到相同的修改


在我的例子中,这导致了随机的着色问题。

关于这方面的任何信息?很可能是另一个安卓bug。