setShadowLayour的属性介绍

    xiaoxiao2021-03-25  121

    setShadowLayer(float radius, float dx, float dy, int shadowColor)

    radius表示阴影的扩散半径;dx和dy表示阴影平面上的偏移值;shadowColor就不说了阴影颜色。

    注意: 这个方法不支持硬件加速,所以我们要测试时必须先关闭硬件加速。

    加上这一句 setLayerType(LAYER_TYPE_SOFTWARE, null); 还要注释@SuppressLint(“NewApi”)


    创建阴影的方法:

    //创建shadow private void createShadow() { if (shadowEffect) { Bitmap shadowBitmap = createShadowBitmap(); BitmapDrawable shadowDrawable = new BitmapDrawable(getResources(), shadowBitmap); if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN) { //noinspection deprecation setBackgroundDrawable(shadowDrawable); } else { setBackground(shadowDrawable); } } else { clearShadow(); } } private Bitmap createShadowBitmap() { Bitmap shadowBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ALPHA_8); Canvas canvas = new Canvas(shadowBitmap); RectF shadowRect = new RectF( shadowRadius + Math.abs(shadowOffsetX), shadowRadius + Math.abs(shadowOffsetY), getWidth() - shadowRadius - Math.abs(shadowOffsetX), getHeight() - shadowRadius - Math.abs(shadowOffsetY)); Paint shadowPaint = new Paint(); shadowPaint.setAntiAlias(true); shadowPaint.setColor(Color.TRANSPARENT); shadowPaint.setStyle(Paint.Style.FILL); if (!isInEditMode()) { //设置Shadow的层级 radius表示阴影的扩散半径;dx和dy表示阴影平面上的偏移值;shadowColor就不说了阴影颜色。 shadowPaint.setShadowLayer(shadowRadius, shadowOffsetX, shadowOffsetY, shadowColor); } canvas.drawRoundRect(shadowRect, shadowCornerRadius, shadowCornerRadius, shadowPaint); return shadowBitmap; }
    转载请注明原文地址: https://ju.6miu.com/read-4043.html

    最新回复(0)