(libgdx学习)AssetManager及进度条的绘制

进度条的绘制实际上就是根据已经加载资源的百分比来改变图片的长度


一、初始化及常见API

manager.load("animation.png",Texture.class);

manager.unload("animation.png");

manager.isLoaded("animation.png")

manager.get("animation.png",Texture.class)

manager.update();

manager.getProgress()


二、应用举例

这个是一个名为“奋斗的小土豆”的博客(可以直接在百度中搜一下)中所用到的源码

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.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class MyGame implements ApplicationListener {

	AssetManager manager;
	BitmapFont font;
	SpriteBatch batch;
	
	BitmapFont font2;
	TextureAtlas tex2;
	Texture tex1;
	
	Texture progressBar_BG;
	Texture progressBar_FT;
	TextureRegion region;
	float progress = 0;
	
	
	@Override
	public void create() {
		batch = new SpriteBatch();
		font = new BitmapFont(Gdx.files.internal("font.fnt"),false);
		manager = new AssetManager();
		progressBar_BG = new Texture("progressBar.png");
		progressBar_FT = new Texture("greenBar.png");
		region = new TextureRegion(progressBar_BG, 0, 0,512,64);
		
		load();
	}

	public void load(){
		tex1 = new Texture("animation.png");
		tex2 = new TextureAtlas(Gdx.files.internal("pack"));
		font2 = new BitmapFont(Gdx.files.internal("verdana39.fnt"),false);
		
		manager.load("animation.png",Texture.class);
		manager.load("pack1.png",Texture.class);
		manager.load("pack",TextureAtlas.class);
		manager.load("verdana39.png",Texture.class);
		manager.load("verdana39.fnt",BitmapFont.class);
		
	}
	
	public void unload(){
		tex1.dispose();
		tex2.dispose();
		font2.dispose();
		
		manager.unload("animation.png");
		manager.unload("pack1.png");
		manager.unload("pack");
		manager.unload("verdana39.png");
		manager.unload("verdana39.fnt");
	}
	
	@Override
	public void dispose() {
		unload();
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
        manager.update();		
		
		batch.begin();
		
		if(manager.isLoaded("animation.png")){
			batch.draw(manager.get("animation.png",Texture.class), 50,50);
		}
		
		if(manager.isLoaded("verdana39.png")){
			batch.draw(manager.get("verdana39.png",Texture.class),250,150,150,150);
		}
		
		if(manager.isLoaded("pack")){
			batch.draw(manager.get("pack",TextureAtlas.class).findRegion("particle-star"),270,70);
		}
		
		if(manager.isLoaded("verdana39.fnt")){
			manager.get("verdana39.fnt",BitmapFont.class).draw(batch, "This is a test", 100, 200);
		}
		
		if(manager.isLoaded("back.ogg",Sound.class)){
			manager.get("back.ogg",Sound.class).play();
		}
		
		progress = manager.getProgress()*512;
		font.draw(batch, "Progress :" + manager.getProgress()*100 +"%", 160, 15);
		batch.draw(progressBar_FT, 40, 0, progress, 54);
		batch.draw(region, 0, 20);
		
		batch.end();
	}

	@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.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class MyGame implements ApplicationListener {

	AssetManager manager;
	SpriteBatch batch;
	BitmapFont font;
	
	Texture background_FT;
	Texture background_BG;
	TextureRegion region;
	
	float progress;
	
	@Override
	public void create() {
		manager = new AssetManager();
		batch = new SpriteBatch();
		font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
		
		background_BG = new Texture(Gdx.files.internal("progressBar.png"));
		background_FT = new Texture(Gdx.files.internal("greenBar.png"));
		region = new TextureRegion(background_BG , 0, 0,512,64);
		
		load();
	}
	
	public void load(){
		manager.load("animation.png",Texture.class);
		manager.load("pack",TextureAtlas.class);
		manager.load("pack1.png",Texture.class);
		manager.load("verdana39.fnt",BitmapFont.class);
		manager.load("verdana39.png",Texture.class);
		
	}

	public void unload(){
		font.dispose();
		batch.dispose();
		
		manager.unload("animation.png");
		manager.unload("pack");
		manager.unload("pack1.png");
		manager.unload("verdana39.png");
		manager.unload("verdana39.fnt");
		
		
	}
	
	
	@Override
	public void dispose() {
		unload();

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		manager.update();
		
		batch.begin();
		
		if(manager.isLoaded("animation.png",Texture.class)){
			batch.draw(manager.get("animation.png", Texture.class), 0,100);
		}
		
		if(manager.isLoaded("verdana39.png",Texture.class)){
			batch.draw(manager.get("verdana39.png",Texture.class),100,100);
		}
		
		if(manager.isLoaded("verdana39.fnt",BitmapFont.class)){
			manager.get("verdana39.fnt",BitmapFont.class).draw(batch, "hello AssetManager", 0, 300);
		}
		
		if(manager.isLoaded("pack",TextureAtlas.class)){
			batch.draw(manager.get("pack", TextureAtlas.class).findRegion("particle-star"), 200,0);
		}
		
		if(manager.isLoaded("pack1.png",Texture.class)){
			batch.draw(manager.get("pack1.png",Texture.class), 300,300);
		}
		
		progress = manager.getProgress()*512;
		
		font.draw(batch, "Progress: " + manager.getProgress()*100 +"%" ,160, 15);
		batch.draw(background_FT, 40, 0, progress, 54);
		batch.draw(region, 0, 20);
		
		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/7129269

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值