上一篇博文已經詳細講解了libgdx的幾個圖像處理類,下面直接通過例子來講解;
Gdx.files是libgdx的文件模塊,主要提供以下5大功能。
- 讀取文件
- 寫文件
- 複製文件
- 移動文件
而獲取操作文件的FileHandle有4種方法。
1.Classpath
路徑相對於classpath,文件通常爲只讀。
2.Internal
內部文件路徑相對於程序根目錄或者android 的assets文件夾。
3.External
外部文件路徑是相對於SD卡根目錄
4.Absolute
assets文件夾本身就是存儲資源的文件夾,而且相比resource文件夾,它其中的資源不會生成R中的ID,用來放圖片很是合適。
所以用Gdx.files.internal("image1.jpg")獲取圖片,然後調用batch.draw(texture,20,10);繪製圖形,20,10是座標,笛卡爾座標,以左下角爲原點
GameTexture類代碼
package com.hanfeng.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GameTexture implements ApplicationListener {
private SpriteBatch spriteBatch;
private Texture texture;
@Override
public void create() {
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("picture.jpg"));
}
@Override
public void dispose() {
spriteBatch.dispose();
texture.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(texture, 0, 0);
spriteBatch.end();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
模擬器運行太慢了,就不截圖了。因爲我是在真機上面進行運行測得,在上傳截圖麻煩,大家可以自行測試。通過運行,我們發現圖片早屏幕上不能夠顯示完成,有一部分被遮住了。其實在實際操作過程,我們使用也是圖片的一部分內容,或是將多個圖片資源集合在一個文件中。
下面我看通過使用TextureRegion類來進行測試看看只顯示圖片部分的效果。
draw(TextureRegion
region, float x, float y, float width, float height)
package com.hanfeng.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
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;
public class GameTexture implements ApplicationListener {
//繪圖
private SpriteBatch spriteBatch;
//紋理
private Texture texture;
//區域
private TextureRegion region;
@Override
public void create() {
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("picture.jpg"));
region = new TextureRegion(texture,30,80,200,200);
}
@Override
public void dispose() {
spriteBatch.dispose();
texture.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(region, 0, 0);
spriteBatch.end();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}
如果你覺得TextureRegion還不夠強大的話,那麼你可以使用Sprite類。可以設置位置和顏色哦!
package com.hanfeng.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class GameTexture implements ApplicationListener {
//繪圖
private SpriteBatch spriteBatch;
//紋理
private Texture texture;
//精靈
private Sprite sprite;
@Override
public void create() {
spriteBatch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("picture.jpg"));
sprite = new Sprite(texture,80,80,400,300);
sprite.setPosition(10, 10);//位置
sprite.setRotation(50);//旋轉
sprite.setColor(1, 0.5f, 0.8f, 1);//着色
}
@Override
public void dispose() {
spriteBatch.dispose();
texture.dispose();
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
}