上一篇博文已经详细讲解了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
}
}