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

Android画布(Canvas)之--- 滚动多彩圆环,利用Path切除一个扇形,形成一段圆弧效果

2017-06-21 09:57 615 查看
public class TestCanvasActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new LoadingView(this));
}

private static class LoadingView extends View {
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float mRotate;
private Matrix mMatrix = new Matrix();
private Shader mShader;
float centerX = 360;
float centerY = 360;
float radius = 200;
float gapStartDeg = 70;
float gapEndDeg = 110;
private Path mPath;
private RectF rect;
private Paint paintRect;
public LoadingView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);

mShader = new SweepGradient(centerX, centerY, new int[] { Color.GREEN,
Color.RED,
Color.BLUE,
Color.GREEN }, null);
mPaint.setShader(mShader);
mPaint.setStyle(Paint.Style.STROKE);
PathEffect effect = new DashPathEffect(new float[] { 5, 8, 5, 8}, 1);
mPaint.setPathEffect(effect);
mPaint.setStrokeWidth(50);
mPath = new Path();
mPath.moveTo(centerX, centerY);
rect = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
mPath.arcTo(rect, gapStartDeg, gapEndDeg - gapStartDeg, false);

paintRect = new Paint();
paintRect.setColor(Color.RED);
paintRect.setStyle(Paint.Style.STROKE);
}

@Override protected void onDraw(Canvas canvas) {
Paint paint = mPaint;

canvas.drawColor(Color.WHITE);

mMatrix.postRotate(mRotate, centerX, centerY);
mShader.setLocalMatrix(mMatrix);

invalidate();
//no worry about resulting in loop, this is way to animate the view.
//you can use: postInvalidate, postInvalidateDelayed.

canvas.drawRect(rect, paintRect);
canvas.clipPath(mPath, Region.Op.DIFFERENCE);
canvas.drawCircle(centerX, centerY, 80, paint);
}
}
}


预览:



因为我无法回复: http://bbs.csdn.net/topics/390746097,所以发帖,希望对你有用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  path canvas sweeploading
相关文章推荐