從custom Drawable看invalidateSelf()

一、invalidateSelf()

參考(https://www.zybuluo.com/linux1s1s/note/93075)
我自己也嘗試著看源碼。不過不同的是在Drawable里面自己調(diào)用invalidateSelf(),而不是在view里面開始。
1.一個繼承ImageView的自定義View,重寫了setImageDrawble(Drawable drawable)

@Override
public void setImageDrawable(Drawable drawable) {
    Tool.LI("WeatherAnimView setImageDrawable");
    Drawable d = getDrawable();
    if (d != null && d.equals(drawable)) { 
       return;
    }
    if (d != null && d instanceof WeatherDrawable) {
        ((WeatherDrawable) d).stopAnimation();
    } 
   super.setImageDrawable(drawable); // start from here
   if (drawable != null && drawable instanceof WeatherDrawable && isShown()) {
        ((WeatherDrawable) drawable).startAnimation();
    }
}

2.從ImageView的方法setImageDrawable(drawable)開始找ViewDrawable的關(guān)系

/**
 * Sets a drawable as the content of this ImageView.
 *
 * @param drawable the Drawable to set, or {@code null}
 * to clear the content
 */
public void setImageDrawable(@Nullable Drawable drawable) {
    if (mDrawable != drawable) {
        mResource = 0;
        mUri = null;
        final int oldWidth = mDrawableWidth;
        final int oldHeight = mDrawableHeight;
        updateDrawable(drawable);
        if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
            requestLayout();
        }
        invalidate();
    }
}

3.先從updateDrawable(drawable)看起。起初被mDrawable騙了,從命名看這是ImageView的成員變量,updateDrawble(Drawable d)首先對其做了一些檢查不管。接著代碼就很明了,把傳進(jìn)來的Drawable對象賦給成員變量mDrawable。如果參數(shù)d不為空的話,那么設(shè)置d的Callback

private void updateDrawable(Drawable d) {
    if (d != mRecycleableBitmapDrawable && mRecycleableBitmapDrawable != null) {
        mRecycleableBitmapDrawable.setBitmap(null);
    }
    if (mDrawable != null) {
        mDrawable.setCallback(null);// 
        unscheduleDrawable(mDrawable);
    }
    mDrawable = d;
    if (d != null) {
        d.setCallback(this);
        d.setLayoutDirection(getLayoutDirection());
        if (d.isStateful()) {
            d.setState(getDrawableState());
        }
        d.setVisible(getVisibility() == VISIBLE, true);
        d.setLevel(mLevel);
        mDrawableWidth = d.getIntrinsicWidth();
        mDrawableHeight = d.getIntrinsicHeight();
        applyImageTint();
        applyColorMod();
        configureBounds();
    } else {
        mDrawableWidth = mDrawableHeight = -1;
    }
}

4.繼續(xù)從d.setCallback(this);看下去,以View對象新建一個弱引用new WeakReference<Callback>(cb)賦給Drawable對象的d的成員變量mCallback

/**
 * Bind a {@link Callback} object to this Drawable.  Required for clients
 * that want to support animated drawables.
 *
 * @param cb The client's Callback implementation.
 *
 * @see #getCallback()
 */public final void setCallback(Callback cb) {
    mCallback = new WeakReference<Callback>(cb);
}

正如(https://www.zybuluo.com/linux1s1s/note/93075) 所說的,類ImageView的父類View實(shí)現(xiàn)了Drawable.Callback的接口。

public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
    private static final boolean DBG = false;
    /**
     * The logging tag used by this class with android.util.Log.
     */    protected static final String VIEW_LOG_TAG = "View";
    // ...此處省略了余下代碼

查看源碼也可以看到Drawable類定義了該接口

public abstract class Drawable {

    // 此處省略了無關(guān)代碼

    private WeakReference<Callback> mCallback = null;// <---- HERE!

    // 此間省略了無關(guān)代碼

    /**
     * Implement this interface if you want to create an animated drawable that
     * extends {@link android.graphics.drawable.Drawable Drawable}.
     * Upon retrieving a drawable, use
     * {@link Drawable#setCallback(android.graphics.drawable.Drawable.Callback)}
     * to supply your implementation of the interface to the drawable; it uses
     * this interface to schedule and execute animation changes.
     */public static interface Callback {
        // 此處省略了注釋
        public void invalidateDrawable(Drawable who);

        // 此處省略了注釋
        public void scheduleDrawable(Drawable who, Runnable what, long when);

        // 此處省略了注釋
        public void unscheduleDrawable(Drawable who, Runnable what);
}

Drawable沒有實(shí)現(xiàn)任何與用戶的互動,而完全是交給View,誠如Google文檔(https://developer.android.com/reference/android/graphics/drawable/Drawable.html) 描述的,ViewDrawable各司其職。

A Drawable is a general abstraction for "something that can be drawn." Most often you will deal with Drawable as the type of resource retrieved for drawing things to the screen; the Drawable class provides a generic API for dealing with an underlying visual resource that may take a variety of forms. Unlike a View
, a Drawable does not have any facility to receive events or otherwise interact with the user.

二、Drawable Call

Google官方文檔描述DrawableinvalidateSelf()如下:

invalidateSelf()
Use the current Drawable.Callback
implementation to have this Drawable redrawn.

1.查看DrawableinvalidateSelf()源碼如下。
所以當(dāng)Drawable內(nèi)部或者其對象調(diào)用invalidateSelf()的時候,便以Drawable對象自身為參數(shù),讓類ImageView來調(diào)用實(shí)現(xiàn)了Drawable.CallbackinvalidateDrawable(Drawable who)方法。

/**
 * Use the current {@link Callback} implementation to have this Drawable
 * redrawn.  Does nothing if there is no Callback attached to the
 * Drawable.
 *
 * @see Callback#invalidateDrawable
 * @see #getCallback()
 * @see #setCallback(android.graphics.drawable.Drawable.Callback)
 */
public void invalidateSelf() {
    final Callback callback = getCallback();
    if (callback != null) {
        callback.invalidateDrawable(this);
    }
}

2.查看View是如何實(shí)現(xiàn)的invalidateDrawable(Drawable who)
如果一切“正常”,即參數(shù)dr等于成員變量mDrawable且不為空,那么最終會調(diào)用invalidate()方法。

@Override
public void invalidateDrawable(Drawable dr) {
    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;
            }
        }
        /* 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);
    }
}

3.最后invalidate()可以參考(https://www.zybuluo.com/linux1s1s/note/93075)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,663評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,125評論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,506評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,614評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,402評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,934評論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,021評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,168評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,690評論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,596評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,288評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,027評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,404評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,662評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,398評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,743評論 2 370

推薦閱讀更多精彩內(nèi)容