进度环组件封装

下面是效果图,为了方便大家直接修改,我会在最下面贴出代码的下载地址,节省大家使用的时间。



进度环效果如上,我们分析上面这个组件,不同地方遇到时需要设置的。

大多数情况下:

初始化时候设置的:进度条宽、高、字体大小、已完成颜色、未完成颜色、环形的宽度

需要动态获取数据时候设置的:进度条的进度,这个值常常是获取后台的一个值或者是用线程控制变化的。


对于需要动态变的:我们写一个方法,直接传进来

public void setProgress(int progress) {
		this.mProgress = progress;
		this.invalidate();
}


对于初始化时候设置的,我们可以用自定义属性来做。首先在attr.xml中添加

 <declare-styleable name="circle_view">
        <attr name="circle_width" format="dimension" />
        <attr name="text_size" format="dimension"/>
        <attr name="undone_color" format="color"/>
        <attr name="done_color" format="color"/>
 </declare-styleable>

在使用时:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:xuan="http://schemas.android.com/apk/res/com.example.testproject"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.testproject.CircleProgressView
        android:id="@+id/circleProgressbar"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_centerInParent="true"
        xuan:circle_width="20dip"
        xuan:done_color="#0199CA"
        xuan:text_size="23sp"
        xuan:undone_color="#E9E9E9" />

</RelativeLayout>

注意两部分:

 xmlns:xuan="http://schemas.android.com/apk/res/com.example.testproject"

xuan:circle_width="20dip"
        xuan:done_color="#0199CA"
        xuan:text_size="23sp"
        xuan:undone_color="#E9E9E9"
上面的自定义属性空间xuan对应下面的自定义属性的开头,自定义控件末尾是我建的项目的包名。


然后在自定义组件中解析定义到的属性

public CircleProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.circle_view);
		mCircleLineStrokeWidth = a.getDimensionPixelSize(
				R.styleable.circle_view_circle_width, CIRCLE_WIDTH);
		mDoneColor = a.getColor(R.styleable.circle_view_done_color, DONE_COLOR);
		mUndoneColor = a.getColor(R.styleable.circle_view_undone_color,
				UNDONE_COLOR);
		mTextSize = a.getDimensionPixelSize(R.styleable.circle_view_text_size,
				TEXT_SIZE);
		mContext = context;
		mRectF = new RectF();
		mPaint = new Paint();
		a.recycle();
	}


获取一个属性的方式如下:
mCircleLineStrokeWidth = a.getDimensionPixelSize(
				R.styleable.circle_view_circle_width, CIRCLE_WIDTH);
这个需要传入一个默认值,当xml中没有使用这个属性时,就用默认值代替。

代码下载地址:http://download.csdn.net/download/u010047390/9665965


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值