How to make an object a fixed size across platforms?

edited January 10 in Android Mode

First thing's first: I just started with Processing about 2 weeks ago, and Android mode a couple days ago, so anything you say should assume very little knowledge but the basics of Java mode. Okay, now. If I have a basic ball bouncing game ported from Java mode, different pixel densities cause the ball the be all different sizes from device to device, so how can I make the ball be a fixed size across multiple platforms?

Answers

  • Answer ✓

    You should use the display's density to properly scale the size of the elements in your sketch. You can get the density from the current context:

    import android.content.Context;
    float density;
    
    void setup() {
      size(640, 360, P3D);
      Context context = getActivity();
      density = context.getResources().getDisplayMetrics().density;
    }
    
    void draw() {
      // use density when drawing stuff...
    }
    

    These pages have useful information as well:

    https://developer.android.com/guide/practices/screens_support.html https://www.captechconsulting.com/blogs/understanding-density-independence-in-android

  • edited January 10

    @codeanticode sorry, but Processing says "The function 'getActivity()' does not exist." for your code. Am I doing something wrong?

  • What version of the Android mode are you using? The code I posted is for the latest stable version, 3.0.2, available through the Contributions Manager.

    If you are using a 4.0 beta release, then the API is slightly different and you need to do:

    import android.content.Context;
    float density;
    
    void setup() {
      size(640, 360, P3D);
      Context context = surface.getActivity();
      density = context.getResources().getDisplayMetrics().density;
    }
    
    void draw() {
      // use density when drawing stuff...
    }
    

    In any case, the next beta release will incorporate a density variable directly in PApplet or PSurface for conveniency (https://github.com/processing/processing-android/issues/263)

  • @codeanticode Where could I find the changes implemented in every new android mode release?

    Kf

  • These changes will be reflected in the Processing for Android site (both in the API section and tutorials), as work with the beta releases continues. The release notes could also be useful to know what's new before the site is updated.

  • @codeanticode Are you one of the ppl in the team making changes to the Android mode in Processing? Just curious. I find the Android site to have limited content. However a link to the release notes is exactly what I was looking for. Thank you!

    Kf

  • @codeanticode I can see in the release notes that you are the author of them. Never min my last question.

    Kf

Sign In or Register to comment.