bilibili / MagicaSakura

MagicaSakura 是 Android 多主题框架。~ is an Android multi theme library which supporting both daily colorful theme and night theme.
http://app.bilibili.com
Apache License 2.0
3.55k stars 468 forks source link

fix AppCompatImageHelper applySupportImageTint VectorDrawable bug #54

Open Caij opened 6 years ago

Caij commented 6 years ago

VectorDrawableCompat invalidateSelf -> mDelegateDrawable(BitmapDrawable) invalidateSelf ->ImageView invalidateDrawable

    public void invalidateDrawable(@NonNull Drawable dr) {
        dr is 是BitmapDrawable  mDrawable是VectorDrawableCompat  不相等 所以不会重绘。
        if (dr == mDrawable) { 
            if (dr != null) {
                // update cached drawable dimensions if they've changed
                final int w = dr.getIntrinsicWidth();
                final int h = dr.getIntrinsicHeight();
                if (w != mDrawableWidth || h != mDrawableHeight) {
                    mDrawableWidth = w;
                    mDrawableHeight = h;
                    // updates the matrix, which is dependent on the bounds
                    configureBounds();
                }
            }
            /* we invalidate the whole view in this case because it's very
             * hard to know where the drawable actually is. This is made
             * complicated because of the offsets and transformations that
             * can be applied. In theory we could get the drawable's bounds
             * and run them through the transformation and offsets, but this
             * is probably not worth the effort.
             */
            invalidate();
        } else {
            super.invalidateDrawable(dr);
        }
    }

这个问题最终是VectorDrawableCompat的是的问题 已经提交官方修改 https://issuetracker.google.com/issues/110174719

xyczero commented 6 years ago
  1. 关于刷新不生效的原因我有一点不同的看法,VectorDrawableCompat并没有给它内部的mDelegateDrawable设置setCallback的回调,然后导致不会刷新的原因是不是例子中的BitmapDrawable的getCallback为空,所以就没走到ImageView的invalidateDrawable方法,当然如果走进该方法后,就如您所说也是不会真正生效的。
  2. VectorDrawableCompat这个bug相对只是个特例,所以建议可以对mDelegateDrawable特殊处理下,一般的时候invalidateSelf效率更高些