本文代码从java项目移植到.net项目   java开源项目:https://github.com/jgilfelt/android-viewbadger

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics; using Java.Lang;
using Android.Util;
using Android.Content.Res;
using Android.Views.Animations;
using Android.Graphics.Drawables;
using Android.Graphics.Drawables.Shapes;
using static Android.Resource;
using static Android.App.ActionBar; namespace Dorid.UI
{
public class BadgeView : TextView
{
public const int POSITION_TOP_LEFT = ;
public const int POSITION_TOP_RIGHT = ;
public const int POSITION_BOTTOM_LEFT = ;
public const int POSITION_BOTTOM_RIGHT = ;
public const int POSITION_CENTER = ; private const int DEFAULT_MARGIN_DIP = ;
private const int DEFAULT_LR_PADDING_DIP = ;
private const int DEFAULT_CORNER_RADIUS_DIP = ;
private const int DEFAULT_POSITION = POSITION_TOP_RIGHT;
private static Android.Graphics.Color DEFAULT_BADGE_COLOR = Android.Graphics.Color.ParseColor("#CCFF0000"); //Color.RED;
private static Android.Graphics.Color DEFAULT_TEXT_COLOR = Android.Graphics.Color.White; private static Android.Views.Animations.Animation fadeIn;
private static Android.Views.Animations.Animation fadeOut; private Context context;
private View target; private int badgePosition;
private int badgeMarginH;
private int badgeMarginV;
private Android.Graphics.Color badgeColor; private bool isShown; private ShapeDrawable badgeBg; private int targetTabIndex; public BadgeView(Context context):this(context,(IAttributeSet)null, Android.Resource.Attribute.TextViewStyle)
{ } public BadgeView(Context context, IAttributeSet attrs) : this(context, attrs, Android.Resource.Attribute.TextViewStyle)
{ } /**
* Constructor -
*
* create a new BadgeView instance attached to a target {@link android.view.View}.
*
* @param context context for this view.
* @param target the View to attach the badge to.
*/
public BadgeView(Context context, View target) : this(context,null, Android.Resource.Attribute.TextViewStyle,target,)
{ } /**
* Constructor -
*
* create a new BadgeView instance attached to a target {@link android.widget.TabWidget}
* tab at a given index.
*
* @param context context for this view.
* @param target the TabWidget to attach the badge to.
* @param index the position of the tab within the target.
*/
public BadgeView(Context context, TabWidget target, int index):this(context,null, Android.Resource.Attribute.TextViewStyle,target,index)
{ } public BadgeView(Context context, IAttributeSet attrs, int defStyle):this(context,attrs,defStyle,null,)
{ } public BadgeView(Context context, IAttributeSet attrs, int defStyle, View target, int tabIndex):base(context,attrs,defStyle)
{
init(context, target, tabIndex);
} private void init(Context context, View target, int tabIndex)
{ this.context = context;
this.target = target;
this.targetTabIndex = tabIndex; // apply defaults
badgePosition = DEFAULT_POSITION;
badgeMarginH = DipToPixels(DEFAULT_MARGIN_DIP);
badgeMarginV = badgeMarginH;
badgeColor = DEFAULT_BADGE_COLOR; Typeface = Typeface.DefaultBold;
int paddingPixels = DipToPixels(DEFAULT_LR_PADDING_DIP); SetPadding(paddingPixels, , paddingPixels, );
SetTextColor(DEFAULT_TEXT_COLOR); fadeIn = new AlphaAnimation(, );
fadeIn.Interpolator = new DecelerateInterpolator();
fadeIn.Duration = ; fadeOut = new AlphaAnimation(, );
fadeOut.Interpolator = new DecelerateInterpolator();
fadeOut.Duration = ; isShown = false; if (this.target != null)
{
ApplyTo(this.target);
}
else
{
Show();
} } private void ApplyTo(View target)
{ ViewGroup.LayoutParams lp = target.LayoutParameters;
IViewParent parent = target.Parent;
FrameLayout container = new FrameLayout(context); if (target is TabWidget) { // set target to the relevant tab child container
target = ((TabWidget)target).GetChildTabViewAt(targetTabIndex);
this.target = target; ((ViewGroup)target).AddView(container,
new LayoutParams(LayoutParams.FillParent, LayoutParams.FillParent)); this.Visibility= ViewStates.Gone;
container.AddView(this); } else { // TODO verify that parent is indeed a ViewGroup
ViewGroup group = (ViewGroup)parent;
int index = group.IndexOfChild(target); group.RemoveView(target);
group.AddView(container, index, lp); container.AddView(target); this.Visibility= ViewStates.Gone;
container.AddView(this); group.Invalidate(); } } /**
* Make the badge visible in the UI.
*
*/
public void Show()
{
Show(false, null);
} /**
* Make the badge visible in the UI.
*
* @param animate flag to apply the default fade-in animation.
*/
public void Show(bool animate)
{
Show(animate, fadeIn);
} /**
* Make the badge visible in the UI.
*
* @param anim Animation to apply to the view when made visible.
*/
public void Show(Android.Views.Animations.Animation anim)
{
Show(true, anim);
} /**
* Make the badge non-visible in the UI.
*
*/
public void Hide()
{
Hide(false, null);
} /**
* Make the badge non-visible in the UI.
*
* @param animate flag to apply the default fade-out animation.
*/
public void Hide(bool animate)
{
Hide(animate, fadeOut);
} /**
* Make the badge non-visible in the UI.
*
* @param anim Animation to apply to the view when made non-visible.
*/
public void Hide(Android.Views.Animations.Animation anim)
{
Hide(true, anim);
} /**
* Toggle the badge visibility in the UI.
*
*/
public void Toggle()
{
Toggle(false, null, null);
} /**
* Toggle the badge visibility in the UI.
*
* @param animate flag to apply the default fade-in/out animation.
*/
public void Toggle(bool animate)
{
Toggle(animate, fadeIn, fadeOut);
} /**
* Toggle the badge visibility in the UI.
*
* @param animIn Animation to apply to the view when made visible.
* @param animOut Animation to apply to the view when made non-visible.
*/
public void Toggle(Android.Views.Animations.Animation animIn, Android.Views.Animations.Animation animOut)
{
Toggle(true, animIn, animOut);
} private void Show(bool animate, Android.Views.Animations.Animation anim)
{ if (Background == null)
{
if (badgeBg == null)
{
badgeBg = getDefaultBackground();
} SetBackgroundDrawable(badgeBg);
}
ApplyLayoutParams(); if (animate)
{
this.StartAnimation(anim);
}
this.Visibility = ViewStates.Visible;
isShown = true;
} private void Hide(bool animate, Android.Views.Animations.Animation anim)
{
this.Visibility = ViewStates.Gone;
if (animate)
{
this.StartAnimation(anim);
}
isShown = false;
} private void Toggle(bool animate, Android.Views.Animations.Animation animIn, Android.Views.Animations.Animation animOut)
{
if (isShown)
{
Hide(animate && (animOut != null), animOut);
}
else
{
Show(animate && (animIn != null), animIn);
}
} /**
* Increment the numeric badge label. If the current badge label cannot be converted to
* an integer value, its label will be set to "0".
*
* @param offset the increment offset.
*/
public int Increment(int offset)
{
var txt = Text;
int i;
if (txt != null)
{
try
{
i = Convert.ToInt32(txt.ToString());
}
catch (NumberFormatException e)
{
i = ;
}
}
else
{
i = ;
}
i = i + offset;
//Text = String.ValueOf(i); return i;
} /**
* Decrement the numeric badge label. If the current badge label cannot be converted to
* an integer value, its label will be set to "0".
*
* @param offset the decrement offset.
*/
public int Decrement(int offset)
{
return Increment(-offset);
} private ShapeDrawable getDefaultBackground()
{ int r = DipToPixels(DEFAULT_CORNER_RADIUS_DIP);
float[] outerR = new float[] { r, r, r, r, r, r, r, r }; RoundRectShape rr = new RoundRectShape(outerR, null, null);
ShapeDrawable drawable = new ShapeDrawable(rr);
drawable.Paint.Color = badgeColor; return drawable; } private void ApplyLayoutParams()
{ FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent); switch (badgePosition)
{
case POSITION_TOP_LEFT:
lp.Gravity = GravityFlags.Left | GravityFlags.Top;
lp.SetMargins(badgeMarginH, badgeMarginV, , );
break;
case POSITION_TOP_RIGHT:
lp.Gravity = GravityFlags.Right | GravityFlags.Top;
lp.SetMargins(, badgeMarginV, badgeMarginH, );
break;
case POSITION_BOTTOM_LEFT:
lp.Gravity = GravityFlags.Left | GravityFlags.Bottom;
lp.SetMargins(badgeMarginH, , , badgeMarginV);
break;
case POSITION_BOTTOM_RIGHT:
lp.Gravity = GravityFlags.Right | GravityFlags.Bottom;
lp.SetMargins(, , badgeMarginH, badgeMarginV);
break;
case POSITION_CENTER:
lp.Gravity = GravityFlags.Center;
lp.SetMargins(, , , );
break;
default:
break;
} LayoutParameters = lp;
} /**
* Returns the target View this badge has been attached to.
*
*/
public View GetTarget()
{
return target;
} /**
* Is this badge currently visible in the UI?
*
*/
public override bool IsShown
{
get
{
return isShown;
}
} /**
* Returns the positioning of this badge.
*
* one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
*
*/
public int getBadgePosition()
{
return badgePosition;
} /**
* Set the positioning of this badge.
*
* @param layoutPosition one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
*
*/
public void setBadgePosition(int layoutPosition)
{
this.badgePosition = layoutPosition;
} /**
* Returns the horizontal margin from the target View that is applied to this badge.
*
*/
public int getHorizontalBadgeMargin()
{
return badgeMarginH;
} /**
* Returns the vertical margin from the target View that is applied to this badge.
*
*/
public int getVerticalBadgeMargin()
{
return badgeMarginV;
} /**
* Set the horizontal/vertical margin from the target View that is applied to this badge.
*
* @param badgeMargin the margin in pixels.
*/
public void setBadgeMargin(int badgeMargin)
{
this.badgeMarginH = badgeMargin;
this.badgeMarginV = badgeMargin;
} /**
* Set the horizontal/vertical margin from the target View that is applied to this badge.
*
* @param horizontal margin in pixels.
* @param vertical margin in pixels.
*/
public void setBadgeMargin(int horizontal, int vertical)
{
this.badgeMarginH = horizontal;
this.badgeMarginV = vertical;
} /**
* Returns the color value of the badge background.
*
*/
public int getBadgeBackgroundColor()
{
return badgeColor;
} /**
* Set the color value of the badge background.
*
* @param badgeColor the badge background color.
*/
public void setBadgeBackgroundColor(Android.Graphics.Color badgeColor)
{
this.badgeColor = badgeColor;
badgeBg = getDefaultBackground();
} private int DipToPixels(int dip)
{
Resources r =Resources;
float px = TypedValue.ApplyDimension(ComplexUnitType.Dip, dip, r.DisplayMetrics);
return (int)px;
}
}
}

使用方法:

    [Activity(Label = "BadgeActivity", MainLauncher = true)]
public class BadgeActivity : Activity
{
private ZsCMS.Dorid.UI.BadgeView bv;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Badge); View buttion= FindViewById(Resource.Id.button1);
bv = new UI.BadgeView(this, buttion);
bv.Text = "";
bv.Show(); buttion.Click += Buttion_Click;
// Create your application here
} private int i = ;
private void Buttion_Click(object sender, EventArgs e)
{
bv.Text = (++i).ToString();
}
}

xamarin.android 给View控件 添加数字提醒效果-BadgeView的更多相关文章

  1. Android自定义view控件

    转载自: http://blog.163.com/ppy2790@126/blog/static/103242241201382210910473/ 开发自定义控件的步骤: 1.了解View的工作原理 ...

  2. 安卓控件 仪表盘控件 柱状图控件 曲线控件 xamarin.android 分类器 瓶子控件 报警控件 水箱控件 进度条控件等

    本篇博客主要介绍一个控件库,HslControls.dll 的界面,这个控件库支持winform,winform的参考另一篇文章:https://www.cnblogs.com/dathlin/p/1 ...

  3. xamarin android——数据绑定到控件(三)

    如果当前活动中,只存在一个listview视图,可以借助ListActivity快速的实现一个列表,即当前Activity继承ListActivity.在OnCreate方法中简单的两行代码,就可以创 ...

  4. Xamarin.android 重写axml控件

    https://www.cnblogs.com/lonelyxmas/p/5632694.html <Laco: 用来用引指定的控件            android:layout_widt ...

  5. Xamarin.Android DatePickerFragment 日期控件

    MainActivity 代码: public class MainActivity : Activity { TextView _dateDisplay; Button _dateSelectBut ...

  6. xamarin android——数据绑定到控件(二)

    本示例为通过媒体内容提供器获取本机中的图片显示在Gallery中. 活动中简单的初始化代码 private void InitGallery() { Gallery gallery = FindVie ...

  7. xamarin android——数据绑定到控件(四)

    本文为通过自定义列表适配器定义ListView,以上文为基础,基于ListActivity. 定义列表项布局,包含一个图片显示,标题和描述 <LinearLayout xmlns:android ...

  8. xamarin android——数据绑定到控件(一)

    mono for android 中光标由ICursor 接口标识,该接口公开了操作结果数据集的所有方法.光标的使用非常消耗系统资源,所以不使用时应该光比光标.可以通过StartManagingCur ...

  9. Android给控件添加默认点击效果

    Android控件点击效果 Android中Button控件是有点击效果的,但是像TextView.ImageView.各种Layout是没有点击效果的,给TextView设置点击事件后,加个点击效果 ...

随机推荐

  1. C#文件和文件夹输入输出流代码

    1.建立一个文本文件 public class FileClass { public static void Main() { WriteToFile(); } static void WriteTo ...

  2. 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedFilesVector VirtualizedItemsVector 绑定

    [源码下载] 重新想象 Windows 8 Store Apps (53) - 绑定: 与 ObservableCollection CollectionViewSource VirtualizedF ...

  3. WebGL/X3DOM 跑在 iOS

    iOS是最早支持WebGL的移动操作系统之一,我们一直在努力让X3DOM运行在那些设备上.然而,标准的Safari浏览器默认是没有开启的.这种情况从iOS8发生改变,iOS8现在完全支持WebGL - ...

  4. coffeescript 1.8.0 documents

    CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...

  5. HTML页面导航栏页脚不动,变换中间部分

    代码段一: <script> $(document).ready(function() { $("#teachingObjectives").click(functio ...

  6. SDK Build Tools revision (19.0.3) is too low for project Min

    SDK Build Tools revision (19.0.3) is too low for project Min(转)       如果你正在使用Android Studio工具进行开发,且将 ...

  7. Xml序列化、反序列化帮助类

    之前从网络上找了一个Xml处理帮助类,并整理了一下,这个帮助类针对Object类型进行序列化和反序列化,而不需要提前定义Xml的结构,把它放在这儿供以后使用 /// <summary> / ...

  8. Jquery学习—jquery的事件

    1.Jquery事件1:one 1)one() 方法是为所选的元素绑定一个仅出发一次的处理函数,调用格式 one(type,[data],fn) 2)其中参数type是事件类型,即需要触发什么类型的事 ...

  9. ArcObject10.1降级至10.0

    最开始接触ArcGIS版本是9.3,为了需要也安装了9.2进行开发:因为自己的电脑配置较低,所以跑不起10.0中文版:毕业工作后,行业内用10.1居多(虽然10.3已出):现在10.4都要出来了:由于 ...

  10. 实战2--应用EL表达式显示投票结果

    (1)编写index.jsp页面,用于收集投票信息 <%@ page language="java" pageEncoding="GBK"%> &l ...