/**
* @FileName CircleProgressBar.java
* @Package com.read.view
* @Description TODO
* @Author Alpha
* @Date 2015-7-30 下午4:52:24
* @Version V1.0 */
package com.read.view;
import com.read.cnblogs.R; import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Cap;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator; public class CircleProgressBar extends View { private static final Interpolator ANGLE_INTERPOLATOR = new LinearInterpolator();
private static final Interpolator SWEEP_INTERPOLATOR = new AccelerateDecelerateInterpolator();
private static final int ANGLE_ANIMATOR_DURATION = 2000;
private static final int SWEEP_ANIMATOR_DURATION = 900;
private static final int MIN_SWEEP_ANGLE = 30;
private static final int DEFAULT_BORDER_WIDTH = 3;
private final RectF fBounds = new RectF(); private ObjectAnimator mObjectAnimatorSweep;
private ObjectAnimator mObjectAnimatorAngle;
private boolean mModeAppearing = true;
private Paint mPaint;
private float mCurrentGlobalAngleOffset;
private float mCurrentGlobalAngle;
private float mCurrentSweepAngle;
private float mBorderWidth;
private boolean mRunning;
private int[] mColors; public CircleProgressBar(Context context) {
this(context, null);
} public CircleProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); float density = context.getResources().getDisplayMetrics().density;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircularProgress, defStyleAttr, 0);
mBorderWidth = a.getDimension(R.styleable.CircularProgress_borderWidth,
DEFAULT_BORDER_WIDTH * density);
a.recycle();
mColors = new int[2];
mColors[0] = context.getResources().getColor(R.color.pink);
//mColors[1]=context.getResources().getColor(R.color.pink); // mCurrentColorIndex = 0;
// mNextColorIndex = 1; mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Cap.ROUND);
mPaint.setStrokeWidth(mBorderWidth);
mPaint.setColor(mColors[0]); setupAnimations();
} private void start() {
if (mRunning) {
return;
}
mRunning = true;
mObjectAnimatorAngle.start();
mObjectAnimatorSweep.start();
invalidate();
} private void stop() {
if (!mRunning) {
return;
}
mRunning = false;
mObjectAnimatorAngle.cancel();
mObjectAnimatorSweep.cancel();
invalidate();
} @Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
if (visibility == VISIBLE) {
start();
} else {
stop();
}
} @Override
protected void onAttachedToWindow() {
start();
super.onAttachedToWindow();
} @Override
protected void onDetachedFromWindow() {
stop();
super.onDetachedFromWindow();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
fBounds.left = mBorderWidth / 2f + .5f;
fBounds.right = w - mBorderWidth / 2f - .5f;
fBounds.top = mBorderWidth / 2f + .5f;
fBounds.bottom = h - mBorderWidth / 2f - .5f;
} @Override
public void draw(Canvas canvas) {
super.draw(canvas);
float startAngle = mCurrentGlobalAngle - mCurrentGlobalAngleOffset;
float sweepAngle = mCurrentSweepAngle;
if (mModeAppearing) {
mPaint.setColor(mColors[0]);
sweepAngle += MIN_SWEEP_ANGLE;
} else {
startAngle = startAngle + sweepAngle;
sweepAngle = 360 - sweepAngle - MIN_SWEEP_ANGLE;
}
canvas.drawArc(fBounds, startAngle, sweepAngle, false, mPaint);//画弧线
} // private static int gradient(int color1, int color2, float p) {
// int r1 = (color1 & 0xff0000) >> 16;
// int g1 = (color1 & 0xff00) >> 8;
// int b1 = color1 & 0xff;
// int r2 = (color2 & 0xff0000) >> 16;
// int g2 = (color2 & 0xff00) >> 8;
// int b2 = color2 & 0xff;
// int newr = (int) (r2 * p + r1 * (1 - p));
// int newg = (int) (g2 * p + g1 * (1 - p));
// int newb = (int) (b2 * p + b1 * (1 - p));
// return Color.argb(200, newr, newg, newb);
// } private void toggleAppearingMode() {
mModeAppearing = !mModeAppearing;
if (mModeAppearing) {
// mCurrentColorIndex = ++mCurrentColorIndex % 4;
// mNextColorIndex = ++mNextColorIndex % 4;
mCurrentGlobalAngleOffset = (mCurrentGlobalAngleOffset + MIN_SWEEP_ANGLE * 2) % 360;
}
}
// ////////////////////////////////////////////////////////////////////////////
// ////////////// Animation private Property<CircleProgressBar, Float> mAngleProperty = new Property<CircleProgressBar, Float>(Float.class, "angle") {
@Override
public Float get(CircleProgressBar object) {
return object.getCurrentGlobalAngle();
} @Override
public void set(CircleProgressBar object, Float value) {
object.setCurrentGlobalAngle(value);
}
}; private Property<CircleProgressBar, Float> mSweepProperty = new Property<CircleProgressBar, Float>(Float.class, "arc") {
@Override
public Float get(CircleProgressBar object) {
return object.getCurrentSweepAngle();
} @Override
public void set(CircleProgressBar object, Float value) {
object.setCurrentSweepAngle(value);
}
}; private void setupAnimations() {
mObjectAnimatorAngle = ObjectAnimator.ofFloat(this, mAngleProperty, 360f);
mObjectAnimatorAngle.setInterpolator(ANGLE_INTERPOLATOR);
mObjectAnimatorAngle.setDuration(ANGLE_ANIMATOR_DURATION);
mObjectAnimatorAngle.setRepeatMode(ValueAnimator.RESTART);
mObjectAnimatorAngle.setRepeatCount(ValueAnimator.INFINITE); mObjectAnimatorSweep = ObjectAnimator.ofFloat(this, mSweepProperty, 360f - MIN_SWEEP_ANGLE * 2);
mObjectAnimatorSweep.setInterpolator(SWEEP_INTERPOLATOR);
mObjectAnimatorSweep.setDuration(SWEEP_ANIMATOR_DURATION);
mObjectAnimatorSweep.setRepeatMode(ValueAnimator.INFINITE);
mObjectAnimatorSweep.setRepeatCount(ValueAnimator.INFINITE);
mObjectAnimatorSweep.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) { } @Override
public void onAnimationEnd(Animator animation) { } @Override
public void onAnimationCancel(Animator animation) { } @Override
public void onAnimationRepeat(Animator animation) {
toggleAppearingMode();
}
}); } public void setCurrentGlobalAngle(float currentGlobalAngle) {
mCurrentGlobalAngle = currentGlobalAngle;
invalidate();
} public float getCurrentGlobalAngle() {
return mCurrentGlobalAngle;
} public void setCurrentSweepAngle(float currentSweepAngle) {
mCurrentSweepAngle = currentSweepAngle;
invalidate();
} public float getCurrentSweepAngle() {
return mCurrentSweepAngle;
}
}

ProgressBar 源码的更多相关文章

  1. 45个android实例源码

    分享45个android实例源码,很好很强大http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码http://www.apkbus.com ...

  2. 分享45个android实例源码,很好很强大

    分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html 分享45个android实例源码,很好很强大http://www ...

  3. android 在线升级借助开源中国App源码

    android 在线升级借助开源中国App源码 http://www.cnblogs.com/luomingui/p/3949429.html android 在线升级借助开源中国App源码分析如下: ...

  4. AsyncTask源码分析

    在Android中,主线程是UI线程,当需要根据其他数据进行更新UI时,如果获取数据的操作比较耗时的话,会触发ANR,所以我们应该讲耗时的操作进行异步操作,尤其是请求网络数据的操作应该放在后台线程进行 ...

  5. ExtJS学习之路第四步:看源码,实战MessageBox

    可以通过看MessageBox.js的源码来深入认识,记住它的主要用法.Ext.MessageBox是实用类,用于生成不同风格的消息框,它是Singleton(单例),别名Ext.Msg.注意Mess ...

  6. android源码解析(十七)-->Activity布局加载流程

    版权声明:本文为博主原创文章,未经博主允许不得转载. 好吧,终于要开始讲讲Activity的布局加载流程了,大家都知道在Android体系中Activity扮演了一个界面展示的角色,这也是它与andr ...

  7. 转--2014年最新810多套android源码2.46GB免费一次性打包下载

    转载自:http://www.eoeandroid.com/thread-497046-1-1.html 感谢该博客主人无私奉献~~ 下面的源码是从今年3月份开始不断整理源码区和其他网站上的安卓例子源 ...

  8. 非常出色的一款WinForm窗体重绘GUI类库源码

    基本控件的演示 ScrollBar滚动条 各种圆形进度条 ProgressBar进度条 Mdi演示,仿谷歌浏览器 多种皮肤可供选择 一套专业级别的GUI控件,目前包含了窗体.进度条.滚动条以及MDI多 ...

  9. C#源码大汇总

    C#高仿QQ2013可在广域网部署聊天系统GG叽叽 动态显示硬盘分区容量饼图 自定义ProgressBar控件高仿Win8进度条 多皮肤精美在线QQ悬浮客服插件 jQuery仿天猫首页多格焦点图片轮播 ...

随机推荐

  1. CSS3学习笔记

    1.Tranform 转换(2D,3D) 2D: div { margin:30px; width:200px; height:100px; background-color:yellow; /* R ...

  2. JavaScript 基础第二天

    一.前言 感觉昨天的内容确实是有点细碎.复杂.感觉是没有书上写的那么的细致而且有导入性,但是我还是喜欢这样只说干货.今天的内容将继续接着昨天最后的内容JS中的语言结构继续讲解并且重点讲解一下其中的内容 ...

  3. [poi2007] biu

    题意:给定一个图,点n<=105,边m<=106,现在求它的补图有多少个联通分量.. 思路:很容易想到并查集,但是补图边太多了.. 于是只能优化掉一些多余的边.. 具体做法是用队列优化.. ...

  4. Inno Setup 下载安装

    Inno Setup 是一个免费的 Windows 安装程序制作软件.第一次发表是在 1997 年,Inno Setup 今天在功能设置和稳定性上的竞争力可能已经超过一些商业的安装程序制作软件. 目前 ...

  5. 纯练手设置ip地址脚本

    #!/bin/bash IFO() { read -p "Configure $line Network card ( 'yes'or'no' )?" CDN </dev/t ...

  6. html+css实现简易下拉菜单

    <!DOCTYPE html> <html> <head> <style> div { width:100px; height:40px; overfl ...

  7. 在CentOS上搭建最^1024基本的Nginx反向服务器

    昨天有过去的同事突然问我,他在CentOS7上试验搭建反向服务器死活不成功.现将最简单的搭建步骤分享下: 0. 环境介绍 本次搭建的集群包括以下服务器 192.168.1.107:nginx反向服务器 ...

  8. hander消息机制原理

    基本原理 线程中调用Handler.sendMsg()方法(参数是Message对象),将需要Main线程处理的事件 添加到Main线程的MessageQueue中,Main线程通过MainLoope ...

  9. Xamarin.IOS之多视图

    欢迎大家加入以下开源社区 Xamarin-Cn:https://github.com/Xamarin-Cn Mvvmcross-Cn:https://github.com/Mvvmcross-Cn  ...

  10. 扒皮下GitHub 404的图片层次轴动特效

    今天要克隆的前端特效非常有意思,可以参见GitHub404页面 https://github.com/vajoy/master/index.html 记得之前华为在站酷发布EMUI设计大赛的主页也用了 ...