Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于增加描边功能的代码实现,请需要的自行拷贝! #15

Open
jdsjlzx opened this issue Jan 3, 2018 · 4 comments
Open

Comments

@jdsjlzx
Copy link

jdsjlzx commented Jan 3, 2018

首先将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>
@luongvo
Copy link

luongvo commented Jan 4, 2018

@jdsjlzx you can make a pull request 👍

@matrixxun
Copy link
Owner

Hi jdsjlzx, Would you agree with I will merge your code to master branch? @jdsjlzx

@jp1017
Copy link

jp1017 commented May 16, 2018

描边时要判断下是否大于0:

            //圆角矩形描边
            if (borderWidth > 0) {
                mPaint.setColor(borderColor);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeWidth(borderWidth);
                mPaint.setAntiAlias(true);
                canvas.drawRoundRect(rectF, R, R, mPaint);
            }

@jdsjlzx
Copy link
Author

jdsjlzx commented May 21, 2018

@matrixxun ok,just do it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants