qt for android 图片可拉伸,qt实现九宫格布局,图片拉伸

在实现qt播放时,调用的mplayer,由于采用的是自定义绘图,用的是setAttribute(Qt::WA_TranslucentBackground);结果不能正常在上面显示播放画面,在默认皮肤下是没有问题的,决定用九宫格图片拉伸方式(效果如图)

090906215945.png

附件图片:

rar.gif

文件:

tst2.rar

大小:

10KB

下载:

typedef struct RECTANGLE

{

int left;

int top;

int right;

int bottom;

RECTANGLE()

{

left = 0;

top = 0;

right = 0;

bottom = 0;

}

RECTANGLE(int _left, int _top, int _right, int _bottom)

{

left = _left;

top = _top;

right = _right;

bottom = _bottom;

}

} RectangleEx;

Form::Form()

{

RectangleEx margin(40, 30, 20, 20);

resize(350, 500);

QPalette p = palette();

QPixmap img("back.png");

QBitmap mask("backMask.png");

QPixmap imgAA = scaleEx(img, margin);;

QPixmap maskAA = scaleEx(mask, margin);;

QRect r = imgAA.rect();

QRect r2 = maskAA.rect();

p.setBrush(QPalette::Window, QBrush(imgAA));

setPalette(p);

setMask(maskAA);

setWindowFlags(Qt::FramelessWindowHint);

}

QPixmap Form::scaleEx(QPixmap &map,RectangleEx rect)

{

QPixmap rMap;

QRect fRect = frameGeometry();

QRect pRect = map.rect();

// for four corner rects of src

// 1. topLeft rect

QRect srcRect1 = QRect(0, 0, rect.left, rect.top);

// 2. topRight rect

QRect srcRect2 = QRect(pRect.width()-rect.right, 0, rect.right, rect.top);

// 3. bottomLeft rect

QRect srcRect3 = QRect(0, pRect.height()-rect.bottom, rect.left, rect.bottom);

// 4. bottomRight rect

QRect srcRect4 = QRect(pRect.width()-rect.right, pRect.height()-rect.bottom,

rect.right, rect.bottom);

QRect pSrcRectMid =  QRect(rect.left, rect.top,

pRect.width()-(rect.left+rect.right), pRect.height()-(rect.top+rect.bottom));

QRect pSrcRectMidTop = QRect(rect.left, 0, pRect.width()-(rect.left+rect.right), rect.top);

QRect pSrcRectMidBottom = QRect(rect.left,pRect.height()-rect.bottom,

pRect.width()-(rect.left+rect.right), rect.bottom);

QRect pSrcRectMidLeft = QRect(0, rect.top, rect.left,

pRect.height()-(rect.bottom+rect.top));

QRect pSrcRectMidRight = QRect(pRect.width()-rect.right,rect.top,

rect.right, pRect.height()-(rect.top+rect.bottom));

// for four corner rects of desc

// 1. topLeft rect

QRect descRect1 = QRect(0, 0, rect.left, rect.top);

// 2. topRight rect

QRect descRect2 = QRect(fRect.width()-rect.right, 0, rect.right, rect.top);

// 3. bottomLeft rect

QRect descRect3 = QRect(0, fRect.height()-rect.bottom, rect.left, rect.bottom);

// 4. bottomRight rect

QRect descRect4 = QRect(fRect.width()-rect.right, fRect.height()-rect.bottom,

rect.right, rect.bottom);

QRect pDescRectMid =  QRect(rect.left, rect.top,

fRect.width()-(rect.left+rect.right), fRect.height()-(rect.top+rect.bottom));

QRect fDescRectMidTop = QRect(rect.left, 0,

fRect.width()-(rect.left+rect.right), rect.top);

QRect fDescRectMidLeft = QRect(0, rect.top, rect.left,

fRect.height()-(rect.top + rect.bottom));

QRect pDescRectMidRight = QRect(fRect.width()-rect.right, rect.top,

rect.right, fRect.height()-(rect.top + rect.bottom));

QRect fDescRectMidBottom = QRect(rect.left, fRect.height()-rect.bottom,

fRect.width()-(rect.left+rect.right), rect.bottom);

QPixmap map1 = map.copy(srcRect1);

QPixmap map2 = map.copy(srcRect2);

QPixmap map3 = map.copy(srcRect3);

QPixmap map4 = map.copy(srcRect4);

QPixmap mapMid = map.copy(pSrcRectMid);

QPixmap mapMidLeft = map.copy(pSrcRectMidLeft);

QPixmap mapMidRight = map.copy(pSrcRectMidRight);

QPixmap mapMidTop = map.copy(pSrcRectMidTop);

QPixmap mapMidBottom = map.copy(pSrcRectMidBottom);

// new pixmap size

QPixmap mapMidNew(fRect.width(), fRect.height());

QPainter painter(&mapMidNew);

// for four corners

painter.drawPixmap(descRect1, map1, map1.rect());

painter.drawPixmap(descRect2, map2, map2.rect());

painter.drawPixmap(descRect3, map3, map3.rect());

painter.drawPixmap(descRect4, map4, map4.rect());

// mid and for four borders

painter.drawPixmap(pDescRectMid, mapMid, mapMid.rect());

painter.drawPixmap(fDescRectMidLeft, mapMidLeft, mapMidLeft.rect());

painter.drawPixmap(pDescRectMidRight, mapMidRight, mapMidRight.rect());

painter.drawPixmap(fDescRectMidTop, mapMidTop, mapMidTop.rect());

painter.drawPixmap(fDescRectMidBottom, mapMidBottom, mapMidBottom.rect());

//mapMidNew.save("kk02.png");

return mapMidNew;

}

Form::~Form()

{

}

补允一下,这是一个测试,若稍改进一点,应该有个类似结构:

typedef struct PIXMAPINFO

{

QRect rectTopLeft;

QRect rectTopRight;

QRect rectBottomLeft;

QRect rectBottomRight;

QRect rectMiddle;

QRect rectMidLeft;

QRect rectMidTop;

QRect rectMidRight;

QRect rectMidBottom;

QPixmap pixTopLeft;

QPixmap pixTopRight;

QPixmap pixBottomLeft;

QPixmap pixBottomRight;

QPixmap pixMid;

QPixmap pixMidLeft;

QPixmap pixMidTop;

QPixmap pixMidRight;

QPixmap pixMidBottom;

} PixmapInfo;

这样理解比较清晰一点

PixmapInfo src;

PixmapInfo desc;

问题:

1. 当切换不同皮肤时,应该把src信息与desc信息分开,否则在resizeEvent中调用会造成拉伸窗口(改变大小)时显得不流畅

2. 当图拉大时,会出现占用内存增大

img;

img.load("test.jpg");

for (int i = 0; i < img.height(); i++)

{

for (int j = 0; j < img.width(); j++)

{

if(img.pixel(j, i) == qRgba(255,255,255,255))

img.setPixel(j, i, Qt::transparent);//Or you can use qRgba(0,0,0,0) instead for trans

}

}

img.save("changed.png");//I dont remember that png can alpha channel or not.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值