Created
June 4, 2015 14:00
-
-
Save wuyexiong/95213d94a78cf704e548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xiaomi.shop2.widget; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.GradientDrawable; | |
import android.graphics.drawable.ShapeDrawable; | |
import android.graphics.drawable.shapes.OvalShape; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.TextView; | |
public class BadgeView extends TextView { | |
private int mBadgeColor = Color.parseColor("#fe672c"); | |
private static final int DEFAULT_CORNER_RADIUS_DIP = 90; | |
private float mCounterHeight = 17; // dp | |
public BadgeView(Context context) { | |
super(context); | |
} | |
public BadgeView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public BadgeView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
@Override | |
public void setText(CharSequence text, BufferType type) { | |
super.setText(text, type); | |
} | |
private Drawable getDefaultBackground() { | |
GradientDrawable gd = new GradientDrawable(); | |
gd.setColor(mBadgeColor); | |
gd.setCornerRadius(DEFAULT_CORNER_RADIUS_DIP); | |
return gd; | |
} | |
private Drawable getOvalDrawable() { | |
OvalShape oval = new OvalShape(); | |
oval.resize(DensityUtil.dip2px(mCounterHeight), DensityUtil.dip2px(mCounterHeight)); | |
ShapeDrawable drawable = new ShapeDrawable(oval); | |
drawable.getPaint().setColor(mBadgeColor); | |
drawable.setIntrinsicHeight(DensityUtil.dip2px(mCounterHeight)); | |
drawable.setIntrinsicWidth(DensityUtil.dip2px(mCounterHeight)); | |
return drawable; | |
} | |
public void setCount(int count) { | |
if (count > 0) { | |
if (count < 10) { | |
setBackgroundDrawable(getOvalDrawable()); | |
setText(String.valueOf(count)); | |
} else if (count > 99) { | |
setBackgroundDrawable(getDefaultBackground()); | |
setText("99+"); | |
} else { | |
setBackgroundDrawable(getDefaultBackground()); | |
setText(String.valueOf(count)); | |
} | |
this.setVisibility(View.VISIBLE); | |
} else { | |
this.setVisibility(View.GONE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment