(libgdx学习)翻转图片的两种方法

第一种是使用TextureRegion 中的flip(boolean ,boolean)方法进行翻转,另一种是使用SpriteBatch中的

draw( TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
进行翻转。。


第一种方法的实现方法:

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class MyGame implements ApplicationListener {

	Stage stage;
	Texture texture;
	TextureRegion region;
	Image image;
	
	
	@Override
	public void create() {
		stage = new Stage(480, 800, false);
		texture = new Texture(Gdx.files.internal("potato.jpg"));
		region = new TextureRegion(texture,0,0,512,512);
//		region.flip(true, false);//第一个参数为true,表示左右翻转
//		region.flip(false, true);//第二个参数为true,表示上下翻转
		region.flip(true, true);
		image = new Image(region);
		
		stage.addActor(image);
		
		Gdx.input.setInputProcessor(stage);
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		stage.act();
		stage.draw();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}



第二种方法:

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;

public class MyGame implements ApplicationListener {

	Stage stage;
	Texture texture;
	TextureRegion region;
	SpriteBatch batch;
	
	
	
	@Override
	public void create() {
		stage = new Stage(480, 800, false);
		texture = new Texture(Gdx.files.internal("potato.jpg"));
		region = new TextureRegion(texture,0,0,512,512);
		
		batch = new SpriteBatch();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		batch.begin();
		
		/**
		 * Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height. 
		 * The rectangle is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the rectangle should be scaled around originX, originY. 
		 * Rotation specifies the angle of counter clockwise rotation of the rectangle around originX, originY. 
		 * 
		 * 以下对这10个参数进行以下说明:
		 * region: TextureRegion
		 * x: 图片的左下角的坐标
		 * y: 图片的右下角坐标
		 * originX: 锚点的横坐标
		 * originY: 锚点的纵坐标
		 * width: 要将region画成的宽度width
		 * height: 要将region画成的高度height
		 * scaleX: X轴方向放大的倍数
		 * scaleY: Y轴方向放大的倍数
		 * rotation: 旋转度数(逆时针为正方向)
		 * 
		 * 这里需要说明的是:
		 * 这里的rotation 、scaleX、scaleY都是相对于originX,originY来说的 
		 */
		batch.draw(region, 0, 0, 128, 128, 256, 256, 1, 1, 90);
		
		
		batch.end();
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


四、源码下载地址:

http://download.csdn.net/detail/caihongshijie6/7157231

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值