阴影布局(继承FrameLayout)
前言:
很多情况下,美腻的UI美眉喜欢搞一些花里胡哨阴影什么的,作为一名有追求的程序员迎合美眉的需求,搞一些小阴影并不是什么大问题,比如写一个自定义shape,用5.0的z轴新特性和CardView都可以满足的,但是有些效果不太符合预计设计的效果,像自定义shape作为背景,看起来阴影会有些假,用5.0新特性第一个是版本问题还一个是有时候不起作用,网上也有解决不起作用的方法,我试了,都不太起作用,用cardview的话,如果cardview包裹的太多太复杂的控件,效果也不是太明显,所以有一个自定义FrameLayout来自己画阴影,以后再碰见阴影就又多了一种手段,满足应付设计师
Demo下载地址
效果图
package fengan.shadowdemo;
/**
* Created by fengan on 2017/10/10/010.
*/
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.support.annotation.FloatRange;
import android.util.AttributeSet;
import android.widget.FrameLayout;
/**
* Created by fengan on 10.10.2017.
*/
public class ShadowLayout extends FrameLayout {
// Default shadow values
private final static float DEFAULT_SHADOW_RADIUS = 30.0F;
private final static float DEFAULT_SHADOW_DISTANCE = 15.0F;
private final static float DEFAULT_SHADOW_ANGLE = 45.0F;
private final static int DEFAULT_SHADOW_COLOR = Color.DKGRAY;
// Shadow bounds values
private final static int MAX_ALPHA = 255;
private final static float MAX_ANGLE = 360.0F;
private final static float MIN_RADIUS = 0.1F;
private final static float MIN_ANGLE = 0.0F;
// Shadow paint
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
{
setDither(true);
setFilterBitmap(true);
}
};
// Shadow bitmap and canvas
private Bitmap mBitmap;
private final Canvas mCanvas = new Canvas();
// View bounds
private final Rect mBounds =