android badgeView(右上角数字提醒)

来源:互联网 发布:实时金融数据发布网 编辑:程序博客网 时间:2024/03/29 14:30

我们在使用如短信类的软件是时会有未看短信数字的提醒,本人在论坛看到这个效果,就给大家分享一下:





最重要的是BadgeView这个重写TextView的类:

[java] view plaincopyprint?
  1. import android.content.Context;  
  2. import android.content.res.Resources;  
  3. import android.graphics.Color;  
  4. import android.graphics.Typeface;  
  5. import android.graphics.drawable.ShapeDrawable;  
  6. import android.graphics.drawable.shapes.RoundRectShape;  
  7. import android.util.AttributeSet;  
  8. import android.util.TypedValue;  
  9. import android.view.Gravity;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.view.ViewGroup.LayoutParams;  
  13. import android.view.ViewParent;  
  14. import android.view.animation.AccelerateInterpolator;  
  15. import android.view.animation.AlphaAnimation;  
  16. import android.view.animation.Animation;  
  17. import android.view.animation.DecelerateInterpolator;  
  18. import android.widget.FrameLayout;  
  19. import android.widget.TabWidget;  
  20. import android.widget.TextView;  
  21.   
  22. /** 
  23.  * A simple text label view that can be applied as a "badge" to any given {@link android.view.View}.  
  24.  * This class is intended to be instantiated at runtime rather than included in XML layouts. 
  25.  *  
  26.  * @author Jeff Gilfelt 
  27.  */  
  28. public class BadgeView extends TextView {  
  29.   
  30.     public static final int POSITION_TOP_LEFT = 1;  
  31.     public static final int POSITION_TOP_RIGHT = 2;  
  32.     public static final int POSITION_BOTTOM_LEFT = 3;  
  33.     public static final int POSITION_BOTTOM_RIGHT = 4;  
  34.       
  35.     private static final int DEFAULT_MARGIN_DIP = 5;  
  36.     private static final int DEFAULT_LR_PADDING_DIP = 5;  
  37.     private static final int DEFAULT_CORNER_RADIUS_DIP = 8;  
  38.     private static final int DEFAULT_POSITION = POSITION_TOP_RIGHT;  
  39.     private static final int DEFAULT_BADGE_COLOR = Color.RED;  
  40.     private static final int DEFAULT_TEXT_COLOR = Color.WHITE;  
  41.       
  42.     private static Animation fadeIn;  
  43.     private static Animation fadeOut;  
  44.       
  45.     private Context context;  
  46.     private View target;  
  47.       
  48.     private int badgePosition;  
  49.     private int badgeMargin;  
  50.     private int badgeColor;  
  51.       
  52.     private boolean isShown;  
  53.       
  54.     private ShapeDrawable badgeBg;  
  55.       
  56.     private int targetTabIndex;  
  57.       
  58.     public BadgeView(Context context) {  
  59.         this(context, (AttributeSet) null, android.R.attr.textViewStyle);  
  60.     }  
  61.       
  62.     public BadgeView(Context context, AttributeSet attrs) {  
  63.          this(context, attrs, android.R.attr.textViewStyle);  
  64.     }  
  65.       
  66.     /** 
  67.      * Constructor - 
  68.      *  
  69.      * create a new BadgeView instance attached to a target {@link android.view.View}. 
  70.      * 
  71.      * @param context context for this view. 
  72.      * @param target the View to attach the badge to. 
  73.      */  
  74.     public BadgeView(Context context, View target) {  
  75.          this(context, null, android.R.attr.textViewStyle, target, 0);  
  76.     }  
  77.       
  78.     /** 
  79.      * Constructor - 
  80.      *  
  81.      * create a new BadgeView instance attached to a target {@link android.widget.TabWidget} 
  82.      * tab at a given index. 
  83.      * 
  84.      * @param context context for this view. 
  85.      * @param target the TabWidget to attach the badge to. 
  86.      * @param index the position of the tab within the target. 
  87.      */  
  88.     public BadgeView(Context context, TabWidget target, int index) {  
  89.         this(context, null, android.R.attr.textViewStyle, target, index);  
  90.     }  
  91.       
  92.     public BadgeView(Context context, AttributeSet attrs, int defStyle) {  
  93.         this(context, attrs, defStyle, null0);  
  94.     }  
  95.       
  96.     public BadgeView(Context context, AttributeSet attrs, int defStyle, View target, int tabIndex) {  
  97.         super(context, attrs, defStyle);  
  98.         init(context, target, tabIndex);  
  99.     }  
  100.   
  101.     private void init(Context context, View target, int tabIndex) {  
  102.           
  103.         this.context = context;  
  104.         this.target = target;  
  105.         this.targetTabIndex = tabIndex;  
  106.           
  107.         // apply defaults  
  108.         badgePosition = DEFAULT_POSITION;  
  109.         badgeMargin = dipToPixels(DEFAULT_MARGIN_DIP);  
  110.         badgeColor = DEFAULT_BADGE_COLOR;  
  111.           
  112.         setTypeface(Typeface.DEFAULT_BOLD);  
  113.         int paddingPixels = dipToPixels(DEFAULT_LR_PADDING_DIP);  
  114.         setPadding(paddingPixels, 0, paddingPixels, 0);  
  115.         setTextColor(DEFAULT_TEXT_COLOR);  
  116.           
  117.         fadeIn = new AlphaAnimation(01);  
  118.         fadeIn.setInterpolator(new DecelerateInterpolator());  
  119.         fadeIn.setDuration(200);  
  120.   
  121.         fadeOut = new AlphaAnimation(10);  
  122.         fadeOut.setInterpolator(new AccelerateInterpolator());  
  123.         fadeOut.setDuration(200);  
  124.           
  125.         isShown = false;  
  126.           
  127.         if (this.target != null) {  
  128.             applyTo(this.target);  
  129.         } else {  
  130.             show();  
  131.         }  
  132.           
  133.     }  
  134.   
  135.     private void applyTo(View target) {  
  136.           
  137.         LayoutParams lp = target.getLayoutParams();  
  138.         ViewParent parent = target.getParent();  
  139.         FrameLayout container = new FrameLayout(context);  
  140.           
  141.         if (target instanceof TabWidget) {  
  142.               
  143.             // set target to the relevant tab child container  
  144.             target = ((TabWidget) target).getChildTabViewAt(targetTabIndex);  
  145.             this.target = target;  
  146.               
  147.             ((ViewGroup) target).addView(container,   
  148.                     new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  149.               
  150.             this.setVisibility(View.GONE);  
  151.             container.addView(this);  
  152.               
  153.         } else {  
  154.               
  155.             // TODO verify that parent is indeed a ViewGroup  
  156.             ViewGroup group = (ViewGroup) parent;   
  157.             int index = group.indexOfChild(target);  
  158.               
  159.             group.removeView(target);  
  160.             group.addView(container, index, lp);  
  161.               
  162.             container.addView(target);  
  163.       
  164.             this.setVisibility(View.GONE);  
  165.             container.addView(this);  
  166.               
  167.             group.invalidate();  
  168.               
  169.         }  
  170.           
  171.     }  
  172.       
  173.     /** 
  174.      * Make the badge visible in the UI. 
  175.      *  
  176.      */  
  177.     public void show() {  
  178.         show(falsenull);  
  179.     }  
  180.       
  181.     /** 
  182.      * Make the badge visible in the UI. 
  183.      * 
  184.      * @param animate flag to apply the default fade-in animation. 
  185.      */  
  186.     public void show(boolean animate) {  
  187.         show(animate, fadeIn);  
  188.     }  
  189.       
  190.     /** 
  191.      * Make the badge visible in the UI. 
  192.      * 
  193.      * @param anim Animation to apply to the view when made visible. 
  194.      */  
  195.     public void show(Animation anim) {  
  196.         show(true, anim);  
  197.     }  
  198.       
  199.     /** 
  200.      * Make the badge non-visible in the UI. 
  201.      *  
  202.      */  
  203.     public void hide() {  
  204.         hide(falsenull);  
  205.     }  
  206.       
  207.     /** 
  208.      * Make the badge non-visible in the UI. 
  209.      * 
  210.      * @param animate flag to apply the default fade-out animation. 
  211.      */  
  212.     public void hide(boolean animate) {  
  213.         hide(animate, fadeOut);  
  214.     }  
  215.       
  216.     /** 
  217.      * Make the badge non-visible in the UI. 
  218.      * 
  219.      * @param anim Animation to apply to the view when made non-visible. 
  220.      */  
  221.     public void hide(Animation anim) {  
  222.         hide(true, anim);  
  223.     }  
  224.       
  225.     /** 
  226.      * Toggle the badge visibility in the UI. 
  227.      *  
  228.      */  
  229.     public void toggle() {  
  230.         toggle(falsenullnull);  
  231.     }  
  232.       
  233.     /** 
  234.      * Toggle the badge visibility in the UI. 
  235.      *  
  236.      * @param animate flag to apply the default fade-in/out animation. 
  237.      */  
  238.     public void toggle(boolean animate) {  
  239.         toggle(animate, fadeIn, fadeOut);  
  240.     }  
  241.       
  242.     /** 
  243.      * Toggle the badge visibility in the UI. 
  244.      * 
  245.      * @param animIn Animation to apply to the view when made visible. 
  246.      * @param animOut Animation to apply to the view when made non-visible. 
  247.      */  
  248.     public void toggle(Animation animIn, Animation animOut) {  
  249.         toggle(true, animIn, animOut);  
  250.     }  
  251.       
  252.     private void show(boolean animate, Animation anim) {  
  253.         if (getBackground() == null) {  
  254.             if (badgeBg == null) {  
  255.                 badgeBg = getDefaultBackground();  
  256.             }  
  257.             setBackgroundDrawable(badgeBg);  
  258.         }  
  259.         applyLayoutParams();  
  260.           
  261.         if (animate) {  
  262.             this.startAnimation(anim);  
  263.         }  
  264.         this.setVisibility(View.VISIBLE);  
  265.         isShown = true;  
  266.     }  
  267.       
  268.     private void hide(boolean animate, Animation anim) {  
  269.         this.setVisibility(View.GONE);  
  270.         if (animate) {  
  271.             this.startAnimation(anim);  
  272.         }  
  273.         isShown = false;  
  274.     }  
  275.       
  276.     private void toggle(boolean animate, Animation animIn, Animation animOut) {  
  277.         if (isShown) {  
  278.             hide(animate && (animOut != null), animOut);      
  279.         } else {  
  280.             show(animate && (animIn != null), animIn);  
  281.         }  
  282.     }  
  283.       
  284.     /** 
  285.      * Increment the numeric badge label. If the current badge label cannot be converted to 
  286.      * an integer value, its label will be set to "0". 
  287.      *  
  288.      * @param offset the increment offset. 
  289.      */  
  290.     public int increment(int offset) {  
  291.         CharSequence txt = getText();  
  292.         int i;  
  293.         if (txt != null) {  
  294.             try {  
  295.                 i = Integer.parseInt(txt.toString());  
  296.             } catch (NumberFormatException e) {  
  297.                 i = 0;  
  298.             }  
  299.         } else {  
  300.             i = 0;  
  301.         }  
  302.         i = i + offset;  
  303.         setText(String.valueOf(i));  
  304.         return i;  
  305.     }  
  306.       
  307.     /** 
  308.      * Decrement the numeric badge label. If the current badge label cannot be converted to 
  309.      * an integer value, its label will be set to "0". 
  310.      *  
  311.      * @param offset the decrement offset. 
  312.      */  
  313.     public int decrement(int offset) {  
  314.         return increment(-offset);  
  315.     }  
  316.       
  317.     private ShapeDrawable getDefaultBackground() {  
  318.           
  319.         int r = dipToPixels(DEFAULT_CORNER_RADIUS_DIP);  
  320.         float[] outerR = new float[] {r, r, r, r, r, r, r, r};  
  321.           
  322.         RoundRectShape rr = new RoundRectShape(outerR, nullnull);  
  323.         ShapeDrawable drawable = new ShapeDrawable(rr);  
  324.         drawable.getPaint().setColor(badgeColor);  
  325.           
  326.         return drawable;  
  327.           
  328.     }  
  329.       
  330.     private void applyLayoutParams() {  
  331.           
  332.         FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  333.           
  334.         switch (badgePosition) {  
  335.         case POSITION_TOP_LEFT:  
  336.             lp.gravity = Gravity.LEFT | Gravity.TOP;  
  337.             lp.setMargins(badgeMargin, badgeMargin, 00);  
  338.             break;  
  339.         case POSITION_TOP_RIGHT:  
  340.             lp.gravity = Gravity.RIGHT | Gravity.TOP;  
  341.             lp.setMargins(0, badgeMargin, badgeMargin, 0);  
  342.             break;  
  343.         case POSITION_BOTTOM_LEFT:  
  344.             lp.gravity = Gravity.LEFT | Gravity.BOTTOM;  
  345.             lp.setMargins(badgeMargin, 00, badgeMargin);  
  346.             break;  
  347.         case POSITION_BOTTOM_RIGHT:  
  348.             lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;  
  349.             lp.setMargins(00, badgeMargin, badgeMargin);  
  350.             break;  
  351.         default:  
  352.             break;  
  353.         }  
  354.           
  355.         setLayoutParams(lp);  
  356.           
  357.     }  
  358.   
  359.     /** 
  360.      * Returns the target View this badge has been attached to. 
  361.      *  
  362.      */  
  363.     public View getTarget() {  
  364.         return target;  
  365.     }  
  366.   
  367.     /** 
  368.      * Is this badge currently visible in the UI? 
  369.      *  
  370.      */  
  371.     @Override  
  372.     public boolean isShown() {  
  373.         return isShown;  
  374.     }  
  375.   
  376.     /** 
  377.      * Returns the positioning of this badge. 
  378.      *  
  379.      * one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT. 
  380.      *  
  381.      */  
  382.     public int getBadgePosition() {  
  383.         return badgePosition;  
  384.     }  
  385.   
  386.     /** 
  387.      * Set the positioning of this badge. 
  388.      *  
  389.      * @param layoutPosition one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT. 
  390.      *  
  391.      */  
  392.     public void setBadgePosition(int layoutPosition) {  
  393.         this.badgePosition = layoutPosition;  
  394.     }  
  395.   
  396.     /** 
  397.      * Returns the horizontal/vertical margin from the target View that is applied to this badge. 
  398.      *  
  399.      */  
  400.     public int getBadgeMargin() {  
  401.         return badgeMargin;  
  402.     }  
  403.   
  404.     /** 
  405.      * Set the horizontal/vertical margin from the target View that is applied to this badge. 
  406.      *  
  407.      * @param badgeMargin the margin in pixels. 
  408.      */  
  409.     public void setBadgeMargin(int badgeMargin) {  
  410.         this.badgeMargin = badgeMargin;  
  411.     }  
  412.       
  413.     /** 
  414.      * Returns the color value of the badge background. 
  415.      *  
  416.      */  
  417.     public int getBadgeBackgroundColor() {  
  418.         return badgeColor;  
  419.     }  
  420.   
  421.     /** 
  422.      * Set the color value of the badge background. 
  423.      *  
  424.      * @param badgeColor the badge background color. 
  425.      */  
  426.     public void setBadgeBackgroundColor(int badgeColor) {  
  427.         this.badgeColor = badgeColor;  
  428.         badgeBg = getDefaultBackground();  
  429.     }  
  430.       
  431.     private int dipToPixels(int dip) {  
  432.         Resources r = getResources();  
  433.         float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());  
  434.         return (int) px;  
  435.     }  
  436.   
  437. }  


现在我们来使用它;

[java] view plaincopyprint?
  1. import android.app.Activity;  
  2. import android.graphics.Color;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.view.animation.BounceInterpolator;  
  7. import android.view.animation.TranslateAnimation;  
  8. import android.widget.Button;  
  9.   
  10. public class BadgerActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     Button  btn ;  
  13.     Button  btn1 ;  
  14.     BadgeView badge;  
  15.     BadgeView badge1;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btn =(Button) findViewById(R.id.btn);  
  21.         btn1 =(Button) findViewById(R.id.btn1);  
  22.           
  23.         badge = new BadgeView(this,btn);  
  24.         badge.setText("0");  
  25.         btn.setOnClickListener(new OnClickListener() {  
  26.               
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 // TODO Auto-generated method stub  
  30.               if(badge.isShown()){  
  31.                   badge.increment(1);  
  32.               }else{  
  33.                   badge.show();  
  34.               }  
  35.             }  
  36.         });  
  37.         badge1 = new BadgeView(this, btn1);  
  38.         badge1.setText("123");  
  39.         badge1.setBadgePosition(BadgeView.POSITION_TOP_LEFT);  
  40.         badge1.setBadgeMargin(15);  
  41.         badge1.setBadgeBackgroundColor(Color.parseColor("#A4C639"));  
  42.         btn1.setOnClickListener(new OnClickListener() {  
  43.             @Override  
  44.             public void onClick(View v) {  
  45.                 TranslateAnimation anim = new TranslateAnimation(-100000);  
  46.                 anim.setInterpolator(new BounceInterpolator());  
  47.                 anim.setDuration(1000);  
  48.                 badge1.toggle(anim, null);  
  49.             }  
  50.         });     
  51.           
  52.     }  
  53. }  


BadgeView原始项目上传csdn,http://download.csdn.net/detail/shaojie519/3861841