Android自定义控件实现环形播放进度条

导读:我们就可以再Android的基础控件上实现我们想要的功能或者自定义的外观。以ProgressBar为例,对于可调节的进度条似乎只有长条形的ProgressBar

 

先上源代码:http://download.csdn.net/detail/ytm52513/5746217

 

 

Android提供了很多基本的控件实现,但不是一个完整、强大的实现。可幸的是,Android提供了自定义控件的实现,有了自定义控件,我们就可以再Android的基础控件上实现我们想要的功能或者自定义的外观。以ProgressBar为例,对于可调节的进度条似乎只有长条形的ProgressBar(圆形的貌似都是不停转动的,有说的不当的地方不要拍砖啊)假如我们想要一个可调节进度的圆形进度条呢。。。
 

下面我们直接切入主题
且看红色LinearLayout里的四个自定义控件的配置参数

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <com.genius.progress.RoundProgressBar
            android:id="@+id/roundBar1"
            android:layout_width="96dp"
            android:layout_height="96dp"
            roundProgress:max="100" />

        <com.genius.progress.RoundProgressBar
            android:id="@+id/roundBar2"
            android:layout_width="96dp"
            android:layout_height="96dp"
            roundProgress:Paint_Width="20"
            roundProgress:fill="false"
            roundProgress:max="100" />

        <com.genius.progress.RoundProgressBar
            android:id="@+id/roundBar3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/background3"
            roundProgress:Inside_Interval="3"
            roundProgress:Paint_Color="0xff0000ff"
            roundProgress:Paint_Width="4"
            roundProgress:Show_Bottom="false"
            roundProgress:fill="false"
            roundProgress:max="100" />

        <com.genius.progress.RoundProgressBar
            android:id="@+id/roundBar4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/background2"
            roundProgress:Inside_Interval="10"
            roundProgress:Paint_Color="0xffaa55aa"
            roundProgress:Paint_Width="4"
            roundProgress:Show_Bottom="false"
            roundProgress:fill="true"
            roundProgress:max="100" />
    </LinearLayout>


roundProgress是我自定义的属性前缀 布局配置文件的前几行是这么写的

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:roundProgress="http://schemas.android.com/apk/res/com.genius.demo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >


注意这一行 xmlns:roundProgress="http://schemas.android.com/apk/res/com.genius.demo“ roundProgress就是在这里定义的 com.genius.demo是R.java文件所在的包名 该类自定义属性的参数配置文件如下

 

<?xml version="1.0" encoding="utf-8"?>  
<resources> 
 
    <declare-styleable name="RoundProgressBar">  
        <attr name="max" format="integer"/>	
        <attr name="fill" format="boolean"/>			<!-- 是否填充圆形区域 -->
        <attr name="Paint_Width" format="integer"/>		<!-- 画笔宽度,填充模式下无效,会被重置为0 -->
        <attr name="Paint_Color" format="integer"/>		<!-- 画笔颜色 -->
        <attr name="Show_Bottom" format="boolean"/>		<!-- 是否显示底色 -->
        <attr name="Inside_Interval" format="integer"/> <!-- 圆形区域向里缩进的距离 --> 
    </declare-styleable>  
    
</resources> 


这样在布局文件中,就可以配置这些属性值了,相关定义注释已写的很清楚,具体的大家下代码跑一跑比对一下就知道了

由于绘图使用的是纯画笔绘制,那么在视觉上看起来会比较单调,其实可以通过对画笔设置渲染效果来达到一个炫丽的效果,有兴趣的童鞋可以试一下,Paint.setShader这个接口,本例无此实现,就不详细介绍了

控件中有两个接口是作动画相关的

        public synchronized void  startCartoom(int time)
        public synchronized void  stopCartoom()


比如你想播放一个10秒的录音,同时用进度条来表示播放进度,那么就可以调用 startCartoom(i10)来开启动画
其他的似乎没啥好说的了,代码就不贴上来了,源码工程里的注释也写很清楚了,大家下下来看看就明白了

至于为什么该类继承于TextView而不是View其实是为了在配置文件里指定背景图能够自适应大小,继承与View则需要自己去实现,其实也不难,重写OnMeasure方法,在该方法里调用setMeasuredDimension重新设置视图大小为背景图大小就可以了,怎么获取背景图就不用哥说了吧。

 

 

转载自:http://www.eoeandroid.com/code/2011/1206/254.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值