简体   繁体   中英

How to access Android resources e.g. dimensions in libgdx?

I need to get dimensions defined in XML files under Android folder from classes in Core directory of libgdx.

I know I can use assets folder for accessing drawables. But how about dimensions or strings?

You can't access this kind of platform specific information directly in the core project. But this can be achieved by interfacing these functions with a Facade, providing a platform-specific implementation for each target.

In libgdx-wiki there is an example for platform specific interfaces.

public interface PlatformSpecificStuff {
    public String getString();
    public int getDimensions();
}

// In -Android project
public class AndroidSpecificStuff implements PlatformSpecificStuff {
    public String getString(){
        // do whaterver you want with the resources.
        return "string resource";
    }

    public int getDimensions(){
        // do whaterver you want with the resources.
        return 42;
    }
}

// In -Desktop project
public class DesktopSpecificStuff implements PlatformSpecificStuff {
    public String getString(){
        // there is no xml or any andorid specific resource
        return null;
    }

    public int getDimensions(){
        // there is no xml or any andorid specific resource
        return 0;
    }
}

public class MyGame implements ApplicationListener {
    private final PlatformSpecificStuff pss;

    public MyGame(PlatformSpecificStuff pss) {
        this.pss= pss;
    }
}

Update : I don't know what are trying to accomplish by getting the dimensions as android resources. But you are probably in wrong track. If you want to support multiple screen sizes, have a look at viewports

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM