1.PragressBar还是上例中封装的Progress主要代码如下:
package com.jun.prograss; import android.R.anim; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.scenes.scene2d.Stage; import com.jun.load.AnimalActor; public class Progress implements ApplicationListener{ private PrograssBar bar; private Stage stage; AnimalActor animal; AssetManager manager; boolean hasinit; @Override public void create() { // TODO Auto-generated method stub bar=new PrograssBar(10, 10); stage=new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); stage.addActor(bar); // 是泪花AssetManager对象 manager=new AssetManager(); // 传入AssetManager的引用,便于animal的资源初始化,只有在调用initResourse方法时候才进行是泪花 animal=new AnimalActor(manager); // 加载29帧动画 for(int i=1;i<30;i++){ manager.load("animal/"+i+".png",Texture.class); } } @Override public void dispose() { // TODO Auto-generated method stub bar.dispose(); animal.dispose(); manager.clear(); manager.dispose(); } @Override public void pause() { // TODO Auto-generated method stub } @Override public void render() { // TODO Auto-generated method stub Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); Gdx.gl.glClearColor(1f, 1f,1f, 0); stage.act(Gdx.graphics.getDeltaTime()); stage.draw(); if(!manager.update()){ bar.setProgress(manager.getProgress()*100); } if(!hasinit&&manager.update()&&Gdx.input.isTouched()){ stage.removeActor(bar); System.out.print("-------#$@#%#$%#^%#^#%@"+(animal==null)); animal.initResourse(); System.out.println("#$@#%#$%#^%#^#%@"+(animal==null)); stage.addActor(animal); hasinit=true; } if(!manager.update()){ System.out.println("QueuedAssets:"+manager.getQueuedAssets()); System.out.println("LoadAssets:"+manager.getLoadedAssets()); System.out.println("Progress:"+manager.getProgress()); } // if(bar.progress<100){ // // bar.progress+=0.5; // // // } // if(bar.progress==100){ // // bar.progress=0; // // } } @Override public void resize(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void resume() { // TODO Auto-generated method stub } }
package com.jun.load; import java.util.ArrayList; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.utils.Disposable; public class AnimalActor extends Actor implements Disposable{ private ArrayList<Texture> textArray=new ArrayList<Texture>(); private ArrayList<TextureRegion> textReArray=new ArrayList<TextureRegion>(); Animation animation; TextureRegion[] workFrame; float stateTime; TextureRegion currentFrame; AssetManager manager; @Override public void draw(SpriteBatch arg0, float arg1) { // TODO Auto-generated method stub stateTime+=Gdx.graphics.getDeltaTime(); // 得到下一帧 currentFrame=animation.getKeyFrame(stateTime, true); // 以(0,0)绘制为起点,左下角为 0,0 画出动画 大小为 128*128 arg0.draw(currentFrame, 0,0,128,128); } public AnimalActor(AssetManager manager){ this.manager=manager; } @Override public Actor hit(float arg0, float arg1) { // TODO Auto-generated method stub return this; } @Override public void touchDragged(float x, float y, int pointer) { // TODO Auto-generated method stub // super.touchDragged(x, y, pointer); } @Override public void touchUp(float x, float y, int pointer) { // TODO Auto-generated method stub // super.touchUp(x, y, pointer); } @Override public boolean touchDown(float x, float y, int pointer) { // TODO Auto-generated method stub return false; } public void initResourse(){ Texture tex; int j; for(int i=1;i<30;i++){ textArray.add(manager.get("animal/"+i+".png", Texture.class)); } // TODO Auto-generated constructor stub for(int i=0;i<textArray.size();i++){ tex=textArray.get(i); // 注意TextureRegion 构造函数的参数 TextureRegion temtex=new TextureRegion(tex); textReArray.add(temtex); } j=textReArray.size(); workFrame=new TextureRegion[j]; for(int i=0;i<j;i++) workFrame[i]=textReArray.get(i); //设置的是0.06s一帧 animation = new Animation(0.06f, workFrame); } @Override public void dispose() { // TODO Auto-generated method stub for(int i=0;i<textArray.size();i++){ textArray.get(i).dispose(); } } }
package com.jun.prograss; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.utils.Disposable; public class PrograssBar extends Actor implements Disposable { Texture platform; Texture bar; int height; int width; float progress; float powerx,powery; public PrograssBar(int x,int y){ // super(); this.x=x; this.y=y; platform=new Texture(Gdx.files.internal("black.png")); bar=new Texture(Gdx.files.internal("green.png")); height=Gdx.graphics.getHeight(); width=Gdx.graphics.getWidth(); // 自己敲代码时候一定要注意800f 400f 后面不带f浮点数标示,不会报错但是进度条一直不出来 powerx=Gdx.graphics.getWidth()/800f; powery=Gdx.graphics.getHeight()/480f; } public void setProgress(float progress){ this.progress=progress; } @Override public void draw(SpriteBatch batch, float arg1) { // TODO Auto-generated method stub // batch.draw(platform, (Gdx.graphics.getWidth()-bar.getWidth()*powerx)/2, 0,platform.getWidth()*powerx, platform.getHeight()*powery); // batch.draw(bar, (Gdx.graphics.getWidth()-bar.getWidth()*powerx)/2,0,bar.getWidth()*progress/100f*powerx, bar.getHeight()*powery); batch.draw(platform, (Gdx.graphics.getWidth()-bar.getWidth()*powerx)/2, 0,platform.getWidth()*powerx,platform.getHeight()*powery); batch.draw(bar,(Gdx.graphics.getWidth()-bar.getWidth()*powerx)/2,0,bar.getWidth()*progress/100f*powerx,bar.getHeight()*powery); } @Override public Actor hit(float arg0, float arg1) { // TODO Auto-generated method stub return null; } @Override public void dispose() { // TODO Auto-generated method stub platform.dispose(); bar.dispose(); } }