using System;
|
using Android.Views;
|
using Android.Content;
|
using Android.Util;
|
using Android.Runtime;
|
|
namespace Shared
|
{
|
//已经全面检查了代码
|
/// <summary>
|
/// 位置布局
|
/// </summary>
|
public class HorizontalSeekBar : View
|
{
|
Android.Widget.ImageView backImageView;
|
Android.Widget.ImageView progressImageView;
|
Android.Widget.Button thumbButton;
|
AndroidHorizontalSeekBar androidHorizonetalSeekBar;
|
/// <summary>
|
/// 当前实例
|
/// </summary>
|
public HorizontalSeekBar()
|
{
|
AndroidView = new AndroidFrameLayout(Application.Activity, null) { };
|
//AndroidView.SetBackgroundColor (Android.Graphics.Color.DarkCyan);
|
{
|
backImageView = new Android.Widget.ImageView(Application.Activity, null) { Enabled=false };
|
backImageView.SetBackgroundColor(Android.Graphics.Color.WhiteSmoke);
|
(AndroidView as AndroidFrameLayout).AddView(backImageView, new Android.Widget.FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Dip2Px(2), GravityFlags.CenterVertical));
|
}
|
|
progressImageView = new Android.Widget.ImageView(Application.Activity, null) { Enabled=false};
|
progressImageView.SetBackgroundColor(Android.Graphics.Color.Rgb(0xFE, 0x5E, 00));
|
(AndroidView as AndroidFrameLayout).AddView(progressImageView, new Android.Widget.FrameLayout.LayoutParams(0, Dip2Px(2), GravityFlags.CenterVertical));
|
|
thumbButton = new Android.Widget.Button(Application.Activity, null) { Background = new Android.Graphics.Drawables.GradientDrawable(),Enabled=false };
|
(AndroidView as AndroidFrameLayout).AddView(thumbButton, new Android.Widget.FrameLayout.LayoutParams(Dip2Px(ThumbRadius), Dip2Px(ThumbRadius), GravityFlags.CenterVertical));
|
|
androidHorizonetalSeekBar = new AndroidHorizontalSeekBar(Application.Activity, this);
|
(AndroidView as AndroidFrameLayout).AddView(androidHorizonetalSeekBar, new Android.Widget.FrameLayout.LayoutParams(Android.Views.ViewGroup.LayoutParams.MatchParent, Android.Views.ViewGroup.LayoutParams.MatchParent));
|
|
}
|
|
uint progressColor = 0xFFEB642C;
|
/// <summary>
|
/// 进度颜色
|
/// </summary>
|
/// <value>The color of the progress.</value>
|
public uint ProgressColor
|
{
|
get
|
{
|
return progressColor;
|
}
|
set
|
{
|
progressColor = value;
|
int r = (byte)(progressColor / 256 / 256 % 256);
|
int g = (byte)(progressColor / 256 % 256);
|
int b = (byte)(progressColor % 256);
|
int a = (byte)(progressColor / 256 / 256 / 256 % 256);
|
progressImageView.SetBackgroundColor(Android.Graphics.Color.Argb(a, r, g, b));
|
}
|
}
|
|
public int SleepTime = 0;
|
|
|
uint backgroundColor = (uint)Android.Graphics.Color.WhiteSmoke.ToArgb();
|
/// <summary>
|
/// 底部颜色
|
/// </summary>
|
/// <value>The color of the background.</value>
|
public new uint BackgroundColor
|
{
|
get
|
{
|
return backgroundColor;
|
}
|
set
|
{
|
backgroundColor = value;
|
int r = (byte)(backgroundColor / 256 / 256 % 256);
|
int g = (byte)(backgroundColor / 256 % 256);
|
int b = (byte)(backgroundColor % 256);
|
int a = (byte)(backgroundColor / 256 / 256 / 256 % 256);
|
backImageView.SetBackgroundColor(Android.Graphics.Color.Argb(a, r, g, b));
|
}
|
}
|
|
/// <summary>
|
/// 进度变化事件
|
/// </summary>
|
public Action<View, int> ProgressChanged;
|
|
/// <summary>
|
/// 最大值
|
/// </summary>
|
/// <value>The max.</value>
|
public int Max
|
{
|
get
|
{
|
return androidHorizonetalSeekBar.Max;
|
}
|
set
|
{
|
androidHorizonetalSeekBar.Max = value;
|
}
|
}
|
/// <summary>
|
/// 是否允许滑动滑块
|
/// </summary>
|
/// <value><c>true</c> if is can scrolled; otherwise, <c>false</c>.</value>
|
public bool IsCanScrolled
|
{
|
get
|
{
|
return androidHorizonetalSeekBar.Enabled;
|
}
|
set
|
{
|
thumbButton.Visibility = value ? ViewStates.Visible : ViewStates.Gone;
|
androidHorizonetalSeekBar.Enabled = value;
|
}
|
}
|
|
//2019-08-29 改100毫秒
|
public int DelayTime = 100;
|
//防止跳动,延时显示状态
|
public DateTime delayDateTime = DateTime.MinValue;
|
/// <summary>
|
/// 当前进度
|
/// </summary>
|
/// <value>The progress.</value>
|
public int Progress
|
{
|
get
|
{
|
return androidHorizonetalSeekBar.Progress;
|
}
|
set
|
{
|
//发送命令后1000毫秒内不更新状态,防止跳动
|
if ((DateTime.Now - delayDateTime).TotalMilliseconds < DelayTime)
|
{
|
return;
|
}
|
androidHorizonetalSeekBar.Progress = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
|
int width = (int)(androidHorizonetalSeekBar.Progress * 1.0 / androidHorizonetalSeekBar.Max * (Width - thumbButton.LayoutParameters.Width));
|
progressImageView.LayoutParameters.Width = width + thumbButton.LayoutParameters.Width / 2;
|
var layoutParameter = (thumbButton.LayoutParameters as Android.Widget.FrameLayout.LayoutParams);
|
layoutParameter.LeftMargin = width;
|
thumbButton.LayoutParameters = layoutParameter;
|
}
|
}
|
|
uint thumbColor = 0xFFFFFFFF;
|
/// <summary>
|
/// 滑块颜色
|
/// </summary>
|
/// <value>The color of the background.</value>
|
public uint ThumbColor
|
{
|
get
|
{
|
return thumbColor;
|
}
|
set
|
{
|
thumbColor = value;
|
int r = (byte)(thumbColor / 256 / 256 % 256);
|
int g = (byte)(thumbColor / 256 % 256);
|
int b = (byte)(thumbColor % 256);
|
int a = (byte)(thumbColor / 256 / 256 / 256 % 256);
|
|
((Android.Graphics.Drawables.GradientDrawable)thumbButton.Background).SetColor(Android.Graphics.Color.Argb(a, r, g, b));
|
}
|
}
|
|
public DateTime dateTime = DateTime.MinValue;
|
internal override bool TouchEvent(MotionEvent e)
|
{
|
if (!IsCanMove||!Enable)
|
{
|
return true;
|
}
|
delayDateTime = DateTime.Now;
|
switch (e.Action)
|
{
|
case MotionEventActions.Down:
|
dateTime = DateTime.MinValue;
|
break;
|
case MotionEventActions.Move:
|
if (dateTime == DateTime.MinValue)
|
{
|
dateTime = DateTime.Now;
|
}
|
setValue(e.GetX());
|
break;
|
case MotionEventActions.Up:
|
dateTime = DateTime.MinValue;
|
setValue(e.GetX());
|
|
break;
|
}
|
return base.TouchEvent(e);
|
}
|
|
int beforValue;
|
/// <summary>
|
/// 是否可以滑动
|
/// </summary>
|
public bool IsCanMove = true;
|
void setValue(float x)
|
{
|
|
if (x < 0)
|
{
|
x = 0;
|
}
|
if (Width - thumbButton.LayoutParameters.Width < x)
|
{
|
x = Width - thumbButton.LayoutParameters.Width;
|
}
|
var tempValue = androidHorizonetalSeekBar.Progress;
|
|
androidHorizonetalSeekBar.Progress = (int)(x / (Width - thumbButton.LayoutParameters.Width) * Max);
|
|
|
int width = (int)(androidHorizonetalSeekBar.Progress * 1.0 / androidHorizonetalSeekBar.Max * (Width - thumbButton.LayoutParameters.Width));
|
progressImageView.LayoutParameters.Width = width + thumbButton.LayoutParameters.Width / 2;
|
var layoutParameter = thumbButton.LayoutParameters as Android.Widget.FrameLayout.LayoutParams;
|
layoutParameter.LeftMargin = width;
|
thumbButton.LayoutParameters = layoutParameter;
|
|
if (tempValue != androidHorizonetalSeekBar.Progress || tempValue == 0 || tempValue == Max|| dateTime== DateTime.MinValue)
|
{
|
//点击下或者弹起或者滑动时间500毫秒的时候,就触发事件
|
if (ProgressChanged != null
|
&& SleepTime < (DateTime.Now - dateTime).TotalMilliseconds)
|
{
|
dateTime = DateTime.MinValue;
|
delayDateTime = DateTime.Now;
|
ProgressChanged(this, androidHorizonetalSeekBar.Progress);
|
}
|
}
|
}
|
|
int thumbRadius = 10;
|
public int ThumbRadius
|
{
|
get
|
{
|
return thumbRadius;
|
}
|
set
|
{
|
thumbRadius = value;
|
if (!IsCanRefresh)
|
{
|
return;
|
}
|
|
thumbButton.LayoutParameters.Width = Dip2Px(ThumbRadius) * 2;
|
thumbButton.LayoutParameters.Height = Dip2Px(ThumbRadius) * 2;
|
var gradientDrawable = thumbButton.Background as Android.Graphics.Drawables.GradientDrawable;
|
gradientDrawable.SetCornerRadius(Dip2Px(ThumbRadius));
|
gradientDrawable.SetSize(Dip2Px(ThumbRadius) * 2, Dip2Px(ThumbRadius) * 2);
|
thumbButton.Background = gradientDrawable;
|
ThumbColor = thumbColor;
|
}
|
}
|
|
public override void Refresh()
|
{
|
base.Refresh();
|
ThumbRadius = thumbRadius;
|
ProgressColor = progressColor;
|
Progress = androidHorizonetalSeekBar.Progress;
|
}
|
|
/// <summary>
|
/// 需要重写的视图
|
/// </summary>
|
class AndroidHorizontalSeekBar : Android.Widget.SeekBar
|
{
|
View view;
|
public AndroidHorizontalSeekBar(Context context, View view)
|
: base(context)
|
{
|
this.view = view;
|
ProgressDrawable = null;
|
SetThumb(null);
|
}
|
/// <summary>
|
/// 如果已经是横向滑动,父控件不允许再请求中断
|
/// </summary>
|
bool isHorizontalActioned;
|
/// <summary>
|
/// 记录最后点击的X轴坐标
|
/// </summary>
|
float mLastMotionX;
|
/// <summary>
|
/// 点击Y轴的坐标
|
/// </summary>
|
float mLastMotionY;
|
public override bool OnTouchEvent(MotionEvent e)
|
{
|
switch (e.Action)
|
{
|
case MotionEventActions.Down:
|
isHorizontalActioned = false;
|
//进来的时候禁止父控制打断
|
Parent?.RequestDisallowInterceptTouchEvent(true);
|
//记录点击的X坐标
|
mLastMotionX = e.RawX;
|
mLastMotionY = e.RawY;
|
break;
|
case MotionEventActions.Move:
|
var deltaX = mLastMotionX - e.RawX;
|
var deltaY = mLastMotionY - e.RawY;
|
//如果主要滑动的是Y轴比较大,返回
|
if (Math.Abs(deltaX) < Math.Abs(deltaY))
|
{
|
if (ViewConfiguration.Get(Context).ScaledTouchSlop < Math.Abs(deltaY) && !isHorizontalActioned)
|
{
|
Parent?.RequestDisallowInterceptTouchEvent(false);
|
}
|
return true;
|
}
|
//如果不在滑动范围内,不执行滑动
|
if (Math.Abs(deltaX) < ViewConfiguration.Get(Context).ScaledTouchSlop)
|
{
|
return true;
|
}
|
isHorizontalActioned = true;
|
view?.TouchEvent(e);
|
break;
|
case MotionEventActions.Up:
|
view?.TouchEvent(e);
|
break;
|
}
|
|
return true;
|
}
|
}
|
}
|
}
|