目录结构:

contents structure [+]

MaskFilter可以用来指定画笔的边缘效果。如果引用开启硬件加速的话,那么MaskFilter将不会起作用。
关闭硬件加速:

  1. android:hardwareAccelerated="false"

Android中有两个已知的MaskFilter实现类,分别是:BlurMaskFilter和EmbossMaskFilter:
BlurMaskFilter:指定模糊样式和影响半径。
EmbossMaskFilter:指定浮雕的光源方向和周围光强度。

在实际中,使用不同的方法可能会有不同的硬件加速情况,比如笔者测试发现drawText默认是关闭硬件加速的,drawRect默认是开启硬件加速的。除了在Application.xml文件中指定硬件加速的开关情况,也可以通过代码来实现:

  1. //不使用硬件加速
  2. myview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  3. //...
  4. //使用硬件加速
  5. myview.setLayerType(View.LAYER_TYPE_HARDWARE,null);

1.EmbossMaskFilter

EmbossMaskFilter用于完成浮雕效果,通过PS可以更简单的完成类似的效果。EmbossMaskFilter的唯一构造方法是:

  1. public EmbossMaskFilter (float[] direction,float ambient,float specular,float blurRadius)

这个构造方法在API 28(Android 9.0)中已经被废弃了。

在指定参数的时候需要指定光源方向(direction)、环境光强度(ambient)、镜面反射系数(specular)和模糊半径(blurRadius)。

  1. float[] direction=new float[]{1,1,1};//指定光源方向
  2. float light=0.4f;//指定环境光强度(0~1),0~1表示环境从暗到亮
  3. float specular=6f;//指定镜面反射系数,越接近0,反射光越强。
  4. float blur=3f;//指定模糊半径,值越大,越清晰。
  5.  
  6. EmbossMaskFilter emboss=new EmbossMaskFilter(direction,light,specular,blur);
  7. if(!canvas.isHardwareAccelerated()){//在未硬件加速的情况下设置效果
  8. myPaint.setMaskFilter(emboss);
  9. }

如下代码:

  1. protected void onDraw(Canvas canvas) {
  2. super.onDraw(canvas);
  3. Paint paint=new Paint();
  4. paint.setColor(Color.RED);
  5. paint.setStyle(Style.FILL);
  6. paint.setTextSize(70);
  7.  
  8. float[] direction=new float[]{1,1,1};//指定光源方向
  9. float light=0.3f;//指定环境光强度
  10. float specular=5;//指定镜面反射强度
  11. float blur=5f;//指定模糊程度
  12.  
  13. EmbossMaskFilter emboss=new EmbossMaskFilter(direction,light,specular,blur);
  14.  
  15. if(!canvas.isHardwareAccelerated()){//如果没有开启硬件加速,就设置浮雕效果
  16. paint.setMaskFilter(emboss);
  17. }
  18. canvas.drawText("test测试", 200,200, paint);//绘制文本
  19. }

效果图:

通过改变为不同的参数,可以得到不同的效果。

2.BlurMaskFilter

BlurMaskFilter有一个构造方法如下:

  1. BlurMaskFilter(float radius, BlurMaskFilter.Blur style)

在构建BlurMaskFilter时,需要传入BlurMaskFilter.Blur枚举值,该枚举值有如下4种:

  1. BlurMaskFilter.Blur.INNER 在边界内模糊,边界外不模糊
  2. BlurMaskFilter.Blur.NORMAL 在边界内和边界外都模糊
  3. BlurMaskFilter.Blur.OUTER 在边界外模糊,边界内不模糊
  4. BlurMaskFilter.Blur.SOLID 在边边界内使用solid边框,边界外模糊

下面是使用示例:
activity_main.xml

  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/rl"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:padding="16dp"
  8. tools:context=".MainActivity"
  9. android:background="#ffffff"
  10. >
  11. <TextView
  12. android:id="@+id/tv"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_centerInParent="true"
  16. android:text="ANDROID"
  17. android:textSize="100dp"
  18. android:textStyle="bold"
  19. android:textColor="#ff0000"
  20. android:gravity="center"
  21. />
  22. <RadioGroup
  23. android:id="@+id/rg"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:orientation="horizontal"
  27. >
  28. <RadioButton
  29. android:id="@+id/rb_none"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="No blur"
  33. />
  34. <RadioButton
  35. android:id="@+id/rb_inner"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="Inner blur"
  39. />
  40. <RadioButton
  41. android:id="@+id/rb_normal"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="Normal blur"
  45. />
  46. <RadioButton
  47. android:id="@+id/rb_outer"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:text="Outer blur"
  51. />
  52. <RadioButton
  53. android:id="@+id/rb_solid"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:text="Solid blur"
  57. />
  58. </RadioGroup>
  59. </RelativeLayout>

MainActivity.jave

  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.content.pm.ActivityInfo;
  4. import android.content.res.Resources;
  5. import android.graphics.BlurMaskFilter;
  6. import android.graphics.Paint;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.Window;
  10. import android.widget.RadioGroup;
  11. import android.widget.RelativeLayout;
  12. import android.widget.TextView;
  13.  
  14. public class MainActivity extends Activity {
  15. Context mContext=null;
  16. Resources mResources=null;
  17. RelativeLayout mRelativeLayout=null;
  18. TextView mTextView=null;
  19. RadioGroup mRadioGroup=null;
  20.  
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置横屏
  25.  
  26. mContext = getApplicationContext();
  27.  
  28. mResources = getResources();
  29.  
  30. mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
  31. mTextView = (TextView) findViewById(R.id.tv);
  32. mRadioGroup = (RadioGroup) findViewById(R.id.rg);
  33.  
  34. // Set a checked change listener for RadioGroup
  35. mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  36. @Override
  37. public void onCheckedChanged(RadioGroup radioGroup, int i) {
  38. if (i == R.id.rb_none) {
  39. // If no blur is checked
  40. // Set the TextView layer type
  41. mTextView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
  42. // Clear any previous MaskFilter
  43. mTextView.getPaint().setMaskFilter(null);
  44. }
  45. if(i == R.id.rb_inner){
  46. // If inner blur checked
  47. applyBlurMaskFilter(mTextView, BlurMaskFilter.Blur.INNER);
  48. }
  49. if(i == R.id.rb_normal){
  50. // If normal blur checked
  51. applyBlurMaskFilter(mTextView, BlurMaskFilter.Blur.NORMAL);
  52. }
  53. if(i == R.id.rb_outer){
  54. // If outer blur checked
  55. applyBlurMaskFilter(mTextView, BlurMaskFilter.Blur.OUTER);
  56. }
  57. if(i == R.id.rb_solid){
  58. // If solid blur checked
  59. applyBlurMaskFilter(mTextView, BlurMaskFilter.Blur.SOLID);
  60. }
  61. }
  62. });
  63. }
  64.  
  65. // Custom method to apply BlurMaskFilter to a TextView text
  66. protected void applyBlurMaskFilter(TextView tv, BlurMaskFilter.Blur style){
  67.  
  68. // Define the blur effect radius
  69. float radius = tv.getTextSize()/10;
  70.  
  71. // Initialize a new BlurMaskFilter instance
  72. BlurMaskFilter filter = new BlurMaskFilter(radius,style);
  73.  
  74. /*
  75. public void setLayerType (int layerType, Paint paint)
  76. Specifies the type of layer backing this view. The layer can be LAYER_TYPE_NONE,
  77. LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE.
  78.  
  79. A layer is associated with an optional Paint instance that controls how the
  80. layer is composed on screen.
  81.  
  82. Parameters
  83. layerType : The type of layer to use with this view, must be one of
  84. LAYER_TYPE_NONE, LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE
  85. paint : The paint used to compose the layer. This argument is optional and can be null. It is ignored when the layer type is LAYER_TYPE_NONE
  86. */
  87. /*
  88. public static final int LAYER_TYPE_SOFTWARE
  89. Indicates that the view has a software layer. A software layer is backed by
  90. a bitmap and causes the view to be rendered using Android's software rendering
  91. pipeline, even if hardware acceleration is enabled.
  92. */
  93.  
  94. // Set the TextView layer type
  95. tv.setLayerType(View.LAYER_TYPE_SOFTWARE, new Paint());//取消硬件加速
  96.  
  97. tv.getPaint().setMaskFilter(filter);
  98. }
  99. }

效果图:

参考文章:

How to use BlurMaskFilter In Android

【Android】解析Paint类中MaskFilter的使用的更多相关文章

  1. 【Android】解析Paint类中Xfermode的使用

    Paint类提供了setXfermode(Xfermode xfermode)方法,Xfermode指明了原图像和目标图像的结合方式.谈到Xfermode就不得不谈它的派生类PorterDuffXfe ...

  2. 解析C#类中的构造函数

    <解析C#类中的构造函数> 一.  C#中的构造函数概述: C#中类包含数据成员和函数成员.函数成员提供了操作类中数据的某些功能,包括方法.属性.构造器和终结器.运算符和索引器. 构造函数 ...

  3. Android Studio查看类中所有方法和属性

    ctrl+f3效果: alt+7效果: 注意区别:虽然所有方法都有,但是顺序自己一看效果便知.一个是根据类中的顺序,另一个是根据a-z的开头字母顺序. 百度查了一下快捷键是ctrl+f12.但是自己试 ...

  4. Android 编程 AMapLocationClientOption 类中的 setNeedAddress 方法用处 (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)

    最近在用高德地图来写Android App, 其中有一些 方法是不太理解的,这里写一下 对  高德地图  com.amap.api.location.AMapLocationClientOption ...

  5. Gson解析POJO类中的泛型参数

    在开发Android与API交互的时候,使用Json格式传输,遇到了这样一个情况,返回数据格式POJO类如下: public class ApiResult<T> { private in ...

  6. Android 编程 AMapLocationClientOption 类中的 setMockEnable (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)

    setMockEnable 高德地图中 AMapLocationClientOption 中有一个方法是设置APP是否接受模拟定位的设置,就是方法 setMockEnable //设置是否允许模拟位置 ...

  7. Spring5源码解析6-ConfigurationClassParser 解析配置类

    ConfigurationClassParser 在ConfigurationClassPostProcessor#processConfigBeanDefinitions方法中创建了Configur ...

  8. 你知道Spring是怎么解析配置类的吗?

    彻底读懂Spring(二)你知道Spring是怎么解析配置类的吗? 推荐阅读: Spring官网阅读系列 彻底读懂Spring(一)读源码,我们可以从第一行读起 Spring执行流程图如下: 如果图片 ...

  9. Android ---paint类

    引自:http://www.cnblogs.com/-OYK/archive/2011/10/25/2223624.html Android Paint和Color类   要绘图,首先得调整画笔,待画 ...

随机推荐

  1. J 判断二叉树每个结点的权值是否关于根节点完全对称

    如果二叉树每个结点的权值关于根节点完全对称 就输出Yes Sample Input 27 //结点1 2 3 //结点1的左孩子是结点2 右孩子是结点32 4 53 6 74 0 05 0 06 0 ...

  2. Maven多模块项目

    1.项目结构-父项目 其中parent是父项目,这个父项目的父项目是springboot,我搭建这个多模块的项目的目的主要是为了研究学习springbatch 父项目的pom文件内容: <pro ...

  3. thinkphp自定义分页类

    先来看下这个分页的样式,没写css,确实丑 什么时候写样式再来上传下css吧...... 就是多一个页面跳转功能 先把这个代码贴一下 <?php namespace Component; cla ...

  4. 用HTML+CSS画出一个同心圆

    参加web前端校招的同学们经常会遇到这样的面试题:用HTML+CSS画出一个同心圆. 例如: 这道题主要考验的是基础盒模型布局能力和倒圆角属性的巧用. 1.html代码 <body> &l ...

  5. Chameleon

    # -*- coding: utf-8 -*- """ Created on Tue Dec 18 09:55:16 2018 @author: Mark,LI &quo ...

  6. 无状态shiro认证组件(禁用默认session)

    准备内容 简单的shiro无状态认证 无状态认证拦截器 import com.hjzgg.stateless.shiroSimpleWeb.Constants; import com.hjzgg.st ...

  7. 本地搭建ELK(elasticsearch, logstash, kibana)日志收集系统

    环境准备:macos 预先安装brew包管理器 1.安装elasticsearch流程 那么,咱们先去安装java8 接着,咱们继续按照elasticsearch 接着,咱们启动elasticsear ...

  8. 安装Git 创建版本库

    安装git [root@node1 ~]# yum -y install git 创建用户 因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址 [root@node ...

  9. javascript中break与continue,及return的区别

    a).在循环体中, break是跳出整个循环,不执行以后的循环语句: continue是结束本次循环语句,进入下一个循环: b). 在if判断句,结束该函数的执行时,用 return: c). 在函数 ...

  10. python 安装pip setuptools

    注意操作前提一定要使用管理员方式运行 python目录要完全允许控制 windows7 下 0.先安装python2.7.13 32位:https://www.python.org/ftp/pytho ...