最近看了安智市场手机清理功能,就想着仿着做做:
下面是原图:
好了,直接上代码:
attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="firstColor" format="color"></attr>
<attr name="sencondColor" format="color"></attr>
<attr name="cicleWidth" fotmat="dimension"></attr>
<attr name="speed" format="integer"></attr>
<attr name="textSize" format="dimension"></attr>
<!-- 申明 -->
<declare-styleable name="CustomProgressBar">
<attr name="firstColor"></attr>
<attr name="sencondColor"></attr>
<attr name="cicleWidth"></attr>
<attr name="speed"></attr>
<attr name="textSize"></attr>
</declare-styleable>
</resources>
CusomProgressBar.java:
package com.example.mycustomwidget_03_4.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import com.example.mycustomwidget_03_4.R;
public class CusomProgressBar extends View {
private int firstColor, scondColor;
private int cicleWidth, speed,textSize;
private Paint mPaint;
private int mProgress;
public static boolean isOK;
public CusomProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public CusomProgressBar(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public CusomProgressBar(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取自定义属性
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CustomProgressBar, defStyleAttr, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.CustomProgressBar_firstColor:
firstColor = a.getColor(attr, Color.BLUE);
break;
case R.styleable.CustomProgressBar_sencondColor:
scondColor = a.getColor(attr, Color.BLUE);
break;
case R.styleable.CustomProgressBar_speed:
speed = a.getInt(attr, 5);
break;
case R.styleable.CustomProgressBar_cicleWidth:
cicleWidth = a.getDimensionPixelSize(attr, 20);
break;
case R.styleable.CustomProgressBar_textSize:
textSize = a.getDimensionPixelSize(attr, 65);
break;
}
}
// 释放
a.recycle();
}
public void setProgress(int progress) {
this.mProgress = progress;
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
postInvalidate();
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
init();
int centre = drawArc(canvas);
drawText(canvas, centre);
drawImg(canvas, centre);
}
/**
* 画图片
* @param canvas
* @param centre
*/
private void drawImg(Canvas canvas, int centre) {
//设置底部图片
if(isOK){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_clear_broom);
int bWidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
canvas.drawBitmap(bitmap, centre-bWidth/2, centre+bHeight, mPaint);
}else{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_clear_ok);
int bWidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
canvas.drawBitmap(bitmap, centre-bWidth/2, centre+bHeight, mPaint);
}
}
/**
* 画文字
* @param canvas
* @param centre
*/
private void drawText(Canvas canvas, int centre) {
// 设置中间的文字
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(textSize);
//设置字体类型
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
mPaint.setStrokeWidth(0);
String text = mProgress+"%";
/**
* 获得字体宽度
*/
float width = mPaint.measureText(text);
/**
* 获得字体的高度
*/
Rect bounds = new Rect();
mPaint.getTextBounds(text, 0, 1, bounds);
//字体高度
int height = bounds.height();
canvas.drawText(text, centre - width / 2, centre+height/2, mPaint);
}
/**
* 画弧形
* @param canvas
* @return
*/
private int drawArc(Canvas canvas) {
// 圆心x坐标
int centre = getWidth() / 2;
// 半径
int radius = centre - cicleWidth / 2;
// 外切矩形
RectF oval = new RectF(cicleWidth / 2, cicleWidth / 2, getWidth()
- cicleWidth / 2, getWidth() - cicleWidth / 2);
//画外层的弧形
mPaint.setColor(firstColor);
canvas.drawArc(oval, 135, 270, false, mPaint);
//画内层的弧形
mPaint.setColor(scondColor);
canvas.drawArc(oval, 135, (float) (mProgress*2.7), false, mPaint);
return centre;
}
/**
* Paint初始化
*/
private void init() {
mPaint = new Paint();
mPaint.setStrokeWidth(cicleWidth);
mPaint.setAntiAlias(true);
mPaint.setStyle(Style.STROKE);
//定义形状为圆头
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
}
下面是MainActivity.java:
package com.example.mycustomwidget_03_4;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import com.example.mycustomwidget_03_4.view.CusomProgressBar;
public class MainActivity extends Activity {
private int mProgress;
private CusomProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (CusomProgressBar) findViewById(R.id.pb);
}
public void start(View view){
new Thread() {
public void run() {
mProgress = 0;
pb.isOK = true;
while (pb.isOK) {
mProgress++;
if (mProgress == 100) {
pb.isOK = false;
}
pb.setProgress(mProgress);
}
};
}.start();
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fhl="http://schemas.android.com/apk/res/com.example.mycustomwidget_03_4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#32c264"
android:orientation="vertical"
>
<com.example.mycustomwidget_03_4.view.CusomProgressBar
android:id="@+id/pb"
fhl:textSize="50sp"
fhl:firstColor="#33ff00"
fhl:sencondColor="#ffffff"
fhl:cicleWidth="20dip"
fhl:speed="50"
android:layout_width="200dip"
android:layout_height="200dip"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="69dp" >
<Button
android:onClick="start"
android:text="点我 开始"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center" />
</RelativeLayout>
</LinearLayout>
到此就结束了。
编译运行结果如下:
源码下载:http://download.csdn.net/detail/yushanfenghailin/8696509