android 重写系统进度条

来源:互联网 发布:h3c路由器端口设置 编辑:程序博客网 时间:2024/06/06 09:09
自定义progressbar
现在要自定义一个等待的时候转动的小圈,相信大家也都嫌系统自带的很难看吧??
如果要自定义那些系统的组件都有一个法子,那就是看系统的是怎么写的。
看下系统的progressbar的方法:
首先看android的系统的style.xml的文件,系统的样式定义都在里面 android-sdk-windows\platforms\android-8\data\res\values 目录下打开style.xml,搜索ProgressBar。
可以看到系统是这样定义progressbar的:
    <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>


接下来我们关注下        <item name="android:indeterminateDrawable">@android:drawable/progress_medium_white</item> 这一行。可以看到它使用了android:drawable/progress_medium_white这样的一个资源
找到这个文件并且打开,我们可以看到:
<?xml version="1.0" encoding="utf-8" ?> - <!-- /***** Copyright 2009, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ****     http://www.apache.org/licenses/LICENSE-2.0 **** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License.*/  -->   <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" /> 


我把前面的注释去掉,大家再看:
<?xml version="1.0" encoding="utf-8" ?>   <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" /> 


就剩这么多了,然后分析下这个文件(总共没有几行代码了嘛)
xmlns:android="http://schemas.android.com/apk/res/android" 约束,不说了,也不需要我们关注
android:drawable="@drawable/spinner_white_48"这个相信接触过android的都知道这是指定了一个图标吧
android:pivotX="50%" 
android:pivotY="50%" 这两行代码是指定了一个点(point嘛)那是什么点呢,中心点,我们让那个圆圈围着这个点转动,就有了动画效果,所以它是指定的围绕哪个点转动(为了证明我的猜想,我在后来自定义的代码中将他们都改成了0,它们就围绕左上角的那个点转动了,所以证明了我的猜想是对的哦,不信的朋友可以再写完以后自己试一下)
android:framesCount="12"  这个是啥帧的count我也不太清楚了
android:frameDuration="100"这个应该是转圈持续的时间,我们可以在做完后改一改这些数字,就知道他们干嘛的啦。


看完这个文件,我们想,已经没有用到其他的文件了,只是缺少一个图标,我到360安全卫士拷贝了一个(虽然个人不太喜欢这个软件的霸道,但图片还是挺喜欢的,嘿嘿)




总结下系统是怎么定义一个progressbar的
1:定义了一个样式:style,style中使用了一个属性:progress_medium_white
2:定义一个属性progress_medium_white.xml,就这两步就完工啦。


好,那光说不练假把式,接下来就去实现下吧:
1:在我们工程的  res/values下新建一个styles.xml的文件,并拷贝progressbar的样式过来
<?xml version="1.0" encoding="utf-8"?><resources>    <style name="test_progressbar_style"><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></resources>


我就不去修改它的属性了,大小啥的,想修改的话自己研究下不会很难哦。
没有写过style的朋友注意下,这个xml文件,申明不说,节点是 resource,style需要申明name name="test_progressbar_style"这个就是以后要引用这个style的名字啦


2:当然现在会报错,因为progress_medium_white这个还没有建嘛
在res下新建 drawable目录
在目录下新建progress_medium_white.xml文件,把系统的拷贝进来:
<?xml version="1.0" encoding="utf-8" ?>   <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" /> 


再把准备好的图片拷贝到drawable-hdpi,或者任意其他目录下。
android:drawable="@drawable/spinner_white_48" 后面的名称改成图片的名字就成功了。


3:然后可以在布局文件中定义一个试一下了哦
            <ProgressBar                style="@style/test_progressbar_style"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />


没白忙活吧,这个的修改虽然很简单,但是我们掌握了这个修改方法,举一反三,就可以把系统的任意样式改成我们喜欢的了哦



原创粉丝点击