美文网首页
Skia实现外阴影、内阴影、高斯模糊、背景模糊效果

Skia实现外阴影、内阴影、高斯模糊、背景模糊效果

作者: 豪爵吸金ing | 来源:发表于2021-06-23 19:58 被阅读0次

    1、外阴影
    外阴影Path:根据图形Path偏移量offsetX,offsetY获得;
    描边外阴影,图片的透明没有阴影;
    外阴影Paint : 高斯模糊值sigmaX,sigmaY, 填充颜色值
    sigmaX,sigmaY,前端过来数据以后有2倍关系;?
    透明度为0,外阴影没有;
    link : https : //fiddle.skia.org/c/c64f77aa06a1634ef2f6833248e5bf86

     void draw(SkCanvas *canvas){
        //基础数据
        const int offsetX = 10;
        const int offsetY = 10;
        const int sigmaX = 10;
        const int sigmaY = 10;
        SkPoint rectPts[] = {{64, 48}, {192, 160}};
        SkRect rect;
        rect.set(rectPts[0], rectPts[1]);
        SkPath path;
        path.addRect(rect);
    
        //-----------------------------------------
        //外阴影绘制
        SkPath dropShadowPath = path;
        SkPaint dropShadowPaint;
        dropShadowPaint.setAntiAlias(true);
        dropShadowPaint.setStyle(SkPaint::kStrokeAndFill_Style);
        dropShadowPaint.setColor(SK_ColorBLUE);
        dropShadowPaint.setImageFilter(SkImageFilters::Blur(sigmaX, sigmaY, nullptr, nullptr));
        SkMatrix mat;
        mat.postTranslate(offsetX, offsetY);
        dropShadowPath.transform(mat);
        canvas->drawPath(dropShadowPath, dropShadowPaint);
    
        //---------------------------------------
    
        //原始图形绘制
        SkPaint paint;
        paint.setAntiAlias(true);
        paint.setStyle(SkPaint::kStrokeAndFill_Style);
        paint.setColor(SK_ColorRED);
        canvas->drawPath(path, paint);
        //---------------------------------------
    }
    
    外阴影效果

    2.内阴影
    内阴影Path:根据图形Path偏移量offsetX,offsetY获得一个偏移Path2,然后Path与Path2做kDifference_Op运算,求得内阴影innerShadowPath;
    内阴影Paint : 高斯模糊值sigmaX,sigmaY, 填充颜色值;
    link : https : //fiddle.skia.org/c/17b7d8076d7ef84c0f1a0fa494259388

    void draw(SkCanvas *canvas){
    
        //基础数据
        const int offsetX = 10;
        const int offsetY = 10;
        const int sigmaX = 10;
        const int sigmaY = 10;
        const int gaussianSigma = 200;
        SkPoint rectPts[] = {{64, 48}, {192, 160}};
        SkRect rect;
        rect.set(rectPts[0], rectPts[1]);
        SkPath path;
        path.addRect(rect);
        //-----------------------------------------
        canvas->clipPath(path, true);
    
        //原始图形绘制
        SkPictureRecorder recorder;
        SkCanvas *recordingCanvas = recorder.beginRecording(rect);
        SkPaint paint;
        paint.setAntiAlias(true);
        paint.setStyle(SkPaint::kStrokeAndFill_Style);
        paint.setColor(SK_ColorRED);
        recordingCanvas->drawPath(path, paint);
        sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
        canvas->drawPicture(playback);
        //---------------------------------------
    
        //内阴影绘制
        SkPath offsetPath = path;
        SkMatrix mat;
        mat.postTranslate(offsetX, offsetY);
        offsetPath.transform(mat);
        SkPath innerShadowPath;
        {
    
            //OP(path, offsetPath, SkPathOp::kDifference_SkPathOp, &innerShadowPath);
            SkRegion region;
            SkRect pathBounds = path.computeTightBounds();
            SkIRect iRect = SkIRect::MakeLTRB(pathBounds.fLeft, pathBounds.fTop, pathBounds.fRight,
                                              pathBounds.fBottom);
            SkRegion shapeRegion(iRect);
            region.setPath(path, shapeRegion);
            SkRegion _region;
            SkRect pathBounds2 = offsetPath.computeTightBounds();
            SkIRect iRect2 = SkIRect::MakeLTRB(pathBounds2.fLeft, pathBounds2.fTop, pathBounds2.fRight,
                                               pathBounds2.fBottom);
    
            SkRegion shapeRegion2(iRect2);
            bool flag = _region.setPath(offsetPath, shapeRegion2);
            region.op(_region, SkRegion::Op::kDifference_Op);
            region.getBoundaryPath(&innerShadowPath);
        }
    
        SkPaint innerShadowPaint;
        innerShadowPaint.setAntiAlias(true);
        innerShadowPaint.setStyle(SkPaint::kStrokeAndFill_Style);
        innerShadowPaint.setColor(SK_ColorBLUE);
        innerShadowPaint.setImageFilter(SkImageFilters::Blur(sigmaX, sigmaY, nullptr, nullptr));
        canvas->drawPath(innerShadowPath, innerShadowPaint);
        //---------------------------------------
    }
    
    内阴影效果

    3.高斯模糊
    高斯模糊SkPicture:录制当前图层的Picture;
    高斯模糊Paint : 高斯模糊值sigmaX,sigmaY;
    link : https : //fiddle.skia.org/c/a62c64dc9afedeb0f5cf13bdebf68fdf

    void draw(SkCanvas *canvas){
    
        //基础数据
        const int offsetX = 10;
        const int offsetY = 10;
        const int sigmaX = 10;
        const int sigmaY = 10;
        SkPoint rectPts[] = {{64, 48}, {192, 160}};
        SkRect rect;
        rect.set(rectPts[0], rectPts[1]);
        SkPath path;
        path.addRect(rect);
    
        //-----------------------------------------
    
        //原始图形绘制
        SkPictureRecorder recorder;
        SkCanvas *recordingCanvas = recorder.beginRecording(rect);
        SkPaint paint;
        paint.setAntiAlias(true);
        paint.setStyle(SkPaint::kStrokeAndFill_Style);
        paint.setColor(SK_ColorRED);
        recordingCanvas->drawPath(path, paint);
        paint.setColor(SK_ColorBLUE);
        SkMatrix mat;
        mat.postTranslate(20, 20);
        path.transform(mat);
        recordingCanvas->drawPath(path, paint);
        sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
    
        //---------------------------------------
    
        //图层绘制
        SkPaint layerPaint;
        layerPaint.setAntiAlias(true);
        layerPaint.setStyle(SkPaint::kStrokeAndFill_Style);
        //layerPaint.setColor(SK_ColorBLUE);
        layerPaint.setImageFilter(SkImageFilters::Blur(sigmaX, sigmaY, nullptr, nullptr));
        canvas->drawPicture(playback, nullptr, &layerPaint);
        //---------------------------------------
    }
    
    高斯模糊效果

    4.背景模糊
    依赖参数
    图层的穿透混合模式
    背景模糊SkPicture:录制当前图层的Picture;
    背景模糊Paint: 高斯模糊值gaussianSigma;
    背景模糊的透明度:Alpha;
    link:https://fiddle.skia.org/c/68d1cf656fc6473146e7cb5d80426365

    void draw(SkCanvas *canvas){
    
        //基础数据
        const int offsetX = 10;
        const int offsetY = 10;
        const int gaussianSigma = 200;
        SkPoint rectPts[] = {{64, 48}, {192, 160}};
        SkRect rect;
        rect.set(rectPts[0], rectPts[1]);
        SkPath path;
        path.addRect(rect);
    
        //底图绘制---------------------------------
        {
            SkPaint paint;
            paint.setAntiAlias(true);
            paint.setStyle(SkPaint::kStrokeAndFill_Style);
            paint.setColor(SK_ColorYELLOW);
            canvas->drawCircle(100, 120, 30, paint);
        }
    
        //-----------------------------------------
        //原始图形绘制
    
        SkPictureRecorder recorder;
        SkCanvas *recordingCanvas = recorder.beginRecording(rect);
        SkPaint paint;
        paint.setStyle(SkPaint::kStrokeAndFill_Style);
        paint.setColor(SK_ColorRED);
        recordingCanvas->drawPath(path, paint);
        sk_sp<SkPicture> playback = recorder.finishRecordingAsPicture();
    
        //---------------------------------------
    
        //图层绘制
        SkPaint layerPaint;
        layerPaint.setAlpha(255 * 0.4);
        //SkMaskFilter: https://fiddle.skia.org/c/a92b2945009a7d5045248a4fc24122f9
    
        layerPaint.setMaskFilter(SkMaskFilter::MakeBlur(kInner_SkBlurStyle, gaussianSigma));
        canvas->drawPicture(playback, nullptr, &layerPaint);
    
        //---------------------------------------
    }
    
    
    背景模糊效果

    相关文章

      网友评论

          本文标题:Skia实现外阴影、内阴影、高斯模糊、背景模糊效果

          本文链接:https://www.haomeiwen.com/subject/szjbyltx.html