歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 重寫系統進度條

Android 重寫系統進度條

日期:2017/3/1 10:03:31   编辑:Linux編程

自定義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安全衛士拷貝了一個(雖然個人不太喜歡這個軟件的霸道,但圖片還是挺喜歡的,嘿嘿)

Copyright © Linux教程網 All Rights Reserved