Android 重写系统进度条


自定义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安全卫士拷贝了一个(虽然个人不太喜欢这个软件的霸道,但图片还是挺喜欢的,嘿嘿)

  • 1
  • 2
  • 下一页

相关内容