路径:
win32: LIBS += -LE:/skia/ -lskia.dll
INCLUDEPATH += E:/skia
INCLUDEPATH += E:/skia/skia
DEPENDPATH += E:/skia
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "paintarea.h"
#include <QMainWindow>
#include<QImage>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
private:
PaintArea* paintarea;
};
#endif // MAINWINDOW_H
paintarea.h
#ifndef PAINTAREA_H
#define PAINTAREA_H
#include <QWidget>
#include <QPaintEvent>
#include <QRect>
#include <QDebug>
#include <QPainter>
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSurface.h"
class PaintArea : public QWidget
{
Q_OBJECT
public:
explicit PaintArea(QWidget *parent = nullptr);
sk_sp<SkSurface> rasterSurface = nullptr; //CPU上的surface
void paintEvent(QPaintEvent *event);
void initGroup(int w,int h);
void drawshape(SkCanvas* canvas);
signals:
public slots:
private:
QVector<SkPath> path_shape;
QImage image;
SkImageInfo info; //图片填充
QByteArray data;
};
#endif // PAINTAREA_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
//#include <skia/src/core/SkAAClip.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
//QWidget* mainWidget=new QWidget();
paintarea=new PaintArea();
setMinimumSize(1000,1000);
setCentralWidget(paintarea);
}
MainWindow::~MainWindow()
{
}
paintarea.cpp
#include "paintarea.h"
#include <iostream>
#include "include/core/SkPath.h"
#include "include/core/SkRect.h"
PaintArea::PaintArea(QWidget *parent) : QWidget(parent)
{
setPalette(QPalette(Qt::white));
setAutoFillBackground(true);
setMinimumSize(500,500);
//std::cout<<"1"<<std::endl;
//update();
}
void PaintArea::paintEvent(QPaintEvent *event)
{
Q_UNUSED(this);
initGroup(width(),height()); //在此处进行初始化目的是在屏幕扩大时画板紧跟扩大
rasterSurface->getCanvas()->clear(SK_ColorWHITE);
auto canvas=rasterSurface->getCanvas();
canvas->save();
drawshape(canvas);
canvas->restore();
image=QImage((uchar*)(data.data()),width(),height(),QImage::Format_RGBA8888);
QPainter painter(this);
painter.drawImage(0,0,image);
}
void PaintArea::initGroup(int w, int h)
{
info=SkImageInfo::Make(w,h,SkColorType::kRGBA_8888_SkColorType,kUnpremul_SkAlphaType); //image中的颜色存储方式
size_t rowBytes=info.minRowBytes();
size_t size=info.computeByteSize(rowBytes);
data.resize(static_cast<int>(size));
rasterSurface=SkSurface::MakeRasterDirect(info,data.data(),rowBytes);
}
void PaintArea::drawshape(SkCanvas* canvas)
{
SkPaint paint;
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStroke(true); //显示框架,如果为false则会默认填充(直线显示会出现问题:无显示,因为无可填充的位置)
SkPath path_now;
//SkRect rect=SkRect::MakeXYWH(x-100,y-100,200,200);
//SkRect rect1=SkRect::MakeXYWH(x-100,y-100,200,100);
SkRect rect=SkRect::MakeXYWH(100,100,100,100);
SkPath path;
path.addRoundRect({10, 20, 54, 120}, 10, 20);
SkRect tests[] = {
{ 10, 40, 54, 80 },
{ 25, 20, 39, 120 },
{ 15, 25, 49, 115 },
{ 13, 27, 51, 113 },
};
for (unsigned i = 0; i < SK_ARRAY_COUNT(tests); ++i) {
SkPaint paint;
paint.setColor(SK_ColorRED);
canvas->drawPath(path, paint);
bool rectInPath = path.conservativelyContainsRect(tests[i]);
paint.setColor(rectInPath ? SK_ColorBLUE : SK_ColorBLACK);
canvas->drawRect(tests[i], paint);
canvas->translate(64, 0);
//path_now.addRect(rect);
//path_now.addOval(rect);
//canvas->drawPath(path_now,paint);
//qDebug()<<path_now.countPoints();
//qDebug()<<path_now.contains(150,150); //看点是否在path内部或者边线上
//qDebug()<<path_now<<endl;
//canvas->drawPath(path_now,paint);
//canvas->drawArc(rect,120,180,true,paint);
//canvas->drawRect(path_now.computeTightBounds(),paint); //得到图形的轮廓
/*
//skia: SkPath& SkPath::close()
paint.setStrokeWidth(15);
paint.setStrokeCap(SkPaint::kRound_Cap);
SkPath path;
const SkPoint points[] = {{20, 20}, {70, 20}, {40, 90}};
path.addPoly(points, SK_ARRAY_COUNT(points), false);
for (int loop = 0; loop < 2; ++loop) {
for (auto style : {SkPaint::kStroke_Style, SkPaint::kFill_Style,
SkPaint::kStrokeAndFill_Style} ) {
paint.setStyle(style);
canvas->drawPath(path, paint);
canvas->translate(85, 0);
}
path.close();
canvas->translate(-255, 128);
}*/
}