标签:
我们在下载的时候需要一个下载的进度,而且可能产品要一个漂亮的界面,而不是android自带的进度条了,在这感谢http://blog.csdn.net/lmj623565791/article/details/24500107博客的无私奉献
废话不多说,直接切入主题
先新建一个android项目:CustomProgressBar
还记得自定义view的步骤么
1、自定义View的属性
2、在View的构造方法中获得我们自定义的属性
[ 3、重写onMesure ]
4、重写onDraw
A: 在values/attrs下自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="firstColor" format="color" />
<attr name="secondColor" format="color" />
<attr name="circleWidth" format="dimension" />
<attr name="speed" format="integer" />
<declare-styleable name="CustomProgressBar">
<attr name="firstColor" />
<attr name="secondColor" />
<attr name="circleWidth" />
<attr name="speed" />
</declare-styleable>
</resources>
B:布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.customprogressbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.customprogressbar.CustomProgressBar
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
custom:circleWidth="15dp"
custom:firstColor="#D4F668"
custom:secondColor="#2F9DD2"
custom:speed="20" />
<com.example.customprogressbar.CustomProgressBar
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
custom:circleWidth="24dp"
android:layout_marginBottom="40dp"
custom:firstColor="#16A3FA"
custom:secondColor="#D20F02"
custom:speed="10" />
</RelativeLayout>
C:在构造函数中获取自定义属性:
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, defStyle, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++)
{
int attr = a.getIndex(i);
switch (attr)
{
case R.styleable.CustomProgressBar_firstColor:
mFirstColor = a.getColor(attr, Color.GREEN);
break;
case R.styleable.CustomProgressBar_secondColor:
mSecondColor = a.getColor(attr, Color.RED);
break;
case R.styleable.CustomProgressBar_circleWidth:
mCircleWidth = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
break;
case R.styleable.CustomProgressBar_speed:
mSpeed = a.getInt(attr, 20);// 默认20
break;
}
}
a.recycle();
mPaint = new Paint();
晚上继续写
标签:
原文地址:http://blog.csdn.net/coderinchina/article/details/42126375