您好,登錄后才能下訂單哦!
一個(gè)WP8樣式的進(jìn)度對(duì)話(huà)框。對(duì)應(yīng)Android轉(zhuǎn)圈圈的ProgressBar。使用方法和普通的dialog一樣
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是Win8ProgressDialog顯示的view,其實(shí)是一個(gè)包含了運(yùn)動(dòng)五個(gè)小藍(lán)點(diǎn)的RelativeLayout,小藍(lán)點(diǎn)的動(dòng)畫(huà)使用了一個(gè)先減速后加速的插值器DecelerateAccelerateStopInterpolator
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(); } // 先減速再加速的插值器,在加速完成后會(huì)再停止一會(huì)兒,循環(huán)地進(jìn)行這個(gè)動(dòng)畫(huà),就和WP8的Loading樣式一樣了 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; } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。