Android移植之自定义ProgressBar

昨天看了一下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网站上查看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值