图片添加阴影,一般两种两种思路:1.layer_list (多张图重叠放置)。 2.setShadowLayer() (作用于 图片 ,或者文字)。
1.setShadowLayer();
radius--------以下所有该参数都代表模糊半径,高斯模糊算法。
/**
*
* @param radius 模糊度与该值成正比
* @param dx 阴影横向偏移量(正值往右偏)
* @param dy 阴影纵轴偏移量(正值偏上)
* @param shadowColor 阴影的颜色,即paint颜色(图片无效 :paint 的颜色值在图片操作中不起作用)
*/
public void setShadowLayer(float radius, float dx, float dy, int shadowColor) {
mShadowLayerRadius = radius;
mShadowLayerDx = dx;
mShadowLayerDy = dy;
mShadowLayerColor = shadowColor;
nSetShadowLayer(mNativePaint, radius, dx, dy, shadowColor);
}
2.ClearShadowLayer() 清除阴影
TextView.setShadow() ,TextViewy以及他的派生类(Button ,EditText) :1.存在直接setShadow() f方法 2.xml 布局设置
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="20sp"
android:text="@string/app_name"
android:shadowColor="#0D80DB"
android:shadowDx="10"
android:shadowDy="10"
android:shadowRadius="0.1"
/>
3.setMaskFilter() 添加发光效果
/**
* @param maskfilter 没有具体实现类 ,只有派生类
* 1. BlurMaskFilter 发光效果
* 2. EmbossMaskFilter 浮雕效果
* @return
*/
public MaskFilter setMaskFilter(MaskFilter maskfilter) {
long maskfilterNative = 0;
if (maskfilter != null) {
maskfilterNative = maskfilter.native_instance;
}
nSetMaskFilter(mNativePaint, maskfilterNative);
mMaskFilter = maskfilter;
return maskfilter;
}
1.BlurMaskFilter(float radius, Blur style)
1.原图 2.Blur.INNER(内发光) 3.Blur.SOLID(外发光) 4.Blur.NORMAL(内外发光) 5.Blur.OUTER(j仅显示发光效果)
2. EmbossMaskFilter(float[] direction, float ambient, float specular, float blurRadius)
direction 是一个含有三个float元素的数组,对应
x、y、z
三个方向上的值;用于指定光源方向ambient 环境光的因子 (0~1),0~1表示从暗到亮
specular 镜面反射系数,越接近0,反射光越强
blurRadius 模糊半径,值越大,模糊效果越明显
4.给图片添加阴影
思路:
1.先创建图片阴影(内外发光)
2.原图间隔一段距离覆盖上去
public ShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
//关闭硬件加速
setLayerType(LAYER_TYPE_SOFTWARE,null);
mBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.nvren);
//1.新建一个与原图一样的alpha bitmap 2.这个 mAlphaBitmap 的颜色受paint 颜色影响
mAlPha=mBitmap.extractAlpha();
alphaPaint=new Paint();
//设置颜色
alphaPaint.setColor(Color.GRAY);
//内外发光
alphaPaint.setMaskFilter(new BlurMaskFilter(50, BlurMaskFilter.Blur.NORMAL));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(mAlPha,null,new RectF(50,50,240,340),alphaPaint);
canvas.translate(-20,-20);
canvas.drawBitmap(mBitmap,null,new RectF(50,50,240,340),alphaPaint);
}