Android自定义view之圆形进度条

简介:
本节介绍自定义view-圆形进度条
思路:
根据前面介绍的自定义view内容可拓展得之;
1:新建类继承自View
2:添加自定义view属性
3:重写onDraw(Canvas canvas)
4:实现功能
下面上代码

1.自定义view代码:

public class CustomView extends View {
	//背景圆环颜色
	private int circleColor;
	//进度条颜色&字体颜色(为了美观,所以设计字体颜色和进度条颜色值一致)
	private int secondCircleColor;
	//进度条&背景圆环宽度
	private float stroke_width;
	//进度值
	private float progress;
	//总进度值,默认为100
	private float totalProgress;
	//字体大小
	private float textSize;
	//填充模式
	private int style_type;
	public CustomView(Context context) {
		super(context);
	}

	public CustomView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.CustomView);
		circleColor=array.getColor(R.styleable.CustomView_circleColor, Color.BLACK);
		secondCircleColor=array.getColor(R.styleable.CustomView_secondCircleColor, Color.RED);
		stroke_width=array.getDimension(R.styleable.CustomView_stroke_width, 2);
		progress=array.getFloat(R.styleable.CustomView_progress, 0);
		totalProgress=array.getFloat(R.styleable.CustomView_totalProgress, 100);
		textSize=array.getDimension(R.styleable.CustomView_textSize, 16);
		style_type=array.getInt(R.styleable.CustomView_style_Type, 0);
	}

	public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public void setCircleColor(int color){
		circleColor=color;
	}
	public int getCircleColor(){
		return circleColor;
	}

	public void setSecondCircleColor(int color){
		secondCircleColor=color;
	}
	public int getSecondColor(){
		return secondCircleColor;
	}
	public void setStrokeWidth(float width){
		stroke_width=width;
	}
	public float getStrokeWidth(){
		return stroke_width;
	}
	public void setProgress(float progress){
		this.progress=progress;
		postInvalidate();//刷新界面
	}
	public float getProgress(){
		return this.progress;
	}
	public void setTotalProgress(float totalProgress){
		this.totalProgress=totalProgress;
	}
	public float getTotalProgress(){
		return this.totalProgress;
	}
	public void setTextSize(float textSize){
		this.textSize=textSize;
	}
	public float getTextSize(){
		return this.textSize;
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//第一进度圆
		final Paint paint_background=new Paint();
		paint_background.setAntiAlias(true);
		paint_background.setStrokeWidth(stroke_width);
		paint_background.setStyle(Style.STROKE);
		paint_background.setColor(circleColor);
		//第二进度圆
		final Paint paint_progress=new Paint();
		paint_progress.setAntiAlias(true);
		paint_progress.setStrokeWidth(stroke_width);
		if(style_type==0){
			paint_progress.setStyle(Style.STROKE);
		}else if(style_type==1){
			paint_progress.setStyle(Style.FILL_AND_STROKE);
		}
		paint_progress.setColor(secondCircleColor);
		//画text
		final Paint paint_text=new Paint();
		paint_text.setAntiAlias(true);
		if(style_type==0){
			paint_text.setColor(secondCircleColor);
		}else if(style_type==1){
			paint_text.setColor(circleColor);
		}
		paint_text.setTextSize(textSize);
		paint_text.setTextAlign(Align.CENTER);
		if(getWidth()!=getHeight()){
			throw new IllegalArgumentException("高度和宽度必须相等");//控制宽度和高度
		}else{
			RectF circle_background=new RectF();
			circle_background.left=getLeft()+stroke_width;
			circle_background.right=getRight()-stroke_width;
			circle_background.top=getTop()+stroke_width;
			circle_background.bottom=getBottom()-stroke_width;
			canvas.drawArc(circle_background, -90, 360, false, paint_background);
			RectF circle_progress=new RectF();
			circle_progress.left=getLeft()+stroke_width;
			circle_progress.right=getRight()-stroke_width;
			circle_progress.top=getTop()+stroke_width;
			circle_progress.bottom=getBottom()-stroke_width;
			if(progress>totalProgress){
				throw new IllegalArgumentException("当前进度值不能大于总进度值");
			}else{
				if(style_type==0){
					canvas.drawArc(circle_progress, -90, progress/totalProgress*360, false, paint_progress);
				}else if(style_type==1){
					canvas.drawArc(circle_progress, -90, progress/totalProgress*360, true, paint_progress);
				}
			}
			canvas.drawText((int)progress+"/"+(int)totalProgress, getLeft()+getWidth()/2, getTop()+getHeight()/2+textSize/4, paint_text);
		}
	}

}

2:attr属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
 	<!--declare-styleable:声明样式类型;attr name=""声明属性名;format="属性的类型"  -->
    <declare-styleable name="CustomEditText">
        <attr name="lineColor" format="color" />
        <attr name="lineHeight" format="dimension"/>
    </declare-styleable>
    <declare-styleable name="CustomView">
        <attr name="stroke_width" format="dimension"/>
        <attr name="circleColor" format="color"/>
        <attr name="secondCircleColor" format="color"/>
        <attr name="progress" format="float"/>
        <attr name="totalProgress" format="float"/>
        <attr name="textSize" format="dimension"/>
        <attr name="style_Type">
            <enum name="stroke" value="0"/>
            <enum name="stroke_and_fill" value="1"/>
        </attr>
    </declare-styleable>

</resources>

3:xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <com.anqiansong.views.CustomView
        xmlns:circle="http://schemas.android.com/apk/res/com.anqiansong.androidcustomview"
        android:id="@+id/customview"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        circle:circleColor="#000000"
        circle:secondCircleColor="#ff0000"
        circle:stroke_width="2dp"
        circle:totalProgress="100" 
        circle:progress="10"
        circle:style_Type="stroke"
        />

</RelativeLayout>

当xml文件中circle:style_Type="stroke"时



当xml文件中circle:style_Type="stroke_and_fill"时

4:activity中调用

public class MainActivity extends ActionBarActivity {

	CustomView customView;
	private float progress=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		customView=(CustomView) findViewById(R.id.customview);
		handler.sendEmptyMessageDelayed(0, 1000);
	}
	Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what==0){
				if(progress>100){
					return;
				}else{
					customView.setProgress(progress);
					progress+=2;
					handler.sendEmptyMessageDelayed(0, 100);
				}
			}
		};
	};


}


当xml文件中circle:style_Type="stroke_and_fill"时

相关文章
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
773 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
XML Java Android开发
Android自定义view之网易云推荐歌单界面
本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。
548 65
Android自定义view之网易云推荐歌单界面
|
XML 前端开发 Android开发
一篇文章带你走近Android自定义view
这是一篇关于Android自定义View的全面教程,涵盖从基础到进阶的知识点。文章首先讲解了自定义View的必要性及简单实现(如通过三个构造函数解决焦点问题),接着深入探讨Canvas绘图、自定义属性设置、动画实现等内容。还提供了具体案例,如跑马灯、折线图、太极图等。此外,文章详细解析了View绘制流程(measure、layout、draw)和事件分发机制。最后延伸至SurfaceView、GLSurfaceView、SVG动画等高级主题,并附带GitHub案例供实践。适合希望深入理解Android自定义View的开发者学习参考。
992 84
|
前端开发 Android开发 UED
讲讲Android为自定义view提供的SurfaceView
本文详细介绍了Android中自定义View时使用SurfaceView的必要性和实现方式。首先分析了在复杂绘制逻辑和高频界面更新场景下,传统View可能引发卡顿的问题,进而引出SurfaceView作为解决方案。文章通过Android官方Demo展示了SurfaceView的基本用法,包括实现`SurfaceHolder.Callback2`接口、与Activity生命周期绑定、子线程中使用`lockCanvas()`和`unlockCanvasAndPost()`方法完成绘图操作。
367 3
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
389 1
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
507 0
|
XML API Android开发
Android 自定义View 之 饼状进度条
Android 自定义View 之 饼状进度条
264 1
|
Android开发
Android自定义一款带进度条的精美按键
Android自定义一款带进度条的精美按键
|
Android开发
Android 音乐APP(三)播放音乐、自定义进度条、自动下一曲
Android 音乐APP(三)播放音乐、自定义进度条、自动下一曲
890 0
Android 音乐APP(三)播放音乐、自定义进度条、自动下一曲
|
XML Android开发 数据格式
Android自定义ProgressBar样式:渐变圆角水平进度条
Android自定义ProgressBar样式:渐变圆角水平进度条 关键是android:progressDrawable的设置,设置一个android:progressDrawable资源,但是android:progressDrawable需要是一个layer-list。
5716 0

热门文章

最新文章