Libgdx 之图片的翻转与旋转方式

时间:2022-10-23 21:10:45

在Libdgx 中经常会对图片进行翻转,旋转处理,很对时候是对资源的复用,一下是本文对图片进行处理的效果:

Libgdx 之图片的翻转与旋转方式


 region = Assets.testAtlas.findRegion("world1");

  TextureRegion region1 = new TextureRegion(region);

   TextureRegion region2 = new TextureRegion(region);

TextureRegion region3 = new TextureRegion(region);



翻转方法一:

使用 region3.flip(boolean, boolean) 进行上下左右翻转如image3

翻转方法二:

通过设置UV

  region2.setV(region.getV2());

        region2.setV2(region.getV());

      region1.setU(region.getU2());
        region1.setU2(region.getU());

这两种方法实质是一样的,都是通过setU setV setU2 setV2 实现翻转

值得注意的是,尽量避免直接对region进行翻转,Gdx底层通过Assets.testAtlas.findRegion("world1");方式,即使用AssetsManager 管理的纹理

都是定义成final类型,在别处使用的都是同一份内存。


图片旋转

如Imgae 4

使用batch 直接绘制资源,当然,可以翻转之后再进行绘制,绘制方法

batch.draw(region, x, y, originX, originY, width, height, scaleX, scaleY, rotation)

本文实现为

      batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
 Libgdx 之图片的翻转与旋转方式

全部实现代码


FlipDemo 主类


package com.whs.demo.application;

import com.badlogic.gdx.backends.jglfw.JglfwApplication;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.whs.demo.Demo;
import com.whs.demo.assets.Assets;
import com.whs.demo.progress.ImageRotation;

public class FlipTest
extends Demo {

private TextureRegion region;
private Stage stage;
private OrthographicCamera camera;
private Viewport viewport;


@Override
public void create() {
super.create();
Assets.init();
Assets.loadAll();
Assets.finishLoading();
Assets.initTestAtlas();
region = Assets.testAtlas.findRegion("world1");
stage = new Stage();
camera = new OrthographicCamera(WIDTH, HEIGHT);
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
viewport = new ScreenViewport(camera);
viewport.setCamera(camera);
stage.setViewport(viewport);
camera.update();

// 正常
LabelStyle labelStyle = new Label.LabelStyle(Assets.font, Color.RED);
Image image = new Image(region);
image.setOrigin(Align.center);
image.setPosition(0, 550F);
Label label = new Label("image", labelStyle);
label.setPosition(image.getX() + (image.getWidth() - label.getWidth()) / 2, image.getY() + image.getHeight());
label.setAlignment(Align.center);
label.setFontScale(0.5F);
label.setScale(0.5F);


stage .addActor(label);
stage.addActor(image);

// 水平翻转
TextureRegion region1 = new TextureRegion(region);
region1.setU(region.getU2());
region1.setU2(region.getU());
Image image1 = new Image(region1);
image1.setPosition(image.getWidth() + 10, 550F);
Label label1 = new Label("image1", labelStyle);
label1.setPosition(image1.getX() + (image1.getWidth() - label1.getWidth()) / 2, image1.getY() + image1.getHeight());
label1.setAlignment(Align.center);
label1.setFontScale(0.5F);
label1.setScale(0.5F);
stage.addActor(image1);
stage.addActor(label1);


// 水平翻转
TextureRegion region2 = new TextureRegion(region);
region2.setV(region.getV2());
region2.setV2(region.getV());
Image image2 = new Image(region2);
image2.setPosition(0 , 300F);
Label label2 = new Label("image2", labelStyle);
label2.setPosition(image2.getX() + (image2.getWidth() - label2.getWidth()) / 2, image2.getY() + image2.getHeight());
label2.setAlignment(Align.center);
label2.setFontScale(0.5F);
label2.setScale(0.5F);
stage.addActor(image2);
stage.addActor(label2);



// 水平翻转
TextureRegion region3 = new TextureRegion(region);
region3.flip(true, false);
Image image3 = new Image(region3);
image3.setPosition(image3.getWidth() + 10F , 300F);
Label label3 = new Label("image3", labelStyle);
label3.setPosition(image3.getX() + (image3.getWidth() - label3.getWidth()) / 2, image3.getY() + image3.getHeight());
label3.setAlignment(Align.center);
label3.setFontScale(0.5F);
label3.setScale(0.5F);
stage.addActor(image3);
stage.addActor(label3);

/**
* 图片的旋转
*/
ImageRotation image4 = new ImageRotation();
image4.setOrigin(Align.center);
image4.setRotation(60);
image4.setPosition(0 + 10F , 50F);
Label label4 = new Label("image4", labelStyle);
label4.setPosition(image4.getX() + (image4.getWidth() - label3.getWidth()) / 2, image4.getY() + image4.getHeight());
label4.setAlignment(Align.center);
label4.setFontScale(0.5F);
label4.setScale(0.5F);
stage.addActor(image4);
stage.addActor(label4);
}

@Override
public void render() {

super.render();
stage.act();
stage.draw();
}

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

stage.dispose();
Assets.dispose();
}

public static void main(String[] args) {
new JglfwApplication(new FlipTest(), "翻转测试 - By WHS", WIDTH , HEIGHT);
}

}
 
ImageRotation类
<pre name="code" class="java">package com.whs.demo.progress;import com.badlogic.gdx.graphics.g2d.Batch;import com.badlogic.gdx.graphics.g2d.TextureRegion;import com.badlogic.gdx.scenes.scene2d.Actor;import com.whs.demo.assets.Assets;public class ImageRotation extends Actor {            private TextureRegion region;    public ImageRotation() {        region = Assets.testAtlas.findRegion("world1");        setSize(region.getRegionWidth() , region.getRegionHeight());    }            @Override    public void draw(Batch batch, float parentAlpha) {        // TODO Auto-generated method stub        super.draw(batch, parentAlpha);              batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());    }    }