Badgeview New hints Open Source Tool class

Source: Internet
Author: User

Badgeview is a reminder to use an icon as a new feature, similar to the number of messages or other display prompts on the top right of the SMS icon after receiving a short interest. Badgeview is a good implementation of this feature, and has been expanded to customize the location and hint icon.

Tool class source code is as follows:

 PackageCn.car273.widget;/** Badgeview.java * badgeview * * Copyright (c), Stefan jauker. *Https://github.com/kodex83/BadgeView* * Licensed under the Apache License, Version 2.0 (the "License"); * you are not a use this file except in compliance With the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is Distribut Ed on an ' as is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ImportAndroid.content.Context;ImportAndroid.graphics.Color;ImportAndroid.graphics.Typeface;Importandroid.graphics.drawable.ShapeDrawable;ImportAndroid.graphics.drawable.shapes.RoundRectShape;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.util.TypedValue;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importandroid.widget.FrameLayout;ImportAndroid.widget.FrameLayout.LayoutParams;ImportAndroid.widget.TabWidget;ImportAndroid.widget.TextView; Public classBadgeviewextendsTextView {Private BooleanMhideonnull =true;  PublicBadgeview (Context context) { This(Context,NULL); }     PublicBadgeview (Context context, AttributeSet attrs) { This(context, Attrs, Android.)    R.attr.textviewstyle); }     PublicBadgeview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle);    Init (); }    Private voidinit () {if(! (Getlayoutparams ()instanceoflayoutparams)) {Layoutparams Layoutparams=NewLayoutparams (Android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Android.view.ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.right|gravity.top);        Setlayoutparams (Layoutparams); }        //Set Default FontSetTextColor (Color.White);        Settypeface (Typeface.default_bold); Settextsize (TYPEDVALUE.COMPLEX_UNIT_SP,11); Setpadding (dip2px (5), dip2px (1), dip2px (5), dip2px (1)); //Set Default BackgroundSetBackground (9, Color.parsecolor ("#d3321b"));        Setgravity (Gravity.center); //Default ValuesSethideonnull (true); Setbadgecount (0); } @SuppressWarnings ("Deprecation")     Public voidSetBackground (intDipradius,intBadgecolor) {        intRadius =dip2px (Dipradius); float[] Radiusarray =New float[] {radius, radius, radius, radius, radius }; Roundrectshape RoundRect=NewRoundrectshape (Radiusarray,NULL,NULL); Shapedrawable bgdrawable=Newshapedrawable (RoundRect);        Bgdrawable.getpaint (). SetColor (Badgecolor);    Setbackgrounddrawable (bgdrawable); }    /**     * @returnReturns True if view is hidden on badge value 0 or null; */     Public BooleanIshideonnull () {returnMhideonnull; }    /**     * @paramHideonnull the hideonnull to set*/     Public voidSethideonnull (Booleanhideonnull) {Mhideonnull=Hideonnull;    SetText (GetText ()); }    /** (Non-javadoc) * * @see Android.widget.textview#settext (java.lang.CharSequence, Android.widget.TextView . Buffertype)*/@Override Public voidSetText (charsequence text, Buffertype type) {if(Ishideonnull () && (Text = =NULL|| Text.tostring (). Equalsignorecase ("0")) {setvisibility (view.gone); } Else{setvisibility (view.visible); }        Super. SetText (text, type); }     Public voidSetbadgecount (intcount)    {SetText (string.valueof (count)); }     PublicInteger Getbadgecount () {if(GetText () = =NULL) {            return NULL; } String Text=GetText (). toString (); Try {            returninteger.parseint (text); } Catch(NumberFormatException e) {return NULL; }    }    /**the position shown, relative to the dependent object*/     Public voidSetbadgegravity (intGravity) {Framelayout.layoutparams params=(Layoutparams) getlayoutparams (); Params.gravity=Gravity;    Setlayoutparams (params); }     Public intgetbadgegravity () {framelayout.layoutparams params=(Layoutparams) getlayoutparams (); returnparams.gravity; }     Public voidSetbadgemargin (intDipmargin)    {Setbadgemargin (Dipmargin, Dipmargin, Dipmargin, Dipmargin); }     Public voidSetbadgemargin (intLeftdipmargin,intTopdipmargin,intRightdipmargin,intBottomdipmargin) {Framelayout.layoutparams params=(Layoutparams) getlayoutparams (); Params.leftmargin=dip2px (Leftdipmargin); Params.topmargin=dip2px (Topdipmargin); Params.rightmargin=dip2px (Rightdipmargin); Params.bottommargin=dip2px (Bottomdipmargin);    Setlayoutparams (params); }     Public int[] Getbadgemargin () {framelayout.layoutparams params=(Layoutparams) getlayoutparams (); return New int[] {params.leftmargin, params.topmargin, Params.rightmargin, params.bottommargin}; }     Public voidIncrementbadgecount (intincrement) {Integer Count=Getbadgecount (); if(Count = =NULL) {setbadgecount (increment); } Else{setbadgecount (increment+count); }    }     Public voidDecrementbadgecount (intdecrement) {Incrementbadgecount (-decrement); }    /** Attach The Badgeview to the tabwidget * * @param target the tabwidget to Attach the Badgeview * * @param tabIndex Index of the tab*/     Public voidSettargetview (Tabwidget Target,intTabIndex) {View Tabview=Target.getchildtabviewat (TabIndex);    Settargetview (Tabview); }    /** Attach The Badgeview to the target view * * @param target , the view to Attach the Badgeview*/     Public voidSettargetview (View target) {if(GetParent ()! =NULL{(ViewGroup) getParent ()). Removeview ( This); }        if(target = =NULL) {            return; }        if(Target.getparent ()instanceofframelayout) {((framelayout) target.getparent ()). AddView ( This); } Else if(Target.getparent ()instanceofViewGroup) {            //Use a new framelayout container for adding badgeViewGroup Parentcontainer =(ViewGroup) target.getparent (); intGroupindex =Parentcontainer.indexofchild (target);            Parentcontainer.removeview (target); Framelayout Badgecontainer=Newframelayout (GetContext ()); Viewgroup.layoutparams Parentlayoutparams=Target.getlayoutparams ();            Parentcontainer.addview (Badgecontainer, Groupindex, parentlayoutparams);            Badgecontainer.addview (target); Badgecontainer.addview ( This); } Else if(target.getparent () = =NULL) {LOG.E (GetClass (). Getsimplename (),"Parentview is needed"); }    }    /** Converts dip to PX*/    Private intDIP2PX (floatdip) {        return(int) (DIP * getcontext (). Getresources (). Getdisplaymetrics (). Density + 0.5f); }}

The specific implementation code is as follows:

  Private void Initbadgeview (Context context) {        new  Badgeview (context);        Badgeview.setbackgroundresource (R.DRAWABLE.SUBSCRIPTION_NUM_BG);        Badgeview.setincludefontpadding (false);        Badgeview.setgravity (gravity.center);        Badgeview.settextsize (8f);        Badgeview.settextcolor (color.white);         // set Prompt information        Badgeview.setbadgecount (+);         // add attached object view         Badgeview.settargetview (IMGIV);            }

Badgeview new prompt open Source Tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.