Skia實現(xiàn)外陰影、內(nèi)陰影、高斯模糊、背景模糊效果

1、外陰影
外陰影Path:根據(jù)圖形Path偏移量offsetX,offsetY獲得;
描邊外陰影,圖片的透明沒有陰影;
外陰影Paint : 高斯模糊值sigmaX,sigmaY, 填充顏色值
sigmaX,sigmaY,前端過來數(shù)據(jù)以后有2倍關(guān)系;?
透明度為0,外陰影沒有;
link : https : //fiddle.skia.org/c/c64f77aa06a1634ef2f6833248e5bf86

 void draw(SkCanvas *canvas){
    //基礎(chǔ)數(shù)據(jù)
    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.內(nèi)陰影
內(nèi)陰影Path:根據(jù)圖形Path偏移量offsetX,offsetY獲得一個偏移Path2,然后Path與Path2做kDifference_Op運算,求得內(nèi)陰影innerShadowPath;
內(nèi)陰影Paint : 高斯模糊值sigmaX,sigmaY, 填充顏色值;
link : https : //fiddle.skia.org/c/17b7d8076d7ef84c0f1a0fa494259388

void draw(SkCanvas *canvas){

    //基礎(chǔ)數(shù)據(jù)
    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);
    //---------------------------------------

    //內(nèi)陰影繪制
    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);
    //---------------------------------------
}
內(nèi)陰影效果

3.高斯模糊
高斯模糊SkPicture:錄制當(dāng)前圖層的Picture;
高斯模糊Paint : 高斯模糊值sigmaX,sigmaY;
link : https : //fiddle.skia.org/c/a62c64dc9afedeb0f5cf13bdebf68fdf

void draw(SkCanvas *canvas){

    //基礎(chǔ)數(shù)據(jù)
    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.背景模糊
依賴參數(shù)
圖層的穿透混合模式
背景模糊SkPicture:錄制當(dāng)前圖層的Picture;
背景模糊Paint: 高斯模糊值gaussianSigma;
背景模糊的透明度:Alpha;
link:https://fiddle.skia.org/c/68d1cf656fc6473146e7cb5d80426365

void draw(SkCanvas *canvas){

    //基礎(chǔ)數(shù)據(jù)
    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);

    //---------------------------------------
}

背景模糊效果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容