您的位置:首页 > 移动开发 > Android开发

Android ProgressBar自定义图片进度,自定义渐变色进度条

2014-08-25 17:18 567 查看
PS:看了9年的小说,自己开始动手写了一本,请各位猿们动动手指,点击下,有起点账号的可以收藏下!!《武意长存》

今天在网上找了下关于关于如何自定义Progress的相关内容,找到了一篇不错的文章http://my.oschina.net/amigos/blog/59871

但是在该文章的三》自定义渐变色进度条
中我按照作者的步骤却出了点小问题

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ProgressBar
android:id="@+id/progressBar1"
android:indeterminateDrawable="@drawable/color_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="80"
android:layout_centerInParent="true" />

</RelativeLayout>


/res/drawable

<?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:angle="270"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:startColor="#ff9d9e9d" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />

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

<gradient
android:angle="270"
android:centerY="0.75"
android:centerColor="#ffff3333"
android:endColor="#ffd20000"
android:startColor="#ffff5353" />
</shape>
</clip>
</item>

</layer-list>




出现这样的结果的确挺费解的,明明已经把layout_width置为match_parent,为何只显示这么短的一小段,无论我如何更改宽带,300d、400dp等等,都无法改变。我想可能是本身主题或者样式的原因。

最后我就找了下源码:

<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>


@android:drawable/progress_horizontal:
<pre name="code" class="html"><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>
</pre>@android:drawable/progress_indeterminate_horizontal:<pre name="code" class="html"><animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/progressbar_indeterminate1" android:duration="200" />
<item android:drawable="@drawable/progressbar_indeterminate2" android:duration="200" />
<item android:drawable="@drawable/progressbar_indeterminate3" android:duration="200" />
</animation-list>



看了源码之后我就自己定义了一个样式
<pre name="code" class="html">    <style name="CustomProgressBar" parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:progressDrawable">@drawable/color_progress</item>
<item name="android:minHeight">16dp</item>
<item name="android:maxHeight">16dp</item>
</style>
</pre><pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ProgressBar
android:id="@+id/progressBar1"
style="@style/CustomProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="80"
android:layout_centerInParent="true" />

</RelativeLayout>




最后,我们还可以用图片做progressbar的背景和进度条

<?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/progress_background">

</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />

<gradient
android:angle="270"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/progress_line" android:clipOrientation="horizontal">

</clip>
</item>

</layer-list>
</pre><p></p><img src="" alt="" /><pre>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: