(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

你可能感兴趣的:((libgdx学习)翻转图片的两种方法)