一个WP8样式的Android ProgressBar

一个WP8样式的进度对话框

public class Win8ProgressDialog extends Dialog {
    private WPLoading loading;

    public Win8ProgressDialog(Context context) {
        super(context);
    }

    public Win8ProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public void setLoading(WPLoading loading) {
        this.loading = loading;
    }

    @Override
    public void cancel() {
        loading.cancel();
        super.cancel();
    }

    public Win8ProgressDialog(Context context, boolean cancelable,
            OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context mContext;
        private boolean canceledOnTouchOutside = true;
        private boolean cancelable = true;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setCanceledOnTouchOutside(boolean flag) {
            canceledOnTouchOutside = flag;
            return this;
        }

        public Builder setCancelable(boolean flag) {
            cancelable = flag;
            return this;
        }

        public Win8ProgressDialog create() {
            final Win8ProgressDialog dialog = new Win8ProgressDialog(mContext,
                    R.style.WPDialog);
            WPLoading loading = new WPLoading(mContext);
            dialog.setLoading(loading);
            dialog.setCancelable(cancelable);
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
            dialog.setCanceledOnTouchOutside(false);
            dialog.addContentView(loading, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            loading.startAnimate();
            return dialog;
        }
    }

}


WPLoading是ProgressBar显示的view,其实是一个包含了五个小蓝点的RelativeLayout,

public class WPLoading extends RelativeLayout {

	private int size = 10;
	private int delay = 300;
	private int duration = 3200;
	private String color = "#0000ff";

	private AnimatorSet animatorSet = new AnimatorSet();

	public WPLoading(Context context) {
		super(context);
		LayoutParams params0 = new LayoutParams(
				DisplayUtil.getScreenWidth(context), size);
		View view = new View(context);
		view.setLayoutParams(params0);
		addView(view);
	}

	public void startAnimate() {
		LayoutParams params = new LayoutParams(size, size);
		animatorSet = new AnimatorSet();
		ArrayList<Animator> animators = new ArrayList<Animator>();
		for (int i = 0; i < 5; i++) {
			View view = new View(getContext());
			view.setBackgroundColor(Color.parseColor(color));
			addView(view);
			view.setLayoutParams(params);
			view.setX(-size);

			ObjectAnimator headAnimator = ObjectAnimator.ofFloat(view, "x",
					view.getX(), DisplayUtil.getScreenWidth(getContext()));
			headAnimator.setDuration(duration);
			headAnimator
					.setInterpolator(new DecelerateAccelerateStopInterpolator());
			headAnimator.setStartDelay(delay * i);
			headAnimator.setRepeatCount(-1);
			animators.add(headAnimator);
		}
		animatorSet.playTogether(animators);
		animatorSet.start();
	}

	public WPLoading(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public WPLoading(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void cancel() {
		animatorSet.end();
	}

	// 先减速<span style="font-family: Arial, Helvetica, sans-serif;">,</span>再加速的插值器,<span style="font-family: Arial, Helvetica, sans-serif;">再停止一会儿。循环起来,就是WP8样式的加载进度</span>
	class DecelerateAccelerateStopInterpolator implements
			android.view.animation.Interpolator {
		private float mFactor = 1.0f;
		private float tailFactor = 0.6f;

		public DecelerateAccelerateStopInterpolator() {
		}

		public DecelerateAccelerateStopInterpolator(float factor) {
			mFactor = factor;
		}

		public float getInterpolation(float x) {
			float result;
			if (x > tailFactor) {
				result = 1;
			} else if (x > tailFactor / 2) {
				result = (float) Math.pow(
						(x - tailFactor / 2) * 2 / tailFactor, 2 * mFactor) / 2 + 0.5f;
			} else {
				result = (float) (1.0f - Math.pow((tailFactor - 2 * x)
						/ tailFactor, 2 * mFactor)) / 2;
			}
			return result;
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值