写点什么

Android 进阶:最简单的方式实现自定义阴影效果,7 年老 Android 一次坑爹的面试经历

用户头像
Android架构
关注
发布于: 刚刚

public class ShadowViewCard extends FrameLayout {private static final int DEFAULT_VALUE_SHADOW_COLOR = R.color.shadow_default_color;private static final int DEFAULT_VALUE_SHADOW_CARD_COLOR = R.color.shadow_card_default_color;private static final int DEFAULT_VALUE_SHADOW_ROUND = 0;


private static final int DEFAULT_VALUE_SHADOW_RADIUS = 10;private static final int DEFAULT_VALUE_SHADOW_TOP_HEIGHT = 5;private static final int DEFAULT_VALUE_SHADOW_LEFT_HEIGHT = 5;private static final int DEFAULT_VALUE_SHADOW_RIGHT_HEIGHT = 5;private static final int DEFAULT_VALUE_SHADOW_BOTTOM_HEIGHT = 5;private static final int DEFAULT_VALUE_SHADOW_OFFSET_Y = 0;private static final int DEFAULT_VALUE_SHADOW_OFFSET_X = DEFAULT_VALUE_SHADOW_TOP_HEIGHT / 3;


private int shadowRound;private int shadowColor;private int shadowCardColor;private int shadowRadius;private int shadowOffsetY;private int shadowOffsetX;private int shadowTopHeight;private int shadowLeftHeight;private int shadowRightHeight;private int shadowBottomHeight;


public ShadowViewCard(Context context) {this(context, null);}


public ShadowViewCard(Context context, AttributeSet attrs) {this(context, attrs, 0);}


public ShadowViewCard(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context, attrs);}


private void initView(Context context, AttributeSet attrs) {TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShadowViewCard);shadowRound = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowRound, DEFAULT_VALUE_SHADOW_ROUND);shadowColor = a.getColor(R.styleable.ShadowViewCard_shadowColor, getResources().getColor(DEFAULT_VALUE_SHADOW_COLOR));shadowCardColor = a.getColor(R.styleable.ShadowViewCard_shadowCardColor, getResources().getColor(DEFAULT_VALUE_SHADOW_CARD_COLOR));shadowTopHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowTopHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_TOP_HEIGHT));shadowRightHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowRightHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_RIGHT_HEIGHT));shadowLeftHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowLeftHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_LEFT_HEIGHT));shadowBottomHeight = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowBottomHeight, dp2px(getContext(), DEFAULT_VALUE_SHADOW_BOTTOM_HEIGHT));shadowOffsetY = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowOffsetY, dp2px(getContext(), DEFAULT_VALUE_SHADOW_OFFSET_Y));shadowOffsetX = a.getDimensionPixelSize(R.styleable.ShadowViewCard_shadowOffsetX, dp2px(getContext(), DEFAULT_VALUE_SHADOW_OFFSET_X));shadowRadius = a.getInteger(R.styleable.ShadowViewCard_shadowRadius, DEFAULT_VALUE_SHADOW_RADIUS);a.recycle();setPadding(shadowLeftHeight, shadowTopHeight, sha


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


dowRightHeight, shadowBottomHeight);setLayerType(LAYER_TYPE_SOFTWARE, null);}


public static int dp2px(Context context, float dipValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dipValue * scale + 0.5f);}


@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}


@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);}


@Overrideprotected void dispatchDraw(Canvas canvas) {Paint shadowPaint = new Paint();shadowPaint.setColor(Color.WHITE);shadowPaint.setStyle(Paint.Style.FILL);shadowPaint.setAntiAlias(true);float left = shadowLeftHeight;float top = shadowTopHeight;float right = getWidth() - shadowRightHeight;float bottom = getHeight() - shadowBottomHeight;shadowPaint.setShadowLayer(shadowRadius, shadowOffsetX, shadowOffsetX, shadowColor);RectF rectF = new RectF(left, top, right, bottom);canvas.drawRoundRect(rectF, shadowRound, shadowRound, shadowPaint);canvas.save();super.dispatchDraw(canvas);}}


attr.xml


color.xml


<color name="shadow_default_color">#1a000000</color><color name="shadow_card_default_color">#ffffff</color>


有更多其他想法的朋友,欢迎[加 vx]vx:mm1591314250,一起交流技术。吃货还整理了一份 Android 高级工程师技术大纲以及一套系统全面而且非常深入的 Android 进阶资料,需要的可以进群,找管理领取。



用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Android进阶:最简单的方式实现自定义阴影效果,7年老Android一次坑爹的面试经历