using System;
|
using System.Threading;
|
using Android.Content;
|
using Android.Graphics;
|
using Android.Views;
|
|
namespace Shared
|
{
|
|
/// <summary>
|
/// ProgressLoading
|
/// </summary>
|
public class ProgressLoading : View
|
{
|
/// <summary>
|
/// 当前视图
|
/// </summary>
|
/// <value>The android ProgressLoadingView.</value>
|
ProgressLoadingView mProgressLoadingView
|
{
|
get
|
{
|
return AndroidView as ProgressLoadingView;
|
}
|
set
|
{
|
AndroidView = value;
|
}
|
}
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public ProgressLoading()
|
{
|
mProgressLoadingView = new ProgressLoadingView(Application.Activity, this);
|
LoadingBackgroundColor = 0x30696969;
|
|
}
|
|
Thread mLoadingThead;
|
bool IsStartLoading;
|
/// <summary>
|
/// 开始Loading线程
|
/// </summary>
|
/// <param name="mTime">最小500ms</param>
|
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();
|
}
|
|
/// <summary>
|
/// 结束Loading
|
/// </summary>
|
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;
|
}
|
|
}
|
|
/// <summary>
|
/// 结束Loading
|
/// </summary>
|
public void HideLoading()
|
{
|
EndLoading();
|
SetIsNeedRemove();
|
}
|
|
/// <summary>
|
/// 当前进度值
|
/// </summary>
|
/// <value>进度值</value>
|
public float Progress
|
{
|
set
|
{
|
(AndroidView as ProgressLoadingView).Progress = value;
|
}
|
get
|
{
|
return (AndroidView as ProgressLoadingView).Progress;
|
}
|
}
|
|
|
/// <summary>
|
/// 当前进度宽
|
/// </summary>
|
/// <value>进度值</value>
|
public int ProgressWidth
|
{
|
set
|
{
|
(AndroidView as ProgressLoadingView).ProgressWidth = value;
|
}
|
get
|
{
|
return (int)(AndroidView as ProgressLoadingView).ProgressWidth;
|
}
|
}
|
|
/// <summary>
|
/// 当前进度半径
|
/// </summary>
|
/// <value>进度值</value>
|
public int ProgressRadius
|
{
|
set
|
{
|
(AndroidView as ProgressLoadingView).Radius = value;
|
}
|
get
|
{
|
return (int)(AndroidView as ProgressLoadingView).Radius;
|
}
|
}
|
|
/// <summary>
|
/// 进度条颜色
|
/// </summary>
|
/// <value>单一进度条颜色</value>
|
public uint ProgressBarColor
|
{
|
set
|
{
|
(AndroidView as ProgressLoadingView).ProgressBarColor = HDLUtils.GetUIColorWithUint(value);
|
|
(AndroidView as ProgressLoadingView).PostInvalidate();
|
|
|
}
|
|
}
|
|
Android.Graphics.Color mLoadingBackgroundColor = Android.Graphics.Color.Gray;
|
/// <summary>
|
/// LoadingBackgroundColor
|
/// </summary>
|
/// <value>背景颜色</value>
|
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;
|
|
}
|
|
/// <summary>
|
/// 绘图的Paint
|
/// </summary>
|
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();
|
}
|
|
}
|
|
}
|
}
|
|
}
|