在项目中有时候难免会用到自定义的加载圈,下面是一个简单的自定义加载圈
下面我们来看一下怎么实现的
1.在res/values下面创建一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="oneColor" format="color"/>
<attr name="twoColor" format="color"/>
<attr name="circleWidth" format="dimension"/>
<attr name="speed" format="integer"/>
<declare-styleable name="CustomProgress">
<attr name="oneColor" />
<attr name="twoColor" />
<attr name="circleWidth" />
<attr name="speed" />
</declare-styleable>
</resources>
2.创建一个类继承View,重写相应的方法
public class MyCustomProgress extends View{
private int myOneColor;
private int myTwoColor;
private int myCircleWidth;
private int mySpeed;
private int myProgress = 0;
private boolean isStart = false; //是否进行下一个
private Paint myPaint;
public MyCustomProgress(Context context) {
this(context, null);
}
public MyCustomProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyCustomProgress(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomProgress, defStyleAttr, 0);
int indexCount = ta.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int index = ta.getIndex(i);
switch (index) {
case R.styleable.CustomProgress_oneColor:
myOneColor = ta.getColor(index, Color.BLUE);
break;
case R.styleable.CustomProgress_twoColor:
myTwoColor = ta.getColor(index, Color.RED);
break;
case R.styleable.CustomProgress_circleWidth:
myCircleWidth = ta.getDimensionPixelSize(index, (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()));
break;
case R.styleable.CustomProgress_speed:
mySpeed = ta.getInt(index, 20);
break;
}
}
ta.recycle();
myPaint = new Paint();
new Thread()
{
public void run()
{
while (true)
{
myProgress++;
if (myProgress == 360)
{
myProgress = 0;
if (!isStart)
isStart = true;
else
isStart = false;
}
postInvalidate();
try
{
Thread.sleep(mySpeed);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
}.start();
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas)
{
int centre = getWidth() / 2;
int radius = centre - myCircleWidth / 2;
myPaint.setStrokeWidth(myCircleWidth);
myPaint.setAntiAlias(true);
myPaint.setStyle(Paint.Style.STROKE); //设置空心
RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); // 用于定义的圆弧的形状和大小的界限
if (!isStart)
{
myPaint.setColor(myOneColor);
canvas.drawCircle(centre, centre, radius, myPaint);
myPaint.setColor(myTwoColor);
canvas.drawArc(oval, -90, myProgress, false, myPaint);
}else{
myPaint.setColor(myTwoColor);
canvas.drawCircle(centre, centre, radius, myPaint);
myPaint.setColor(myOneColor);
canvas.drawArc(oval, -90, myProgress, false, myPaint);
}
}
3.在布局中使用
<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.mycustomprogress"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.mycustomprogress.MyCustomProgress
android:layout_width="50dp"
android:layout_height="50dp"
custom:circleWidth="6dp"
custom:oneColor="#789089"
custom:twoColor="#563685"
android:layout_centerInParent="true"
custom:speed="5" />
</RelativeLayout>
这样就能够完美的运行了,小弟菜鸟起步,有问题的地方希望大家指正批评