Android移植之自訂ProgressBar

來源:互聯網
上載者:User

昨天看了一下progressbar,今天總結一下。

ProgressBar這個類在/froyo/frameworks/base/core/java/android/widget/ProgressBar.java

當你去new一個ProgressBar的時候需要傳進去參數,我們從這裡看一下它執行的流程。

本篇的主要意圖還是告訴你如何修改style去改變樣式,如果你想diy自己的ProgressBar,相信對作移植的朋友有所協助。

ProgressBar.java


    public ProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, com.android.internal.R.attr.progressBarStyle);

        //如果你只是給出ProgressBar的參數集,那麼就會去找預設的那個progressBar的style;

        //我們就以預設的style作為樣本,從這裡去調下面的
構造方法

     }

    public ProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mUiThreadId = Thread.currentThread().getId();

        //這裡不多說,我們需要UI線程才能畫圖

        initProgressBar();

        TypedArray a =
            context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyle, 0);
        //這裡才是關鍵,通過傳過來的defstyle的名字
com.android.internal.R.attr.progressBarStyle
找到themes.xml中的item

        //通過這個item的name去styles.xml中找相應的style,具體過程如下:

       

        //R.styleable.ProgressBar是聲明參數列表和參數類型的,用於跟前面的attrs匹配,封裝成TypedArray

        //這個過程在 /froyo/frameworks/base/core/java/android/content/Context.java

        //           /froyo/frameworks/base/core/java/android/content/res/Resource.java

        //裡面可以找到。

        //下面就是初始化一些參數了,不必細述。

        ...
    }
attrs.xml  聲明了ProgressBar這個styleable參數列表其他的widget下的公用控制項也是如此。

             R.styleable.ProgressBar

    <declare-styleable name="ProgressBar">
        <!-- Defines the maximum value the progress can take. -->
        <attr name="max" format="integer" />
        <!-- Defines the default progress value, between 0 and max. -->
        <attr name="progress" format="integer" />
        <!-- Defines the secondary progress value, between 0 and max. This progress is drawn between
             the primary progress and the background.  It can be ideal for media scenarios such as
             showing the buffering progress while the default progress shows the play progress. -->
        <attr name="secondaryProgress" format="integer" />
        <!-- Allows to enable the indeterminate mode. In this mode the progress
         bar plays an infinite looping animation. -->
        <attr name="indeterminate" format="boolean" />
        <!-- Restricts to ONLY indeterminate mode (state-keeping progress mode will not work). -->
        <attr name="indeterminateOnly" format="boolean" />
        <!-- Drawable used for the indeterminate mode. -->
        <attr name="indeterminateDrawable" format="reference" />
        <!-- Drawable used for the progress mode. -->
        <attr name="progressDrawable" format="reference" />
        <!-- Duration of the indeterminate animation. -->
        <attr name="indeterminateDuration" format="integer" min="1" />
        <!-- Defines how the indeterminate mode should behave when the progress
        reaches max. -->
        <attr name="indeterminateBehavior">
            <!-- Progress starts over from 0. -->
            <enum name="repeat" value="1" />
            <!-- Progress keeps the current value and goes back to 0. -->
            <enum name="cycle" value="2" />
        </attr>
        <attr name="minWidth" format="dimension" />
        <attr name="maxWidth" />
        <attr name="minHeight" format="dimension" />
        <attr name="maxHeight" />
        <attr name="interpolator" format="reference" />
    </declare-styleable>

themes.xml 定義了一些索引式的item,通過這些item可以找到相應的style,通過下面的紅色部分可以去styles.xml中找對應的style

     <style>

        <item name="progressBarStyle">@android:style/Widget.ProgressBar
</item>

     <style>

styles.xml 定義了各種style的內容,包含圖片,animation,尺寸等,通過下面紅字部分可以找到圖片

    <style name="Widget.ProgressBar">
        <item name="android:indeterminateOnly">true</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_medium_white
</item>
        <item name="android:indeterminateBehavior">repeat</item>
        <item name="android:indeterminateDuration">3500</item>
        <item name="android:minWidth">48dip</item>
        <item name="android:maxWidth">48dip</item>
        <item name="android:minHeight">48dip</item>
        <item name="android:maxHeight">48dip</item>
    </style>
drawable/
progress_medium_white.xml

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white_48"
    android:pivotX="50%"
    android:pivotY="50%"
    android:framesCount="12"
    android:frameDuration="100" />

找到這裡應該就可以結束本篇了。想要知道各種參數的意義,可以去api網站上查看。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.