您的位置:首页 > 移动开发 > Android开发

android 自定义View实现进度条增长

2015-04-08 11:50 531 查看
现在的app下载的时候是下载的时候使用一个圆然后根据下载的进度进行显示:

自定义个atterx.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RoundProgressBar">
<attr name="roundColor" format="color"/>
<attr name="roundProgressColor" format="color"/>
<attr name="roundWidth" format="dimension"></attr>
<attr name="style">
<enum name="STROKE" value="0"></enum>
<enum name="FILL" value="1"></enum>
</attr>
</declare-styleable>
</resources>


然后进行layout.xml中调用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android_progress="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START" />

<com.xushuangshuang.planshow.RoundProgressBar
android:id="@+id/round_progress_bar"
android:layout_width="60dp"
android:layout_height="60dp"
android_progress:roundColor="@color/skyblue"
android_progress:roundWidth="10dip"
android_progress:roundProgressColor="@color/red" />

</LinearLayout>
然后在自定义一个view的实现类:

public class RoundProgressBar extends View {

private int roundColor;
private float roundWidth;
private int roundProgressColour;
private Paint paint;
private int progress;

public RoundProgressBar(Context context) {
this(context, null);
}

public RoundProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, R.color.skyblue);
roundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
roundProgressColour = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, R.color.red);
typedArray.recycle();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

int centre = getWidth() / 2;
int radius = (int) (centre - roundWidth / 2);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(roundWidth);
paint.setColor(roundColor);
canvas.drawCircle(centre, centre, radius, paint);

paint.setStrokeWidth(roundWidth);
paint.setColor(roundProgressColour);
RectF rectF = new RectF(centre - radius, centre - radius, centre + radius, centre + radius);
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(rectF, 270, 360 * progress / 100, false, paint);

}

public synchronized int getProgress() {
return progress;
}

public synchronized void setProgress(int progress) {
if (progress < 0) {
throw new IllegalArgumentException("progress not less than 0");
}
if (progress > 100) {
progress = 100;
}
if (progress <= 100) {
this.progress = progress;
<span style="color:#ff0000;">postInvalidate();//这句很重要,他表示了每一次进来后都需要进行重绘</span>
}
}
}


在mainactivity中进行数据的调用:

public class MainActivity extends ActionBarActivity {

RoundProgressBar progressBar;
int progress = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.start);
progressBar = (RoundProgressBar) findViewById(R.id.round_progress_bar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
while (progress <= 100) {
Log.e("test", progress + "");

progress += 3;
progressBar.setProgress(progress);

try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});

}
http://download.csdn.net/detail/xushuangshuang1/8573645
这是源代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: