using System; using System.Threading; using Android.Content; using Android.Graphics; using Android.Views; namespace Shared { /// /// ProgressLoading /// public class ProgressLoading : View { /// /// 当前视图 /// /// The android ProgressLoadingView. ProgressLoadingView mProgressLoadingView { get { return AndroidView as ProgressLoadingView; } set { AndroidView = value; } } /// /// 构造函数 /// public ProgressLoading() { mProgressLoadingView = new ProgressLoadingView(Application.Activity, this); LoadingBackgroundColor = 0x30696969; } Thread mLoadingThead; bool IsStartLoading; /// /// 开始Loading线程 /// /// 最小500ms public void StartLoading(int mTime = 3000) { if (mTime < 500) mTime = 500; EndLoading(); mProgressLoadingView.SetBackgroundColor(mLoadingBackgroundColor); //Progress = 0; float PP = 0; int count = (int)(mTime * 0.01f); mLoadingThead = new Thread((obj) => { try { IsStartLoading = true; while (PP <= 1 && IsStartLoading) { PP = PP + 0.01f; Application.RunOnMainThread(() => { Progress = PP; }); Thread.Sleep(count); } } catch { } finally { IsStartLoading = false; Application.RunOnMainThread(() => { Progress = 0; mProgressLoadingView.SetBackgroundColor(Android.Graphics.Color.Transparent); SetIsNeedRemove(); }); } }); mLoadingThead.Start(); } /// /// 结束Loading /// public void EndLoading() { IsStartLoading = false; if (mLoadingThead != null) mLoadingThead.Abort(); Progress = 0; mProgressLoadingView.SetBackgroundColor(Android.Graphics.Color.Transparent); } void SetIsNeedRemove() { if (IsNeedRemove) { this.RemoveFromParent(); } } bool isNeedRemove = true; public bool IsNeedRemove { get { return isNeedRemove; } set { isNeedRemove = value; } } /// /// 结束Loading /// public void HideLoading() { EndLoading(); SetIsNeedRemove(); } /// /// 当前进度值 /// /// 进度值 public float Progress { set { (AndroidView as ProgressLoadingView).Progress = value; } get { return (AndroidView as ProgressLoadingView).Progress; } } /// /// 当前进度宽 /// /// 进度值 public int ProgressWidth { set { (AndroidView as ProgressLoadingView).ProgressWidth = value; } get { return (int)(AndroidView as ProgressLoadingView).ProgressWidth; } } /// /// 当前进度半径 /// /// 进度值 public int ProgressRadius { set { (AndroidView as ProgressLoadingView).Radius = value; } get { return (int)(AndroidView as ProgressLoadingView).Radius; } } /// /// 进度条颜色 /// /// 单一进度条颜色 public uint ProgressBarColor { set { (AndroidView as ProgressLoadingView).ProgressBarColor = HDLUtils.GetUIColorWithUint(value); (AndroidView as ProgressLoadingView).PostInvalidate(); } } Android.Graphics.Color mLoadingBackgroundColor = Android.Graphics.Color.Gray; /// /// LoadingBackgroundColor /// /// 背景颜色 public uint LoadingBackgroundColor { set { mLoadingBackgroundColor = HDLUtils.GetUIColorWithUint(value); } } public class ProgressLoadingView : Android.Views.View { View view; public ProgressLoadingView(Context context, View view) : base(context) { this.view = view; initProgressBarPaint(); Radius = mRadius; //SetBackgroundColor(Android.Graphics.Color.Transparent); } public override bool OnTouchEvent(MotionEvent e) { var v = base.OnTouchEvent(e); view?.TouchEvent(e); return true; } // 初始化进度圆弧画笔 private void initProgressBarPaint() { mArcProgressBarPaint = new Paint(); //初始化进度圆弧画笔 mArcProgressBarPaint.AntiAlias = true; mArcProgressBarPaint.Color = mProgressBarColor; mArcProgressBarPaint.SetStyle(Paint.Style.Stroke); mArcProgressBarPaint.StrokeCap = Paint.Cap.Round; mArcProgressBarPaint.StrokeJoin = Paint.Join.Round; mArcProgressBarPaint.StrokeWidth = mProgressWidth; } /// /// 绘图的Paint /// private Paint mArcProgressBarPaint; //public Android.Graphics.Color mProgressBarColor = Android.Graphics.Color.White; public float StartAngle = 0.0f; protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); if (isShow && mProgress > 0) { canvas.Save(); canvas.Rotate(-90, Width / 2, Height / 2); //float nowAngle = mProgress * 360.0f; //float endAngle; //if (nowAngle >= 90) //{ // endAngle = nowAngle - 90; //} //else { // endAngle = (float)(StartAngle + mProgress * 360.0f); //} float endAngle = (float)(StartAngle + mProgress * 360.0f); canvas.DrawArc(mRect, StartAngle, endAngle, false, mArcProgressBarPaint); canvas.Restore(); } } private RectF mRect; protected override void OnSizeChanged(int w, int h, int oldw, int oldh) { base.OnSizeChanged(w, h, oldw, oldh); RefreshRectF(); } void RefreshRectF() { mRect = new RectF(this.Width / 2 - mRadius, this.Height / 2 - mRadius, this.Width / 2 + mRadius, this.Height / 2 + mRadius); } Android.Graphics.Color mProgressBarColor = Android.Graphics.Color.White; public Android.Graphics.Color ProgressBarColor { get { return mProgressBarColor; } set { mProgressBarColor = value; mArcProgressBarPaint.Color = mProgressBarColor; } } float mProgressWidth = DensityUtil.Dip2Px(3); public float ProgressWidth { get { return mProgressWidth; } set { mProgressWidth = value; mArcProgressBarPaint.StrokeWidth = mProgressWidth; } } float mRadius = DensityUtil.Dip2Px(15); public float Radius { get { return mRadius; } set { mRadius = value; RefreshRectF(); } } bool isShow = true; public bool IsShow { get { return isShow; } set { isShow = value; PostInvalidate(); } } float mProgress = 0.0f; public float Progress { get { return mProgress; } set { if (value < 0) value = 0.0f; if (value > 1) value = 1.0f; mProgress = value; PostInvalidate(); } } } } }