垂直进度条VerticalProgressBar的实现

来源:互联网 发布:文字图片编辑软件 编辑:程序博客网 时间:2024/06/05 15:11
第一次写自定义,这个还是比较简单,查了下一般自定义都需要重写onDraw()和onMeasure()方法,这里也无例外。本例中只需在原ProgressBar源码的基础上做一些简单的修改。
原理就是将水平滚动条经过旋转竖起来,需要注意的是经旋转后原来的高变成了现在的宽,高变成了现在的宽。

代码如下:
public class VerticalProgressBar extends ProgressBar
{

    public VerticalProgressBar(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public VerticalProgressBar(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public VerticalProgressBar(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected synchronized void onDraw(Canvas canvas)
    {
        // TODO Auto-generated method stub
        canvas.rotate(-90);//反转90度,将水平ProgressBar竖起来
        canvas.translate(-getHeight(), 0);//将经过旋转后得到的VerticalProgressBar移到正确的位置,注意经旋转<span style="white-space:pre">                        </span>    后宽高值互换
        super.onDraw(canvas);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());//互换宽高值
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldw, oldh);//互换宽高值
    }
}

如果需要修改style可以使用android自带的style="?android:attr/progressBarStyleHorizontal"。
源码如下:
<pre name="code" class="java">   
    <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>
        <item name="android:mirrorForRtl">true</item>
    </style>
如果对上面style不满意,可以自己定义style,只需要把上面的item重写即可,android:xxxHeight在此指垂直进度条的宽度,最重要的就是
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
这个<item>,先来看下progress_horizontal的源码:
<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>
这里面有3个item,分别为:background、secondProgress、progress,看名字就能知道其大概作用,我们比较关心的应该是后两个,把这个文件copy一份到自己的项目下,就可以随心所欲的修改shape属性:圆角、渐变等等。
background、secondProgress、progress:
progress是当前进度,滑块位置那个
secondaryprogess也是个进度条,可以看做是总进度条之类的,稍浅色点那个
比如安装软件的时候,安装很多组件,progress显示各个组件的安装情况,
后面 的secondaryprogess可以显示整个软件安装进度
红色的是背景,你看看就知道
0 0
原创粉丝点击