android 自訂水平和圓形progressbar 只定義一些style就可以

來源:互聯網
上載者:User

標籤:

:



修改progressbar 讓他滿足自己的需求一般來說只修改一下,progressbar的style就可以滿足需求了:

activity代碼:僅僅顯示一個布局而已

package com.example.testcustomprogressbar;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

xml介面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.testcustomprogressbar.MainActivity" >    <ProgressBar        android:id="@+id/my_customprogressbar1"        style="@style/mycustom_progressbar1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true" />    <ProgressBar        android:id="@+id/my_customprogressbar2"        style="@style/mycustom_progressbar2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/my_customprogressbar1"        android:layout_below="@+id/my_customprogressbar1"        android:layout_marginTop="26dp" />    <ProgressBar        android:id="@+id/my_customprogressbar3"        style="@style/mycustom_progressbar3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/my_customprogressbar2"        android:layout_below="@+id/my_customprogressbar2"        android:layout_marginTop="26dp" />         <ProgressBar        android:id="@+id/my_customprogressbar4"        style="@style/mycustom_progressbar_hor"        android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_below="@+id/my_customprogressbar3"        android:layout_centerHorizontal="true"        android:layout_marginTop="22dp"        android:max="100"        android:progress="40"        android:secondaryProgress="60" />        <ProgressBar        android:id="@+id/my_customprogressbar5"        style="@style/mycustom_progressbar_hor2"        android:layout_width="200dp"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/my_customprogressbar4"        android:layout_below="@+id/my_customprogressbar4"        android:layout_marginTop="26dp"        android:max="100"        android:progress="40"        android:secondaryProgress="60" />   </RelativeLayout>

第一種圓形progressbar 僅僅讓一個圖片旋轉起來:

style的代碼:

 <style name="mycustom_progressbar1">        <item name="android:indeterminateDrawable">@drawable/progressbar_ver_image</item>        <item name="android:minHeight">30dp</item>        <item name="android:minWidth">30dp</item>        <item name="android:maxHeight">60dp</item>        <item name="android:maxWidth">60dp</item>    </style>

progressbar_ver_image的代碼:

<?xml version="1.0" encoding="utf-8"?><!-- 定義一個旋轉的動畫 ,圖片是我們自訂的--><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/resizeapi"    android:fromDegrees="0"    android:pivotX="50%"    android:pivotY="50%"    android:toDegrees="360" ></rotate>


第二種圓形progressbar 使用一個動畫:

style:

 <style name="mycustom_progressbar2">        <item name="android:indeterminateDrawable">@drawable/progressbar_ver_animation</item>        <item name="android:minHeight">60dp</item>        <item name="android:minWidth">60dp</item>        <item name="android:maxHeight">100dp</item>        <item name="android:maxWidth">100dp</item>    </style>

progressbar_ver_animation的代碼:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:drawable="@drawable/ok1"        android:duration="250"/>    <item        android:drawable="@drawable/ok2"        android:duration="250"/>    <item        android:drawable="@drawable/ok3"        android:duration="250"/>    <item        android:drawable="@drawable/ok4"        android:duration="250"/></animation-list>


第三種圓形progressbar 使用shape來構建drawable:

<style name="mycustom_progressbar3">        <item name="android:indeterminateDrawable">@drawable/progressbar_ver_shape</item>        <item name="android:minHeight">60dp</item>        <item name="android:minWidth">60dp</item>        <item name="android:maxHeight">100dp</item>        <item name="android:maxWidth">100dp</item>    </style>

progressbar_ver_shape的代碼:

<?xml version="1.0" encoding="utf-8"?><!-- 定義一個旋轉的動畫 ,圖片是我們自訂的shape --><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:fromDegrees="0"    android:pivotX="50%"    android:pivotY="50%"    android:toDegrees="360" >    <shape        android:innerRadiusRatio="3"        android:shape="ring"        android:thicknessRatio="8"        android:useLevel="false" >        <gradient            android:centerColor="#FFFFFF"            android:centerY="0.50"            android:endColor="#1E90FF"            android:startColor="#000000"            android:type="sweep"            android:useLevel="false" />    </shape></rotate>

第四種: 橫向progressbar 複製粘貼系統的style,然後修改:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">       <item android:id="@android:id/background">        <shape>            <corners android:radius="5dip" />            <gradient                    android:startColor="#ffff0000"                    android:centerColor="#ffff0000"                    android:centerY="0.75"                    android:endColor="#ffff0000"                    android:angle="270"            />        </shape>    </item>       <item android:id="@android:id/secondaryProgress">        <clip>            <shape>                <corners android:radius="5dip" />                <gradient                        android:startColor="#800000ff"                        android:centerColor="#800000ff"                        android:centerY="0.75"                        android:endColor="#800000ff"                        android:angle="270"                />            </shape>        </clip>    </item>       <item android:id="@android:id/progress"        >        <clip >                       <shape>                <corners android:radius="5dip" />                <gradient                        android:startColor="#ff00ff00"                        android:centerColor="#ff00ff00"                        android:centerY="0.75"                        android:endColor="#ff00ff00"                        android:angle="270"                />            </shape>        </clip>    </item>   </layer-list>

這樣直接在progressbar中引用該style就可以滿足需求了,當然我們也可以把progressbar background progress secondprogress都分別定義成一個drawable,然後使用:

background:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >    <corners android:radius="5dip" />    <gradient        android:angle="270"        android:centerColor="#ffff0000"        android:centerY="0.75"        android:endColor="#ffff0000"        android:startColor="#ffff0000" /></shape>

progress:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >    <corners android:radius="5dip" />    <gradient        android:angle="270"        android:centerColor="#ff00ff00"        android:centerY="0.75"        android:endColor="#ff00ff00"        android:startColor="#ff00ff00" /></shape>

secondprogress:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >    <corners android:radius="5dip" />    <gradient        android:angle="270"        android:centerColor="#800000ff"        android:centerY="0.75"        android:endColor="#800000ff"        android:startColor="#800000ff" /></shape>

這個時候style中的代碼就會清晰一點:

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:id="@android:id/background"        android:drawable="@drawable/progressbar_hor_bg">    </item>    <item android:id="@android:id/secondaryProgress">        <clip android:drawable="@drawable/progressbar_hor_second_progress" >        </clip>    </item>    <item android:id="@android:id/progress">        <clip android:drawable="@drawable/progressbar_hor_progress" >        </clip>    </item>    <!-- progress 這個地方原作者寫的是一個點9的圖-->      <!-- <item android:id="@android:id/progress">          <clip>              <nine-patch android:src="@drawable/progress_patch_galy" />          </clip>      </item>   --></layer-list>


嘿嘿..最後,做人要厚道:神一般男人的原博主博文串連http://blog.csdn.net/mad1989/article/details/38042875


對了,代碼還是貼上來吧.總感覺在ide中直接看代碼會更舒服一些:http://download.csdn.net/detail/u010399316/9000197


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

android 自訂水平和圓形progressbar 只定義一些style就可以

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.