{"id":944,"date":"2023-03-11T18:52:37","date_gmt":"2023-03-11T10:52:37","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=944"},"modified":"2023-04-29T15:40:39","modified_gmt":"2023-04-29T07:40:39","slug":"android-shadow-border-gradient-effect-based-on-maskfilter","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-shadow-border-gradient-effect-based-on-maskfilter\/","title":{"rendered":"Android\u57fa\u4e8e\u6a21\u7cca\u6ee4\u955c(MaskFilter)\u7684\u9634\u5f71\u8fb9\u6846\u6e10\u53d8\u6548\u679c"},"content":{"rendered":"<p>setMaskFilter(MaskFilter maskfilter)\uff1a\u8bbe\u7f6eMaskFilter\uff0c\u53ef\u4ee5\u7528\u4e0d\u540c\u7684MaskFilter\u5b9e\u73b0\u6ee4\u955c\u7684\u6548\u679c\uff0c\u5982\u6ee4\u5316\uff0c\u7acb\u4f53\u7b49\uff01 <\/p>\n<p>\u800c\u6211\u4eec\u4e00\u822c\u4e0d\u4f1a\u76f4\u63a5\u53bb\u7528\u8fd9\u4e2aMaskFilter\uff0c\u800c\u662f\u4f7f\u7528\u5b83\u7684\u4e24\u4e2a\u5b50\u7c7b\uff1a<\/p>\n<ul>\n<li>BlurMaskFilter\uff1a\u6307\u5b9a\u4e00\u4e2a\u6a21\u7cca\u7684\u6837\u5f0f\u548c\u534a\u5f84\u6765\u5904\u7406Paint\u7684\u8fb9\u7f18<\/li>\n<li>EmbossMaskFilter\uff1a\u6307\u5b9a\u5149\u6e90\u7684\u65b9\u5411\u548c\u73af\u5883\u5149\u5f3a\u5ea6\u6765\u6dfb\u52a0\u6d6e\u96d5\u6548\u679c<\/li>\n<\/ul>\n<h2>BlurMaskFilter(\u6a21\u7cca\u6548\u679c)<\/h2>\n<p>\u6784\u9020\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">BlurMaskFilter(float radius, Blur style)<\/code><\/pre>\n<p><!-- more --><\/p>\n<ul>\n<li>\u53c2\u65701\uff1a\u6307\u5b9a\u6a21\u7cca\u8fb9\u7f18\u7684\u534a\u5f84<\/li>\n<li>\u53c2\u65702\uff1a\u6307\u5b9a\u6a21\u7cca\u7684\u98ce\u683c\uff0c\u53ef\u9009\u503c\u6709<\/li>\n<li>\n<ul>\n<li>BlurMaskFilter.Blur.NORMAL\uff1a\u5185\u5916\u6a21\u7cca<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>BlurMaskFilter.Blur.OUTER\uff1a\u5916\u90e8\u6a21\u7cca<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>BlurMaskFilter.Blur.INNER\uff1a\u5185\u90e8\u6a21\u7cca<\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li>BlurMaskFilter.Blur.SOLID\uff1a\u5185\u90e8\u52a0\u7c97\uff0c\u5916\u90e8\u6a21\u7cca<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>\u9634\u5f71\u6e10\u53d8\u8fb9\u6846<\/h2>\n<pre><code class=\"language-java\">public class ShadowBorderLayout extends ConstraintLayout {\n    private static final String TAG = &quot;yezhou&quot;;\n    private BlurMaskFilter mBlurMaskFilter;\n    private Paint mPaint;\n    private float mShadowWidth;\n\n    public ShadowBorderLayout(Context context) {\n        this(context, null);\n    }\n\n    public ShadowBorderLayout(Context context, AttributeSet attrs) {\n        this(context, attrs, 0);\n    }\n\n    public ShadowBorderLayout(Context context, AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n        init();\n    }\n\n    private void init() {\n        mShadowWidth = DisplayUtil.dp2px(4);\n\n        mPaint = new Paint();\n        mPaint.setAntiAlias(true);                                  \/\/\u6297\u952f\u9f7f\n        mPaint.setColor(Color.parseColor(&quot;#00A0E9&quot;));  \/\/\u753b\u7b14\u989c\u8272\n        mPaint.setStyle(Paint.Style.FILL);                          \/\/\u753b\u7b14\u98ce\u683c\n        mPaint.setStrokeWidth(5);                                   \/\/\u753b\u7b14\u7c97\u7ec6\n\n        mBlurMaskFilter = new BlurMaskFilter(10f, BlurMaskFilter.Blur.OUTER);\n        mPaint.setMaskFilter(mBlurMaskFilter);\n\n        setLayerType(View.LAYER_TYPE_SOFTWARE, null);     \/\/\u5173\u95ed\u786c\u4ef6\u52a0\u901f\n    }\n\n    @Override\n    protected void onDraw(Canvas canvas) {\n        super.onDraw(canvas);\n    }\n\n    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)\n    @Override\n    protected void dispatchDraw(Canvas canvas) {\n        super.dispatchDraw(canvas);\n        Log.i(TAG, &quot;left: &quot; + getLeft() + &quot;, top: &quot; + getTop() + &quot;, right: &quot; + getRight() + &quot;, bottom: &quot; + getBottom());\n        Log.i(TAG, &quot;width: &quot; + getWidth() + &quot;, height: &quot; + getHeight());\n        canvas.drawRect(mShadowWidth, mShadowWidth, getWidth()-mShadowWidth, getHeight()-mShadowWidth, mPaint);\n    }\n\n}<\/code><\/pre>\n<h2>\u5706\u89d2\u9634\u5f71\u6e10\u53d8\u8fb9\u6846<\/h2>\n<pre><code class=\"language-java\">public class ShadowRoundBorderLayout extends ConstraintLayout {\n    private static final String TAG = &quot;yezhou&quot;;\n    private BlurMaskFilter mBlurMaskFilter;\n    private Paint mPaint;\n    private float mRadius;\n\n    public ShadowRoundBorderLayout(Context context) {\n        this(context, null);\n    }\n\n    public ShadowRoundBorderLayout(Context context, AttributeSet attrs) {\n        this(context, attrs, 0);\n    }\n\n    public ShadowRoundBorderLayout(Context context, AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n        init();\n    }\n\n    private void init() {\n        mRadius = DisplayUtil.dp2px(6);\n\n        mPaint = new Paint();\n        mPaint.setAntiAlias(true);                     \/\/\u6297\u952f\u9f7f\n        mPaint.setColor(Color.parseColor(&quot;#00A0E9&quot;));  \/\/\u753b\u7b14\u989c\u8272\n        mPaint.setStyle(Paint.Style.STROKE);           \/\/\u753b\u7b14\u98ce\u683c\n        mPaint.setStrokeWidth(5);                      \/\/\u753b\u7b14\u7c97\u7ec6\n\n        mBlurMaskFilter = new BlurMaskFilter(10f, BlurMaskFilter.Blur.OUTER);\n        mPaint.setMaskFilter(mBlurMaskFilter);\n\n        setLayerType(View.LAYER_TYPE_SOFTWARE, null);     \/\/\u5173\u95ed\u786c\u4ef6\u52a0\u901f\n    }\n\n    @Override\n    protected void onDraw(Canvas canvas) {\n        super.onDraw(canvas);\n    }\n\n    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)\n    @Override\n    protected void dispatchDraw(Canvas canvas) {\n        super.dispatchDraw(canvas);\n        Log.i(TAG, &quot;left: &quot; + getLeft() + &quot;, top: &quot; + getTop() + &quot;, right: &quot; + getRight() + &quot;, bottom: &quot; + getBottom());\n        Log.i(TAG, &quot;width: &quot; + getWidth() + &quot;, height: &quot; + getHeight());\n        canvas.drawRoundRect(0, 0, getWidth(), getHeight(), mRadius, mRadius, mPaint);\n    }\n\n}<\/code><\/pre>\n<h2>\u5f15\u7528\u81ea\u5b9a\u4e49\u5e03\u5c40<\/h2>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;android.support.constraint.ConstraintLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    xmlns:app=&quot;http:\/\/schemas.android.com\/apk\/res-auto&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;\n    &gt;\n\n    &lt;android.support.v7.widget.Toolbar\n        android:id=&quot;@+id\/toolbar&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;?android:attr\/actionBarSize&quot;\n        android:background=&quot;@color\/colorPrimaryDark&quot;\n        app:title=&quot;\u57fa\u4e8e\u6a21\u7cca\u6ee4\u955c\u7684\u9634\u5f71\u6e10\u53d8\u8fb9\u6846\u6548\u679c&quot;\n        app:titleTextColor=&quot;#FFF&quot;\n        &gt;\n    &lt;\/android.support.v7.widget.Toolbar&gt;\n\n    &lt;me.yezhou.widget.ShadowBorderLayout\n        android:id=&quot;@+id\/layout_shadow_border&quot;\n        android:layout_width=&quot;match_parent&quot;\n        app:layout_constraintTop_toBottomOf=&quot;@id\/toolbar&quot;\n        android:layout_height=&quot;160dp&quot;\n        android:layout_margin=&quot;15dp&quot;\n        &gt;\n        &lt;TextView\n            android:layout_width=&quot;wrap_content&quot;\n            android:layout_height=&quot;wrap_content&quot;\n            app:layout_constraintLeft_toLeftOf=&quot;parent&quot;\n            app:layout_constraintRight_toRightOf=&quot;parent&quot;\n            app:layout_constraintTop_toTopOf=&quot;parent&quot;\n            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;\n            android:text=&quot;\u9634\u5f71\u6e10\u53d8\u8fb9\u6846&quot;\n            android:textSize=&quot;20sp&quot;\n            \/&gt;\n    &lt;\/me.yezhou.widget.ShadowBorderLayout&gt;\n\n    &lt;me.yezhou.widget.ShadowRoundBorderLayout\n        android:layout_width=&quot;match_parent&quot;\n        app:layout_constraintTop_toBottomOf=&quot;@id\/layout_shadow_border&quot;\n        android:layout_height=&quot;160dp&quot;\n        android:layout_margin=&quot;15dp&quot;\n        &gt;\n        &lt;TextView\n            android:layout_width=&quot;wrap_content&quot;\n            android:layout_height=&quot;wrap_content&quot;\n            app:layout_constraintLeft_toLeftOf=&quot;parent&quot;\n            app:layout_constraintRight_toRightOf=&quot;parent&quot;\n            app:layout_constraintTop_toTopOf=&quot;parent&quot;\n            app:layout_constraintBottom_toBottomOf=&quot;parent&quot;\n            android:text=&quot;\u5706\u89d2\u9634\u5f71\u6e10\u53d8\u8fb9\u6846&quot;\n            android:textSize=&quot;20sp&quot;\n            \/&gt;\n    &lt;\/me.yezhou.widget.ShadowRoundBorderLayout&gt;\n\n&lt;\/android.support.constraint.ConstraintLayout&gt;<\/code><\/pre>\n<h2>\u6548\u679c\u6f14\u793a<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/ShadowBorderLayout.png\" alt=\"ShadowBorderLayout\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>setMaskFilter(MaskFilter maskfilter)\uff1a\u8bbe\u7f6eMaskFilter\uff0c\u53ef\u4ee5\u7528\u4e0d\u540c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-944","post","type-post","status-publish","format-standard","hentry","category-android-ui"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=944"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/944\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}