NinePatch拉伸图片- 原始方法

何为NinePatch?其实android原生即有NinePatch类,常在按钮中使用。

NinePatch

如图,将图片分成九份。中间部分可以根据需要扩大,使按钮的大小内容变动不受图片的限制。

而在libgdx的NinePatch其实就是九个TextureRegion对象。

常用的实例化方法有两个:

public  NinePatch (Texture texture, int  left, int  right, int  top, int  bottom)
 
public  NinePatch (TextureRegion region, int  left, int  right, int  top, int  bottom)

关于其中的四个int型参数如何取值我们可以参考一下源码:

public  NinePatch (TextureRegion region, int  left, int  right, int  top, int  bottom) {
         int  middleWidth = region.getRegionWidth() - left - right;
         int  middleHeight = region.getRegionHeight() - top - bottom;
         this .patches = new  TextureRegion[] { new  TextureRegion(region, 0 , 0 , left, top),
             new  TextureRegion(region, left, 0 , middleWidth, top), new  TextureRegion(region, left + middleWidth, 0 , right, top),
             new  TextureRegion(region, 0 , top, left, middleHeight), new  TextureRegion(region, left, top, middleWidth, middleHeight),
             new  TextureRegion(region, left + middleWidth, top, right, middleHeight),
             new  TextureRegion(region, 0 , top + middleHeight, left, bottom),
             new  TextureRegion(region, left, top + middleHeight, middleWidth, bottom),
             new  TextureRegion(region, left + middleWidth, top + middleHeight, right, bottom)};
     }

先计算中间部分的宽度和高度。然后开始切图,首先取顶部的最左边的那个,即图中编号1的那块,然后去它右边的,然后再右边的。

取完最上边的那行,然后取中间的那行,然后取最后一行的。

由上自下,由左自右。

而在绘制时又是如何处理的呢?看源码:

public  void  draw (SpriteBatch batch, float  x, float  y, float  width, float  height) {
         float  centerColumnX = x;
         if  (patches[BOTTOM_LEFT] != null )
             centerColumnX += patches[BOTTOM_LEFT].getRegionWidth();
         else  if  (patches[MIDDLE_LEFT] != null )
             centerColumnX += patches[MIDDLE_LEFT].getRegionWidth();
         else  if  (patches[TOP_LEFT] != null ) //
             centerColumnX += patches[TOP_LEFT].getRegionWidth();
 
         float  rightColumnX = x + width;
         if  (patches[BOTTOM_RIGHT] != null )
             rightColumnX -= patches[BOTTOM_RIGHT].getRegionWidth();
         else  if  (patches[MIDDLE_RIGHT] != null )
             rightColumnX += patches[MIDDLE_RIGHT].getRegionWidth();
         else  if  (patches[TOP_RIGHT] != null ) //
             rightColumnX += patches[TOP_RIGHT].getRegionWidth();
 
         float  middleRowY = y;
         if  (patches[TOP_LEFT] != null )
             middleRowY += patches[TOP_LEFT].getRegionHeight();
         else  if  (patches[TOP_CENTER] != null )
             middleRowY += patches[TOP_CENTER].getRegionHeight();
         else  if  (patches[TOP_RIGHT] != null ) //
             middleRowY += patches[TOP_RIGHT].getRegionHeight();
 
         float  topRowY = y + height;
         if  (patches[TOP_LEFT] != null )
             topRowY -= patches[TOP_LEFT].getRegionHeight();
         else  if  (patches[TOP_CENTER] != null )
             topRowY -= patches[TOP_CENTER].getRegionHeight();
         else  if  (patches[TOP_RIGHT] != null ) //
             topRowY -= patches[TOP_RIGHT].getRegionHeight();
 
         // Bottom row
         if  (patches[BOTTOM_LEFT] != null ) batch.draw(patches[BOTTOM_LEFT], x, y, centerColumnX - x, middleRowY - y);
         if  (patches[BOTTOM_CENTER] != null )
             batch.draw(patches[BOTTOM_CENTER], centerColumnX, y, rightColumnX - centerColumnX, middleRowY - y);
         if  (patches[BOTTOM_RIGHT] != null )
             batch.draw(patches[BOTTOM_RIGHT], rightColumnX, y, x + width - rightColumnX, middleRowY - y);
 
         // Middle row
         if  (patches[MIDDLE_LEFT] != null ) batch.draw(patches[MIDDLE_LEFT], x, middleRowY, centerColumnX - x, topRowY - middleRowY);
         if  (patches[MIDDLE_CENTER] != null )
             batch.draw(patches[MIDDLE_CENTER], centerColumnX, middleRowY, rightColumnX - centerColumnX, topRowY - middleRowY);
         if  (patches[MIDDLE_RIGHT] != null )
             batch.draw(patches[MIDDLE_RIGHT], rightColumnX, middleRowY, x + width - rightColumnX, topRowY - middleRowY);
 
         // Top row
         if  (patches[TOP_LEFT] != null ) batch.draw(patches[TOP_LEFT], x, topRowY, centerColumnX - x, y + height - topRowY);
         if  (patches[TOP_CENTER] != null )
             batch.draw(patches[TOP_CENTER], centerColumnX, topRowY, rightColumnX - centerColumnX, y + height - topRowY);
         if  (patches[TOP_RIGHT] != null )
             batch.draw(patches[TOP_RIGHT], rightColumnX, topRowY, x + width - rightColumnX, y + height - topRowY);
     }

先计算左右栏的宽度,在计算中间和顶部的高度。然后从下自上的绘制。

加载一张需要被拉伸的图片,代码如下:

TextureRegion windowbg;//窗口背景
NinePatch ninepatchwinbg;//拉伸窗口背景

windowbg = new TextureRegion(atlas0.findRegion("windowbg"));
ninepatchwinbg = new NinePatch(windowbg,50,100,20,20);
bg = new Image(ninepatchwinbg);
bg.setWidth(windowWid-100);
bg.setHeight(windowHeight-60);
bg.setPosition(70, 10);
addActor(bg);

若将图片作为window窗口背景图片,则:

TextureAtlas atlas = GameLoading.assetMager.get("emp/emp.pack", TextureAtlas.class);

BitmapFont font = GameLoading.assetMager.get(font/font.fnt", BitmapFont.class);

bg1 = new TextureRegion(atlas.findRegion("bg1"));
NinePatch ninepatch = new NinePatch(bg1,30,15,20,30);
NinePatchDrawable ninedrawable = new NinePatchDrawable(ninepatch);
WindowStyle windowStylem = new WindowStyle(font,
Color.BLACK,ninedrawable);
Window window = new Window("",windowStylem);

addActor(window);

资料来源:http://www.cnblogs.com/htynkn/archive/2011/11/16/libgdx_5.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值