首页 > 代码库 > 自定义view——环形进度条,带progress值
自定义view——环形进度条,带progress值
第一步:新建文件Circle.java
package com.lancy.demo.democircle.widget;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import com.lancy.demo.democircle.R;
public class Circle extends View {
private Paint mPaint;
private float strokeWidth = 10;
private float progress = 0;
private int defaultColor;
private int activeColor;
public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.circle);
strokeWidth = a.getDimension(R.styleable.circle_strokeWidth, strokeWidth);
progress = a.getFraction(R.styleable.circle_progress, 360, 100, 10);
defaultColor = a.getColor(R.styleable.circle_defaultColor, Color.GRAY);
activeColor = a.getColor(R.styleable.circle_activeColor, Color.BLUE);
a.recycle();
}
public Circle(Context context) {
this(context, null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int minimumWidth = getSuggestedMinimumWidth();
final int minimumHeight = getSuggestedMinimumHeight();
int viewWidth = resolveMeasured(widthMeasureSpec, minimumWidth);
int viewHeight = resolveMeasured(heightMeasureSpec, minimumHeight);
setMeasuredDimension(viewWidth, viewHeight);
}
private int resolveMeasured(int measureSpec, int desired) {
int result = 0;
int specSize = MeasureSpec.getSize(measureSpec);
switch (MeasureSpec.getMode(measureSpec)) {
case MeasureSpec.UNSPECIFIED:
result = desired;
break;
case MeasureSpec.AT_MOST:
result = Math.max(specSize, desired);
break;
case MeasureSpec.EXACTLY:
default:
result = specSize;
}
return result;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
float h = width > height ? height : width;
Paint p = new Paint();
p.setColor(defaultColor);
p.setStyle(Paint.Style.STROKE);
p.setAntiAlias(true);
p.setStrokeWidth(strokeWidth);
RectF oval1 = new RectF(strokeWidth / 2, strokeWidth / 2, h-strokeWidth / 2, h-strokeWidth / 2);
canvas.drawArc(oval1, 0, 360, false, p);
p.setColor(activeColor);
canvas.drawArc(oval1, 270, progress, false, p);//小弧形
}
public void setProgress(float progress) {
this.progress = progress / 100 * 360;
invalidate();
}
public float getProgress() {
return progress * 100 / 360;
}
}
第二步 新建attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="circle">
<attr name="strokeWidth" format="dimension"></attr>
<attr name="progress" format="fraction"></attr>
<attr name="defaultColor" format="color"></attr>
<attr name="activeColor" format="color"></attr>
</declare-styleable>
</resources>
第三步 建布局文件 circle.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:circle="http://schemas.android.com/apk/res/com.lancy.demo.democircle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp"
android:gravity="center"
android:orientation="vertical" >
<com.lancy.demo.democircle.widget.Circle
android:id="@+id/circle"
android:layout_width="match_parent"
android:layout_height="match_parent"
circle:defaultColor="#95a1aa"
circle:activeColor="#1290dd"
circle:strokeWidth="10dp"
circle:progress="25%"
/>
<!-- -->
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:circle="http://schemas.android.com/apk/res/com.lancy.demo.democircle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp"
android:gravity="center"
android:orientation="vertical" >
<com.lancy.demo.democircle.widget.Circle
android:id="@+id/circle"
android:layout_width="match_parent"
android:layout_height="match_parent"
circle:defaultColor="#95a1aa"
circle:activeColor="#1290dd"
circle:strokeWidth="10dp"
circle:progress="25%"
/>
<!-- -->
</LinearLayout>
第三步 建MainActivity.java
package com.lancy.demo.democircle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import com.lancy.demo.democircle.widget.Circle;
public class MainActivity extends Activity {
Circle circle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.circle);
circle = (Circle) findViewById(R.id.circle);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
registerReceiver(new TimeReceiver(), filter);
}
class TimeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("TimeReceiver----------------", intent.getAction());
circle.setProgress(circle.getProgress()-1);
}
}
}
自定义view——环形进度条,带progress值
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。