一个简单的Loading控件
Posted 南极冰川雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的Loading控件相关的知识,希望对你有一定的参考价值。
实现效果如下:
使用方法:
在layout文件中添加以下代码:
<com.example.jack.ui.widget.RingLoading android:layout_width="20.0dip" android:layout_height="20.0dip" />
具体代码如下:
package com.example.jack.ui.widget; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.SweepGradient; import android.util.AttributeSet; import android.view.View; /** * Created by pengf on 2017/4/10. */ public class RingLoading extends View { private static final int PROGRESS = 5; private int centerX; private int centerY; private Context context; private int mProgressColor ; private Matrix matrix; private final Paint paint; private int roundWidth = 10; private int start = 0; public RingLoading(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; this.paint = new Paint(); this.paint.setAntiAlias(true); this.paint.setStyle(Paint.Style.STROKE); this.paint.setStrokeWidth(this.roundWidth); this.mProgressColor =Color.parseColor("#ffffcc00"); } @Override protected void onDraw(Canvas canvas) { int center = getWidth() / 2; int radius = center - this.roundWidth / 2; SweepGradient sweepGradient = new SweepGradient(this.centerX, this.centerY, Color.TRANSPARENT, this.mProgressColor); this.paint.setShader(sweepGradient); this.matrix = new Matrix(); this.matrix.postRotate(this.start, this.centerX, this.centerY); canvas.concat(this.matrix); canvas.drawCircle(center, center, radius, this.paint); this.start = (PROGRESS + this.start); invalidate(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); this.centerX = (w / 2); this.centerY = (h / 2); } }
以上是关于一个简单的Loading控件的主要内容,如果未能解决你的问题,请参考以下文章
WPF和Expression Blend开发实例:Loading动画