libgdx进度条的使用

Progress.java:
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();
		
	}

}


Progress.java:


package com.jun.prograss;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.Stage;

public class Progress implements ApplicationListener{
private PrograssBar bar;

private Stage stage;
	@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);
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		bar.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(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
		
	}

}


MainActivity.java


package com.jun.libgdxprograss;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.jun.prograss.Progress;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends AndroidApplication {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       initialize(new Progress(), false);
    }

   
}

效果图:



libgdx进度条的使用_第1张图片

你可能感兴趣的:(libgdx进度条的使用)