利用BlurMaskFilter实现具有阴影效果的Imageview

主要是
BlurMaskFilter bf=new BlurMaskFilter(20,BlurMaskFilter.Blur.INNER);
设置影音大小为20,模式为Inner:
解释为Blur inside the border
paint.setMaskFilter(bf);设置画笔
canvas.drawBitmap(bit.extractAlpha(paint,null),src,rect,paint);绘画影音
canvas.drawBitmap(bit,src,rect1,null);叠上原图片

import android.content.Context;import android.graphics.Bitmap;import android.graphics.BlurMaskFilter;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.support.annotation.Nullable;import android.util.AttributeSet;/** * Created by yang on 2017/8/7. */public class shadowimageview extends android.support.v7.widget.AppCompatImageView { private Paint paint; public shadowimageview(Context context) { this(context,null); } public shadowimageview(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public shadowimageview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint=new Paint(); init(); } private void init() { BlurMaskFilter bf=new BlurMaskFilter(20,BlurMaskFilter.Blur.INNER); paint.setColor(Color.GRAY); paint.setMaskFilter(bf); } @Override protected void onDraw(Canvas canvas) { Drawable drawable=getDrawable(); Bitmap bit=((BitmapDrawable)drawable).getBitmap(); setBackgroundDrawable(null); int x=getWidth(); int y=getHeight(); Rect rect=new Rect(20,20,x,y); Rect rect1=new Rect(0,0,x-20,y-20); int x2=bit.getWidth(); int y2=bit.getHeight(); Rect src=new Rect(0,0,x2,y2); canvas.drawBitmap(bit.extractAlpha(paint,null),src,rect,paint); canvas.drawBitmap(bit,src,rect1,null); //super.onDraw(canvas); }}
效果:
 


          
展开阅读全文

没有更多推荐了,返回首页