Mục tiêu:
Tạo 4 Image(hình ảnh):
- Hình ảnh gốc.
 - Hình ảnh được xoay
 - Hình ảnh bị giảm kích thước
 - Hình ảnh sử dụng kết cấu lặp lại (phản chiếu trên các cạnh).
 Chi tiết
- Tạo một sân khấu. Khởi tạo nó trong phương thức Tạo của bạn để đặt nó thành toàn màn hình
 
12345privateStage stage;@Overridepublicvoidcreate () {stage =newStage(newScreenViewport());}- Thay đổi Phương thức kết xuất của bạn để thêm vùng hiển thị
 
1234567@Overridepublicvoidrender () {Gdx.gl.glClearColor(1,1,1,1);Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);stage.act();stage.draw();}- Sao chép hình ảnh vào thư mục nội dung trong dự án Android của bạn. Trong bạn Create method load Texture
 
1Texture texture =newTexture(Gdx.files.internal("image.jpg"));Tạo hình ảnh
123Image image1 =newImage(texture);image1.setPosition(Gdx.graphics.getWidth()/3-image1.getWidth()/2,Gdx.graphics.getHeight()*2/3-image1.getHeight()/2);stage.addActor(image1);Xoay hình ảnh
12345Image image2 =newImage(texture);image2.setPosition(Gdx.graphics.getWidth()*2/3-image2.getWidth()/2,Gdx.graphics.getHeight()*2/3-image2.getHeight()/2);image2.setOrigin(image2.getWidth()/2,image2.getHeight()/2);image2.rotateBy(45);stage.addActor(image2);Thay đổi kích thước hình ảnh
1234Image image3 =newImage(texture);image3.setSize(texture.getWidth()/2,texture.getHeight()/2);image3.setPosition(Gdx.graphics.getWidth()/3-image3.getWidth()/2,Gdx.graphics.getHeight()/3-image3.getHeight());stage.addActor(image3);Tạo hình ảnh với kết cấu lặp lại
Quan trọng : nếu bạn định chạy mã của mình trên iOS, hãy đảm bảo rằng bạn sử dụng kết cấu có kích thước là bội số của 64. Trên iOS, nó sẽ không hoạt động nếu không.
- Thêm “setWrap” vào texture và yêu cầu nó “phản chiếu” các cạnh bằng cách sử dụng “setWrap (Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);”
 - Tạo một TextureRegion với kích thước tương đương với 8 x 4 của kết cấu giả này.
 - Tạo hình ảnh 4 bằng cách sử dụng vùng kết cấu đó
 - Đặt kích thước và vị trí của Image4.
 - Thêm nó vào sân khấu.
 
1234567texture.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);TextureRegion textureRegion =newTextureRegion(texture);textureRegion.setRegion(0,0,texture.getWidth()*8,texture.getHeight()*4);Image image4 =newImage(textureRegion);image4.setSize(200,100);image4.setPosition(Gdx.graphics.getWidth()*2/3-image4.getWidth()/2,Gdx.graphics.getHeight()/3-image4.getHeight());stage.addActor(image4);Mã:
Tải xuống hướng dẫn và xem mã hỗ trợ bên dưới
1234567số 8910111213141516171819202122232425262728293031323334353637383940414243publicclassMyGdxGameextendsApplicationAdapter {privateStage stage;@Overridepublicvoidcreate () {stage =newStage(newScreenViewport());Texture texture =newTexture(Gdx.files.internal("image.jpg"));Image image1 =newImage(texture);image1.setPosition(Gdx.graphics.getWidth()/3-image1.getWidth()/2,Gdx.graphics.getHeight()*2/3-image1.getHeight()/2);stage.addActor(image1);Image image2 =newImage(texture);image2.setPosition(Gdx.graphics.getWidth()*2/3-image2.getWidth()/2,Gdx.graphics.getHeight()*2/3-image2.getHeight()/2);image2.setOrigin(image2.getWidth()/2,image2.getHeight()/2);image2.rotateBy(45);stage.addActor(image2);Image image3 =newImage(texture);image3.setSize(texture.getWidth()/2,texture.getHeight()/2);image3.setPosition(Gdx.graphics.getWidth()/3-image3.getWidth()/2,Gdx.graphics.getHeight()/3-image3.getHeight());stage.addActor(image3);texture.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat);TextureRegion textureRegion =newTextureRegion(texture);textureRegion.setRegion(0,0,texture.getWidth()*8,texture.getHeight()*4);Image image4 =newImage(textureRegion);image4.setSize(200,100);image4.setPosition(Gdx.graphics.getWidth()*2/3-image4.getWidth()/2,Gdx.graphics.getHeight()/3-image4.getHeight());stage.addActor(image4);}@Overridepublicvoidrender () {Gdx.gl.glClearColor(1,1,1,1);Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);stage.act();stage.draw();}}
Tags:
LibGDX
