一般两种:BlurMaskFilter(模糊效果)和EmbossMaskFilter(浮雕效果),可以让图片更有立体感。
效果图如下:
activity_main.xml代码如下:
BlurMaskFilter.java的代码如下:
package com.deepreality.maskfilterdemo; import android.content.Context; import android.graphics.BlurMaskFilter; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; /** * 模糊效果View */ public class BlurMaskFilterView extends View { private Paint mPaint; public BlurMaskFilterView(Context context) { super(context); PaintInit(); } public BlurMaskFilterView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); PaintInit(); } public BlurMaskFilterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); PaintInit(); } private void PaintInit() { mPaint = new Paint(); mPaint.setAntiAlias(true); //抗锯齿 mPaint.setColor(Color.RED);//画笔颜色 mPaint.setStyle(Paint.Style.FILL); //画笔风格 mPaint.setTextSize(68); //绘制文字大小,单位px mPaint.setStrokeWidth(5); //画笔粗细 } @Override protected void onDraw(Canvas canvas) { /*//标准模式 BlurMaskFilter blurMaskFilter1 = new BlurMaskFilter(10f, BlurMaskFilter.Blur.NORMAL); mPaint.setMaskFilter(blurMaskFilter1); canvas.drawText("目前最喜欢的是Android开发!", 100, 100, mPaint);*/ /*//外模糊模式 BlurMaskFilter blurMaskFilter2 = new BlurMaskFilter(10f, BlurMaskFilter.Blur.OUTER); mPaint.setMaskFilter(blurMaskFilter2); canvas.drawText("目前最喜欢的是Android开发!", 100, 200, mPaint);*/ /*//内模糊模式 BlurMaskFilter blurMaskFilter3 = new BlurMaskFilter(10f, BlurMaskFilter.Blur.INNER); mPaint.setMaskFilter(blurMaskFilter3); canvas.drawText("目前最喜欢的是Android开发!", 100, 300, mPaint);*/ //内部加粗,外部模糊 BlurMaskFilter blurMaskFilter4 = new BlurMaskFilter(10f, BlurMaskFilter.Blur.SOLID); mPaint.setMaskFilter(blurMaskFilter4); canvas.drawText("目前最喜欢的是Android开发!", 100, 400, mPaint); //关闭硬件加速 setLayerType(View.LAYER_TYPE_SOFTWARE, null); } }
EmbossMaskFilter.java的代码如下:
package com.deepreality.maskfilterdemo; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.EmbossMaskFilter; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; /** * 浮雕效果View */ public class EmbossMaskFilterView extends View { public EmbossMaskFilterView(Context context) { super(context); } public EmbossMaskFilterView(Context context, AttributeSet attrs) { super(context, attrs); } public EmbossMaskFilterView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { float[] direction = new float[]{ 1, 1, 3 }; // 设置光源的方向 float light = 0.4f; //设置环境光亮度 float specular = 8; // 定义镜面反射系数 float blur = 3.0f; //模糊半径 EmbossMaskFilter emboss = new EmbossMaskFilter(direction,light,specular,blur); Paint paint = new Paint(); paint.setAntiAlias(true); //抗锯齿 paint.setColor(Color.BLUE);//画笔颜色 paint.setStyle(Paint.Style.FILL); //画笔风格 paint.setTextSize(70); //绘制文字大小,单位px paint.setStrokeWidth(8); //画笔粗细 paint.setMaskFilter(emboss); paint.setMaskFilter(emboss); canvas.drawText("这是一张漂亮妹子的图片", 50, 100, paint); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.meizi); canvas.drawBitmap(bitmap, 150, 200, paint); //关闭硬件加速 setLayerType(View.LAYER_TYPE_SOFTWARE, null); } }
在使用MaskFilter的时候要注意,当我们的targetSdkVersion >= 14的时候,MaskFilter 就不会起效果了,这是因为Android在API 14以上版本都是默认开启硬件加速的,这样充分 利用GPU的特性,使得绘画更加平滑,但是会多消耗一些内存!好吧,我们把硬件加速关了 就好,可以在不同级别下打开或者关闭硬件加速,一般是关闭~
- Application:在配置文件的application节点添加: android:hardwareAccelerated="true"
- Activity:在配置文件的activity节点添加 android:hardwareAccelerated="false"
- View:可以获得View对象后调用,或者直接在View的onDraw()方法里设置: view.setLayerType(View.LAYER_TYPE_HARDWARE, null);