---
title: "libgdx学习之图片翻转"
author: "macken"
date: "2013-03-12T17:01:58Z"
tags: ["Android", "Libgdx", "Mobile", "game"]
categories: ["移动开发"]
slug: "libgdx"
draft: false
lastmod: "2013-03-12T17:10:40Z"
id: 1828278
url: "https://macken.iteye.com/blog/1828278"
---

在看libgdx的cuboc的游戏源码遇到了图片翻转的两种形式，初次接触游戏编程，研究了半天才弄明白，衰

1.调用函数TextRegion.flip(boolean x,boolean y)

x、y轴进行翻转；

```
TextRegion text=new TextRegion(new Texture(Gdx.files.internal("a.jpg"));
text.flip(true,flase);
```

 原图

![](http://dl.iteye.com/upload/attachment/0081/5230/ca37f937-0428-3597-9edd-f7e98e4f6c14.jpg)  
x轴翻转以后

![](http://dl.iteye.com/upload/attachment/0081/5232/8760ed7a-e034-33ef-bf58-4e404e716626.jpg)

y轴翻转

```
text.flip(false,true)
```

原图 

![](http://dl.iteye.com/upload/attachment/0081/5230/ca37f937-0428-3597-9edd-f7e98e4f6c14.jpg)

y翻转后  
![](http://dl.iteye.com/upload/attachment/0081/5256/74b1faeb-cc6d-31b6-b77c-3883c0d9130a.jpg)

x、y轴翻转

```
text.flip(true,true)
```

 原图 

![](http://dl.iteye.com/upload/attachment/0081/5230/ca37f937-0428-3597-9edd-f7e98e4f6c14.jpg)

x、y轴翻转以后

![](http://dl.iteye.com/upload/attachment/0081/5258/23191e27-84b6-3f95-a5da-4bc73800a327.jpg)

flip函数适用于90和180对称旋转的情况，当然用SpriteBatch.draw的rotation功能也能实现类似的效果

 

2.SpriteBatch.draw()

函数定义

```
public void draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height,
float scaleX, float scaleY, float rotation){
}
```

 参数说明：

region：绘制纹理

x:位置坐标x

y:位置坐标y

originX:与x的x轴偏移量

originY:与y的y轴的偏移量

width:宽度

height:长度

scaleX:扩展倍数

scaleY:扩展倍数

rotation:翻转角度 逆时针方向为正(刚开始看成顺时针了,想了半天没想明白)

 

旋转原理

ex=originX+x

ey=originY+y

以(ex,ey)为原点，如果rotation为正，则逆时针旋转rotation度，为负，则顺时针旋转|rotation|度