Permalink
Browse files

allow margins to be set independently, center position option,

added subtle transparency to default badge colour
  • Loading branch information...
1 parent 2966920 commit 570607746caaa07b97d3195818ce7f80a75976e6 @jgilfelt committed Oct 17, 2011
View
Binary file not shown.
@@ -33,12 +33,13 @@
public static final int POSITION_TOP_RIGHT = 2;
public static final int POSITION_BOTTOM_LEFT = 3;
public static final int POSITION_BOTTOM_RIGHT = 4;
+ public static final int POSITION_CENTER = 5;
private static final int DEFAULT_MARGIN_DIP = 5;
private static final int DEFAULT_LR_PADDING_DIP = 5;
private static final int DEFAULT_CORNER_RADIUS_DIP = 8;
private static final int DEFAULT_POSITION = POSITION_TOP_RIGHT;
- private static final int DEFAULT_BADGE_COLOR = Color.RED;
+ private static final int DEFAULT_BADGE_COLOR = Color.parseColor("#CCFF0000"); //Color.RED;
private static final int DEFAULT_TEXT_COLOR = Color.WHITE;
private static Animation fadeIn;
@@ -48,7 +49,8 @@
private View target;
private int badgePosition;
- private int badgeMargin;
+ private int badgeMarginH;
+ private int badgeMarginV;
private int badgeColor;
private boolean isShown;
@@ -108,7 +110,8 @@ private void init(Context context, View target, int tabIndex) {
// apply defaults
badgePosition = DEFAULT_POSITION;
- badgeMargin = dipToPixels(DEFAULT_MARGIN_DIP);
+ badgeMarginH = dipToPixels(DEFAULT_MARGIN_DIP);
+ badgeMarginV = badgeMarginH;
badgeColor = DEFAULT_BADGE_COLOR;
setTypeface(Typeface.DEFAULT_BOLD);
@@ -336,19 +339,23 @@ private void applyLayoutParams() {
switch (badgePosition) {
case POSITION_TOP_LEFT:
lp.gravity = Gravity.LEFT | Gravity.TOP;
- lp.setMargins(badgeMargin, badgeMargin, 0, 0);
+ lp.setMargins(badgeMarginH, badgeMarginV, 0, 0);
break;
case POSITION_TOP_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.TOP;
- lp.setMargins(0, badgeMargin, badgeMargin, 0);
+ lp.setMargins(0, badgeMarginV, badgeMarginH, 0);
break;
case POSITION_BOTTOM_LEFT:
lp.gravity = Gravity.LEFT | Gravity.BOTTOM;
- lp.setMargins(badgeMargin, 0, 0, badgeMargin);
+ lp.setMargins(badgeMarginH, 0, 0, badgeMarginV);
break;
case POSITION_BOTTOM_RIGHT:
lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;
- lp.setMargins(0, 0, badgeMargin, badgeMargin);
+ lp.setMargins(0, 0, badgeMarginH, badgeMarginV);
+ break;
+ case POSITION_CENTER:
+ lp.gravity = Gravity.CENTER;
+ lp.setMargins(0, 0, 0, 0);
break;
default:
break;
@@ -378,7 +385,7 @@ public boolean isShown() {
/**
* Returns the positioning of this badge.
*
- * one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT.
+ * one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT, POSTION_CENTER.
*
*/
public int getBadgePosition() {
@@ -388,19 +395,27 @@ public int getBadgePosition() {
/**
* Set the positioning of this badge.
*
- * @param layoutPosition one of POSITION_TOP_LEFT, POSITION_TOP_RIGHT, POSITION_BOTTOM_LEFT, POSITION_BOTTOM_RIGHT.
+ * @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/vertical margin from the target View that is applied to this badge.
+ * 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 getBadgeMargin() {
- return badgeMargin;
+ public int getVerticalBadgeMargin() {
+ return badgeMarginV;
}
/**
@@ -409,7 +424,19 @@ public int getBadgeMargin() {
* @param badgeMargin the margin in pixels.
*/
public void setBadgeMargin(int badgeMargin) {
- this.badgeMargin = 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;
}
/**
@@ -76,7 +76,7 @@ public void onCreate(Bundle savedInstanceState) {
btnPosition = (Button) findViewById(R.id.position_target);
badge1 = new BadgeView(this, btnPosition);
badge1.setText("12");
- badge1.setBadgePosition(BadgeView.POSITION_BOTTOM_LEFT);
+ badge1.setBadgePosition(BadgeView.POSITION_CENTER);
btnPosition.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
@@ -117,7 +117,7 @@ public void onClick(View v) {
badge4 = new BadgeView(this, btnAnim2);
badge4.setText("123");
badge4.setBadgePosition(BadgeView.POSITION_TOP_LEFT);
- badge4.setBadgeMargin(15);
+ badge4.setBadgeMargin(15, 10);
badge4.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
btnAnim2.setOnClickListener(new OnClickListener() {
@Override
@@ -168,7 +168,7 @@ public void onClick(View v) {
TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);
btnTab = (Button) findViewById(R.id.tab_btn);
- badge7 = new BadgeView(this, tabs, 2);
+ badge7 = new BadgeView(this, tabs, 0);
badge7.setText("5");
btnTab.setOnClickListener(new OnClickListener() {
@Override

0 comments on commit 5706077

Please sign in to comment.