自定義View面試總結(jié)

本著針對(duì)面試,不負(fù)責(zé)任的態(tài)度,寫下《面試總結(jié)》系列。本系列記錄面試過程中各個(gè)知識(shí)點(diǎn),而不是入門系列,如果有不懂的自行學(xué)習(xí)。


轉(zhuǎn)載請(qǐng)標(biāo)明出處,本文地址: http://www.itdecent.cn/p/a13458744a02

不負(fù)責(zé)任系列


自定義View三種方式,組合現(xiàn)有控件,繼承現(xiàn)有控件,繼承View

本文只針對(duì)繼承View的方式,另兩種自行學(xué)習(xí)。

1. 重寫方法

onMeasure、 onLayout、onDraw、onTouchEvent

onMeasure

可能多次觸發(fā),在measure的過程中注意MeasureSpec,specMode、specSize
講到LinearLayout、RelativeLayout源碼

MeasureSpec

MeasureSpec,specMode、specSize

  1. EXACTLY

表示父布局希望子布局的大小應(yīng)該是由specSize的值來決定的,系統(tǒng)默認(rèn)會(huì)按照這個(gè)規(guī)則來設(shè)置子布局的大小,開發(fā)人員當(dāng)然也可以按照自己的意愿設(shè)置成任意的大小。

  1. AT_MOST

表示子布局最多只能是specSize中指定的大小,開發(fā)人員應(yīng)該盡可能小得去設(shè)置這個(gè)布局,并且保證不會(huì)超過specSize。系統(tǒng)默認(rèn)會(huì)按照這個(gè)規(guī)則來設(shè)置子布局的大小,開發(fā)人員當(dāng)然也可以按照自己的意愿設(shè)置成任意的大小。

  1. UNSPECIFIED

表示開發(fā)人員可以將布局按照自己的意愿設(shè)置成任意的大小,沒有任何限制。這種情況比較少見,不太會(huì)用到。

childParams/parentMode EXACTLY AT_MOST UNSPECIFIED
dp/px EXACTLY(childsize) EXACTLY(childsize) EXACTLY(childsize)
match_parent EXACTLY(parentsize) AT_MOST(parentsize) UNSPECIFIED(0)
wrap_content AT_MOST(parentsize) AT_MOST(parentsize) UNSPECIFIED(0)

上圖表摘自https://blog.csdn.net/singwhatiwanna/article/details/38426471

onLayout

在ViewGroup中,只觸發(fā)一次,決定子View的位置

onDraw

繪制內(nèi)容,Canvas.drawxxx(),paint

onTouchEvent

處理點(diǎn)擊事件

2. 自定義view與viewgroup的區(qū)別

  1. onDraw(Canvas canvas)

View類中用于重繪的方法,這個(gè)方法是所有View、ViewGroup及其派生類都具有的方法,也是Android UI繪制最重要的方法。開發(fā)者可重載該方法,并在重載的方法內(nèi)部基于參數(shù)canvas繪制自己的各種圖形、圖像效果。

  1. onLayout()

重載該類可以在布局發(fā)生改變時(shí)作定制處理,這在實(shí)現(xiàn)一些特效時(shí)非常有用。View中的onLayout不是必須重寫的,ViewGroup中的onLayout()是抽象的,自定義ViewGroup必須重寫。

  1. dispatchDraw()

ViewGroup類及其派生類具有的方法,控制子View繪制分發(fā),重載該方法可改變子View的繪制,進(jìn)而實(shí)現(xiàn)一些復(fù)雜的視效,典型的例子可參見Launcher模塊Workspace的dispatchDraw重載。

  1. drawChild()

ViewGroup類及其派生類具有的方法,直接控制繪制某局具體的子view,重載該方法可控制具體某個(gè)具體子View。

3. View方法執(zhí)行過程

三次measure,兩次layout和一次draw
http://blog.csdn.net/u012422440/article/details/52972825

Android視圖樹的根節(jié)點(diǎn)是DecorView,而它是FrameLayout的子類,所以就會(huì)讓其子視圖繪制兩次,所以onMeasure函數(shù)會(huì)先被調(diào)用兩次。

  • onResume(Activity)
  • onPostResume(Activity)
  • onAttachedToWindow(View)
  • onMeasure(View)
  • onMeasure(View)
  • onLayout(View)
  • onSizeChanged(View)
  • onMeasure(View)
  • onLayout(View)
  • onDraw(View)
  • dispatchDraw()

4. invalidate()、postInvalidate()、requestLayout()

invalidate()

/**
     * Invalidate the whole view. If the view is visible,
     * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
     * the future.
     * <p>
     * This must be called from a UI thread. To call from a non-UI thread, call
     */
    public void invalidate() {
        invalidate(true);
    }

invalidate方法會(huì)執(zhí)行draw過程,重繪View樹。
當(dāng)改變view的顯隱性、背景、狀態(tài)(focus/enable)等,這些都屬于appearance范疇,都會(huì)引起invalidate操作。需要更新界面顯示,就可以直接調(diào)用invalidate方法。

注意:

View(非容器類)調(diào)用invalidate方法只會(huì)重繪自身,ViewGroup調(diào)用則會(huì)重繪整個(gè)View樹。

postInvalidate()

/**
     * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
     * Use this to invalidate the View from a non-UI thread.</p>
     *
     * <p>This method can be invoked from outside of the UI thread
     * only when this View is attached to a window.</p>
     */
    public void postInvalidate() {
        postInvalidateDelayed(0);
    }

在子線程中被調(diào)用,刷新UI。

requestLayout()

 /**
     * Call this when something has changed which has invalidated the
     * layout of this view. This will schedule a layout pass of the view
     * tree. This should not be called while the view hierarchy is currently in a layout
     * pass ({@link #isInLayout()}. If layout is happening, the request may be honored at the
     * end of the current layout pass (and then layout will run again) or after the current
     * frame is drawn and the next layout occurs.
     *
     * <p>Subclasses which override this method should call the superclass method to
     * handle possible request-during-layout errors correctly.</p>
     */
    @CallSuper
    public void requestLayout() {
        }

當(dāng)View的寬高,發(fā)生了變化,不再適合現(xiàn)在的區(qū)域,調(diào)用requestLayout方法重新對(duì)View布局。
當(dāng)View執(zhí)行requestLayout方法,會(huì)向上遞歸到頂級(jí)父View中,再執(zhí)行這個(gè)頂級(jí)父View的requestLayout,所以其他View的onMeasure,onLayout也可能會(huì)被調(diào)用。
3

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容