android 垂直方向进度条progressbar

本文介绍了如何在Android中实现垂直方向的进度条。通过修改原生水平进度条的XML资源文件,调整`clipOrientation`为垂直并改变`gravity`属性,可以创建一个自定义的垂直进度条。在布局文件中应用修改后的XML,最终展示出垂直进度条的效果。


在实际需求会碰到使用垂直方向的是进度条


一 水平方向进度条

1.先看看原生的水平方向进度条



<ProgressBar
        android:id="@+id/progress3"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:indeterminate="false"
        android:indeterminateOnly="false"
        android:max="100"
        android:progress="50"
        />


2.根据style="?android:attr/progressBarStyleHorizontal",我们找到源码中的style.xml


<style name="Widget.ProgressBar.Horizontal">
         <item name="android:indeterminateOnly">false</item>
         <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
         <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
         <item name="android:minHeight">20dip</item>
         <item name="android:maxHeight">20dip</item>
     </style>


3.看到<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
木有,继续发掘源码,找到drawable下面的progress_horizontal.xml,这就是我们今天的主角

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 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.
-->

<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="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>
    
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerY="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
</layer-list>



4. 把这个progress_horizontal.xml拷到我们项目下drawable,在布局文件使用如下:

<ProgressBar
        android:id="@+id/progress3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:indeterminate="false"
        android:indeterminateOnly="false"
        android:max="100"
        android:progress="50"
        android:progressDrawable="@drawable/progress_horizontal" />




 5.水平方向效果:


二.垂直方向进度条。

1.复制一份progress_horizontal.xml改名为progress_vertical.xml,内容修改为

<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="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerX="0.75"
                    android:endColor="#ff747674"
                    android:angle="90"
                    />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerX="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="90"
                        />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip  android:clipOrientation="vertical"
               android:gravity = "bottom">
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerX="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="90"
                        />
            </shape>
        </clip>
    </item>

</layer-list>


或内容为

    <item android:id="@android:id/progress">
        <clip android:clipOrientation="vertical"
              android:gravity = "bottom">
            <bitmap  android:src="@drawable/img_graduation_dtc" />
            <!--

   <shape>
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerX="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="90"
                />
            </shape>
            -->

        </clip>
    </item>
    
</layer-list>


 2为什么shape外面要包一层clip呢,官方文档解释是clipdrawable是可以自我复制的,来看看定义

<?xml version="1.0" encoding="utf-8"?>
 <clip     xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/drawable_resource"
     android:clipOrientation=["horizontal" | "vertical"]
     android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
                     "fill_vertical" | "center_horizontal" | "fill_horizontal" |
                     "center" | "fill" | "clip_vertical" | "clip_horizontal"] />
android:clipOrientation有两个属性,默认为horizontal
android:gravity有两个属性,默认为left
那我们试试改成vertical和bottom会有什么效果,新建一个progress_vertical.xml,把源码progress_horizontal.xml的内容复制过来,这里去掉了secondaryProgress,修改了clip,shape的渐变中心centerY改为centerX


3.布局文件中使用

<ProgressBar
        android:id="@+id/progress5"
        style="@style/VLineLayout"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:indeterminate="false"
        android:indeterminateOnly="false"
        android:max="100"
        android:visibility="gone"
        android:progress="50"
        android:background="@drawable/img_graduation_gray"
        android:progressDrawable="@drawable/progress_vertical" />



4 效果图如下:




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值