using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Util;
using Android.Graphics.Drawables;
using Android.Views.Animations;
using Android.Graphics;
using Android.OS;
using Android.Views.InputMethods;
using Android.Text;
namespace Shared
{
//已经全面检查了代码
///
/// Button 按键
///
public class Button : View
{
///
/// 当前视图
///
/// The android button.
AndroidButton currentButton {
get {
return AndroidView as AndroidButton;
}
set {
AndroidView = value;
}
}
///
/// 构造函数
///
public Button ()
{
currentButton = new AndroidButton (Application.Activity, this);
currentButton.SetTextSize (ComplexUnitType.Sp, Application.FontSize);
}
byte[] imageBytes;
public byte[] ImageBytes
{
get
{
return imageBytes;
}
set
{
imageBytes = value;
IsSelected = isSelected;
}
}
///
/// 文字大小
///
/// The size of the text.
public float TextSize {
get {
return currentButton.TextSize;
}
set {
currentButton.SetTextSize (ComplexUnitType.Sp, value);
}
}
int textID;
///
/// 根据ID获取对应的备注
///
/// The text I.
public int TextID {
get {
return textID;
}
set {
textID = value;
Text = Language.StringByID (TextID);
}
}
///
/// 文本
///
/// The text.
public string Text {
get {
return currentButton.Text;
}
set {
currentButton.Text = value;
}
}
/////
///// 字体名称
/////
///// The name of the font.
//public string FontName {
// get {
// return "";
// }
// set {
// var typeFace = Typeface.CreateFromAsset (Shared.Application.Activity.Assets, "titilliumtext25l005.otf");
// if (typeFace != null) {
// currentButton.SetTypeface (typeFace, TypefaceStyle.Normal);
// }
// }
//}
/////
///// 普通字体
/////
//string mFontNameNormal;
//Typeface typeFaceNormal;
//public string FontNameNormal {
// get
// {
// return mFontNameNormal;
// }
// set
// {
// mFontNameNormal = value;
// var typeFace = Typeface.CreateFromAsset(Shared.Application.Activity.Assets, mFontNameNormal);
// if (typeFace != null)
// {
// typeFaceNormal = typeFace;
// }
// }
//}
//string mFontNameBold;
//Typeface typeFaceBold;
//public string FontNameBold
//{
// get
// {
// return mFontNameBold;
// }
// set
// {
// mFontNameBold = value;
// var typeFace = Typeface.CreateFromAsset(Shared.Application.Activity.Assets, mFontNameBold);
// if (typeFace != null)
// {
// typeFaceBold = typeFace;
// }
// }
//}
string mFontName;
///
/// 字体名称
///
/// The name of the font.
public string FontName
{
get
{
return mFontName;
}
set
{
try
{
var typeFace = Typeface.CreateFromAsset(Shared.Application.Activity.Assets, value);
if (typeFace != null)
{
mFontName = value;
currentButton.SetTypeface(typeFace, TypefaceStyle.Normal);
}
}
catch {
}
}
}
bool isMoreLines;
public bool IsMoreLines {
get {
return isMoreLines;
}
set {
isMoreLines = value;
currentButton.SetSingleLine (!value);
}
}
///
/// 是否显示粗体
///
bool isBold;
public bool IsBold
{
get
{
return isBold;
}
set
{
isBold = value;
if (isBold)
{
currentButton.SetTypeface(Typeface.DefaultFromStyle(TypefaceStyle.Bold), TypefaceStyle.Bold);
}
else {
currentButton.SetTypeface(Typeface.DefaultFromStyle(TypefaceStyle.Normal), TypefaceStyle.Normal);
}
}
}
///
/// 获取字体长度
///
public int GetTextWidth()
{
TextPaint textPaint = new TextPaint();
textPaint.TextSize = this.TextSize;
return (int)textPaint.MeasureText(this.Text);
}
///
/// 刷新
///
public override void Refresh ()
{
base.Refresh ();
IsSelected = isSelected;
}
uint textColor = 0xFFFFFFFF;
///
/// 文字颜色
///
/// The color of the text.
public uint TextColor {
get {
return textColor;
}
set {
textColor = value;
if (IsSelected) {
return;
}
byte r, g, b, a;
r = (byte)(textColor / 256 / 256 % 256);
g = (byte)(textColor / 256 % 256);
b = (byte)(textColor % 256);
a = (byte)(textColor / 256 / 256 / 256 % 256);
currentButton.SetTextColor (Android.Graphics.Color.Argb (a, r, g, b));
}
}
uint selecteTextColor = 0xFFFFFFFF;
///
/// 文字颜色
///
/// The color of the text.
public uint SelectedTextColor {
get {
return selecteTextColor;
}
set {
selecteTextColor = value;
if (!IsSelected) {
return;
}
byte r, g, b, a;
r = (byte)(selecteTextColor / 256 / 256 % 256);
g = (byte)(selecteTextColor / 256 % 256);
b = (byte)(selecteTextColor % 256);
a = (byte)(selecteTextColor / 256 / 256 / 256 % 256);
currentButton.SetTextColor (Android.Graphics.Color.Argb (a, r, g, b));
}
}
bool isSelected;
///
/// 选中状态
///
/// true if this instance is selected; otherwise, false.
public bool IsSelected {
get {
return isSelected;
}
set {
isSelected = value;
if (!IsCanRefresh) {
return;
}
if (isSelected) {
if (SelectedImagePath == null) {
SelectedBackgroundColor = SelectedBackgroundColor;
} else {
Background (SelectedImagePath);
}
SelectedTextColor = selecteTextColor;
} else {
if (imageBytes != null)
{
AndroidView.Background = Bytes2Drawable(imageBytes);
return;
}
if (UnSelectedImagePath == null) {
BackgroundColor = BackgroundColor;
} else {
Background (UnSelectedImagePath);
}
TextColor = textColor;
}
}
}
TextAlignment textAlignment = TextAlignment.Center;
///
/// 文字对齐方式
///
/// 横向
/// 纵向
public TextAlignment TextAlignment {
get {
return textAlignment;
}
set {
textAlignment = value;
switch (value) {
case TextAlignment.TopLeft:
currentButton.Gravity = GravityFlags.Top | GravityFlags.Left;
break;
case TextAlignment.TopCenter:
currentButton.Gravity = GravityFlags.Top | GravityFlags.Center;
break;
case TextAlignment.TopRight:
currentButton.Gravity = GravityFlags.Top | GravityFlags.Right;
break;
case TextAlignment.CenterLeft:
currentButton.Gravity = GravityFlags.Center | GravityFlags.Left;
break;
case TextAlignment.Center:
currentButton.Gravity = GravityFlags.Center | GravityFlags.Center;
break;
case TextAlignment.CenterRight:
currentButton.Gravity = GravityFlags.Center | GravityFlags.Right;
break;
case TextAlignment.BottomLeft:
currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Left;
break;
case TextAlignment.BottomCenter:
currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Center;
break;
case TextAlignment.BottomRight:
currentButton.Gravity = GravityFlags.Bottom | GravityFlags.Right;
break;
}
}
}
///
/// 内边距
///
/// The padding.
public override Padding Padding {
get {
return new Padding (currentButton.PaddingTop, currentButton.PaddingLeft, currentButton.PaddingBottom, currentButton.PaddingRight);
}
set {
currentButton.SetPadding (value.Left, value.Top, value.Right, value.Bottom);
}
}
string selectedImagePath;
///
/// 选择时背景图路径
///
/// The selected image path.
public string SelectedImagePath {
get {
return selectedImagePath;
}
set {
selectedImagePath = value;
IsSelected = isSelected;
}
}
string unSelectedImagePath;
///
/// 非选中状态的背景图路径
///
/// The un selected image path.
public string UnSelectedImagePath {
get {
return unSelectedImagePath;
}
set {
unSelectedImagePath = value;
IsSelected = isSelected;
}
}
uint selectedBackgroundColor;
///
/// 选择时背景颜色
///
/// The color of the text.
public uint SelectedBackgroundColor {
get {
return selectedBackgroundColor;
}
set {
selectedBackgroundColor = value;
if (AndroidView.Background == null || AndroidView.Background.GetType () != typeof (GradientDrawable)) {
AndroidView.Background = new Android.Graphics.Drawables.GradientDrawable ();
}
var gradientDrawable = (GradientDrawable)AndroidView.Background;
gradientDrawable.SetCornerRadius (DensityUtil.Dip2Px (Radius));
byte r, g, b, a;
r = (byte)(BorderColor / 256 / 256 % 256);
g = (byte)(BorderColor / 256 % 256);
b = (byte)(BorderColor % 256);
a = (byte)(BorderColor / 256 / 256 / 256 % 256);
gradientDrawable.SetStroke ((int)BorderWidth, Android.Graphics.Color.Argb (a, r, g, b));
r = (byte)(selectedBackgroundColor / 256 / 256 % 256);
g = (byte)(selectedBackgroundColor / 256 % 256);
b = (byte)(selectedBackgroundColor % 256);
a = (byte)(selectedBackgroundColor / 256 / 256 / 256 % 256);
gradientDrawable.SetColor (Android.Graphics.Color.Argb (a, r, g, b).ToArgb ());
}
}
internal class AndroidButton : Android.Widget.Button
{
View view;
public AndroidButton (Context context, View view)
: base (context)
{
this.view = view;
SetBackgroundColor (Android.Graphics.Color.Transparent);
SetPadding (0, 0, 0, 0);
SetTextColor (Android.Graphics.Color.White);
SetLines (1);
SetEllipsize (true);
}
public override bool OnTouchEvent (MotionEvent e)
{
var v = base.OnTouchEvent (e);
view?.TouchEvent (e);
return true;
}
public void SetEllipsize (bool ellipsize) {
if (ellipsize) {
Ellipsize = TextUtils.TruncateAt.End;
} else {
Ellipsize = TextUtils.TruncateAt.Marquee;
}
}
public override TextUtils.TruncateAt Ellipsize { get => base.Ellipsize; set => base.Ellipsize = value; }
}
}
}