Supporting Multiple image resolution for libgdx

…衆ロ難τιáo~ 提交于 2019-12-06 07:19:04

Update: Newer versions of Libgdx use a different strategy to resolve filenames (with directories, not files), so this answer does not apply to newer Libgdx versions after (0.9.2, I think? https://github.com/libgdx/libgdx/commit/3afcfe49c40196765033a90b4610159183dde981)

The built-in ResolutionFileResolver does not use a directory hierarchy. It uses file suffixes to match screen resolutions to assets.

Here's the code from the libGDX AssetManagerTest:

Resolution[] resolutions = { new Resolution(320, 480, ".320480"),
                             new Resolution(480, 800, ".480800"),
                             new Resolution(480, 856, ".480854") };
ResolutionFileResolver resolver = new ResolutionFileResolver(new InternalFileHandleResolver(), resolutions);
AssetManager manager = new AssetManager();
manager.setLoader(Texture.class, new TextureLoader(resolver));
...

The resolver appends one of those suffixes based on the current screen resolution. So, for example, if you look up "foo.jpg" on a 480x800 device, the file "foo.jpg.480800" will be opened.

If you want to resolve files based on directories (so "foo.jpg" would be resolved to "480x800/foo.jpg" or something like that), you can write a new resolver. Just implement FileHandleResolver. The ResolutionFileResolver.java source is probably a good place to start.

Once me too suffered from this problem but at end i got the working solution, for drawing anything using SpriteBatch or Stage in libgdx. Using orthogrphic camera we can do this.

first choose one constant resolution which is best for game. Here i have taken 1280*720(landscape).

class ScreenTest implements Screen{
 
final float appWidth = 1280, screenWidth = Gdx.graphics.getWidth();
final float appHeight = 720, screenHeight = Gdx.graphics.getHeight();

OrthographicCamera camera;

SpriteBatch batch;
Stage stage;

Texture img1;
Image img2;

 public ScreenTest(){
  		camera = new OrthographicCamera();
		camera.setToOrtho(false, appWidth, appHeight);
 		
        batch = new SpriteBatch();
 		batch.setProjectionMatrix(camera.combined);
  			
        img1 = new Texture("your_image1.png");
 		img2 = new Image(new Texture("your_image2.png"));
 		img2.setPosition(0, 0); // drawing from (0,0)
 	
        stage = new Stage(new StretchViewport(appWidth, appHeight, camera));
        stage.addActor(img2);
  }
    
    @Override
	public void render(float delta) {
	    batch.begin();
		batch.draw(img, 0, 0);
		batch.end();

		stage.act();
		stage.act(delta);
		stage.draw();
		
		// Also You can get touch input according to your Screen.
		
		if (Gdx.input.isTouched()) {
		 System.out.println(" X " + Gdx.input.getX() * (appWidth / screenWidth));
		 System.out.println(" Y " + Gdx.input.getY() * (appHeight / screenHeight));
	   }
	   
	}
  //
  : 
  :
  //
}

run this code in Any type of resolution it will going to adjust in that resolution without any disturbance.

If you use an OrthographicCamera you won't need to deal with different image resolutions.

Say that you have a camera with the size 400x300. Then you draw a 400x300 large image at coordinate (0,0). Then, regardless of screen resolution, the image will be scaled to fill the screen. This is really neat on Android where the screen resolution can vary a lot between different devices, and doing it like this you won't have to think about it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!