9宮格縮放規則以下:
一、將一張圖分割成9塊
二、四個角(1,3,7,9)在縮放的時候是保持大小不變
三、圖塊2,8僅當寬度變化時縮放寬度。
四、圖塊4,6僅當高度變化時縮放高度。
五、圖塊5當圖片大小發生變化,寬度和高度都進行縮放。.net
/*
** 功能 : 九宮格圖處理
** picName : 圖片名字
** iHorzSplit : 四個角上格子的寬度
** iVertSplit : 四個角上格子的高度
** DstWidth : 整個圖片縮放的寬度
** DstWidth : 整個圖片縮放的高度
** 返回值 : 處理完成的圖片
*/
QPixmap* NinePatch(QString& picName,int iHorzSplit,int iVertSplit, int DstWidth, int DstHeight)
{
QPixmap* pix = new QPixmap(picName);
int pixWidth = pix->width();
int pixHeight = pix->height();
QPixmap pix_1 = pix->copy(0, 0, iHorzSplit, iVertSplit);
QPixmap pix_2 = pix->copy(iHorzSplit, 0, pixWidth-iHorzSplit*2, iVertSplit);
QPixmap pix_3 = pix->copy(pixWidth-iHorzSplit, 0, iHorzSplit, iVertSplit);
QPixmap pix_4 = pix->copy(0, iVertSplit, iHorzSplit, pixHeight-iVertSplit*2);
QPixmap pix_5 = pix->copy(iHorzSplit, iVertSplit, pixWidth-iHorzSplit*2, pixHeight-iVertSplit*2);
QPixmap pix_6 = pix->copy(pixWidth-iHorzSplit, iVertSplit, iHorzSplit, pixHeight-iVertSplit*2);
QPixmap pix_7 = pix->copy(0, pixHeight-iVertSplit, iHorzSplit, iVertSplit);
QPixmap pix_8 = pix->copy(iHorzSplit, pixHeight-iVertSplit, pixWidth-iHorzSplit*2, pixWidth - iHorzSplit*2);
QPixmap pix_9 = pix->copy(pixWidth-iHorzSplit, pixHeight-iVertSplit, iHorzSplit, iVertSplit);
pix_2 = pix_2.scaled(DstWidth-iHorzSplit*2,iVertSplit, Qt::IgnoreAspectRatio);//保持高度拉寬
pix_4 = pix_4.scaled(iHorzSplit, DstHeight-iVertSplit*2, Qt::IgnoreAspectRatio);//保持寬度拉高
pix_5 = pix_5.scaled(DstWidth-iHorzSplit*2,DstHeight-iVertSplit*2, Qt::IgnoreAspectRatio);//寬高都縮放
pix_6 = pix_6.scaled(iHorzSplit, DstHeight-iVertSplit*2, Qt::IgnoreAspectRatio);//保持寬度拉高
pix_8 = pix_8.scaled(DstWidth-iHorzSplit*2, iVertSplit);//保持高度拉寬
QPixmap* resultImg =new QPixmap(DstWidth, DstHeight);
QPainter* painter = new QPainter(resultImg);
if (!resultImg->isNull()) {
painter->drawPixmap(0,0,pix_1);
painter->drawPixmap(iHorzSplit, 0, pix_2);
painter->drawPixmap(DstWidth-iHorzSplit,0,pix_3);
painter->drawPixmap(0, iVertSplit, pix_4);
painter->drawPixmap(iHorzSplit, iVertSplit, pix_5);
painter->drawPixmap(DstWidth-iHorzSplit, iVertSplit, pix_6);
painter->drawPixmap(0, DstHeight-iVertSplit, pix_7);
painter->drawPixmap(iHorzSplit, DstHeight-iVertSplit, pix_8);
painter->drawPixmap(DstWidth-iHorzSplit, DstHeight-iVertSplit, pix_9);
painter->end();
}
return resultImg;
}blog
參考連接:http://bbs.csdn.net/topics/390506636
————————————————
版權聲明:本文爲CSDN博主「燦哥哥」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/caoshangpa/article/details/53391230圖片