MaterialBadgeTextView
MaterialBadgeTextView copied to clipboard
关于增加描边功能的代码实现,请需要的自行拷贝!
首先将https://github.com/matrixxun/MaterialBadgeTextView/blob/master/MaterialBadgeTextView/library/src/main/java/com/matrixxun/starry/badgetextview/MaterialBadgeTextView.java 拷贝到自己工程,然后修改内部的自定义OvalShadow、SemiCircleRectDrawable即可。
代码如下:
private class OvalShadow extends OvalShape {
private RadialGradient mRadialGradient;
private Paint mShadowPaint;
private int mCircleDiameter;
public OvalShadow(int shadowRadius, int circleDiameter) {
super();
mShadowPaint = new Paint();
mShadowRadius = shadowRadius;
mCircleDiameter = circleDiameter;
mRadialGradient = new RadialGradient(mCircleDiameter / 2, mCircleDiameter / 2,
mShadowRadius, new int[]{
FILL_SHADOW_COLOR, Color.TRANSPARENT
}, null, Shader.TileMode.CLAMP);
mShadowPaint.setShader(mRadialGradient);
}
@Override
public void draw(Canvas canvas, Paint paint) {
final int viewWidth = BadgeTextView.this.getWidth();
final int viewHeight = BadgeTextView.this.getHeight();
paint.setColor(backgroundColor);
paint.setStyle(Paint.Style.FILL);
//canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2 + mShadowRadius), mShadowPaint);
canvas.drawCircle(viewWidth / 2, viewHeight / 2, (mCircleDiameter / 2), paint);
//圆描边
paint.setColor(borderColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(borderWidth);
paint.setAntiAlias(true);
canvas.drawCircle(viewWidth / 2 + 1, viewHeight / 2 + 1, (mCircleDiameter / 2), paint);
}
}
class SemiCircleRectDrawable extends Drawable {
private final Paint mPaint;
private RectF rectF;
public Paint getPaint() {
return mPaint;
}
public SemiCircleRectDrawable() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
if (rectF == null) {
rectF = new RectF(left + diffWH, top + mShadowRadius+4, right - diffWH, bottom - mShadowRadius-4);
} else {
rectF.set(left + diffWH, top + mShadowRadius+4, right - diffWH, bottom - mShadowRadius-4);
}
}
@Override
public void draw(Canvas canvas) {
float R = (float)(rectF.bottom * 0.4);
if (rectF.right < rectF.bottom) {
R = (float)(rectF.right * 0.4);
}
mPaint.setColor(backgroundColor);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawRoundRect(rectF, R, R, mPaint);
//圆角矩形描边
mPaint.setColor(borderColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(borderWidth);
mPaint.setAntiAlias(true);
canvas.drawRoundRect(rectF, R, R, mPaint);
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
mPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSPARENT;
}
}
xml中配置如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">
<com.xylife.common.widget.MaterialBadgeTextView
android:id="@+id/coupon_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:layout_marginRight="@dimen/dp_10"
android:background="#FF4433"
app:mbtv_border_color="@android:color/white"
app:mbtv_border_width="1dp"
android:textColor="@android:color/white"
/>
</LinearLayout>
@jdsjlzx you can make a pull request 👍
Hi jdsjlzx, Would you agree with I will merge your code to master branch? @jdsjlzx
描边时要判断下是否大于0:
//圆角矩形描边
if (borderWidth > 0) {
mPaint.setColor(borderColor);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(borderWidth);
mPaint.setAntiAlias(true);
canvas.drawRoundRect(rectF, R, R, mPaint);
}
@matrixxun ok,just do it