Skip to content

Commit cab353f

Browse files
committed
added display density
1 parent 76c9a1c commit cab353f

File tree

7 files changed

+61
-34
lines changed

7 files changed

+61
-34
lines changed

core/src/processing/android/AppComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract public interface AppComponent extends PConstants {
3434
public void initDimensions();
3535
public int getDisplayWidth();
3636
public int getDisplayHeight();
37+
public float getDisplayDensity();
3738
public int getKind();
3839
public void setSketch(PApplet sketch);
3940
public PApplet getSketch();

core/src/processing/android/PFragment.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
//import android.app.Fragment;
2626
import android.support.v4.app.Fragment;
27+
import android.util.DisplayMetrics;
2728
import android.content.pm.ActivityInfo;
2829
import android.content.res.Configuration;
2930
import android.graphics.Point;
@@ -38,32 +39,23 @@
3839
import processing.core.PApplet;
3940

4041
public class PFragment extends Fragment implements AppComponent {
41-
// private DisplayMetrics metrics;
42+
private DisplayMetrics metrics;
4243
protected Point size;
4344
protected PApplet sketch;
4445

4546
public PFragment() {
4647
}
4748

4849
public void initDimensions() {
49-
/*
50-
metrics = new DisplayMetrics();
51-
// getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
5250
WindowManager wm = getActivity().getWindowManager();
5351
Display display = wm.getDefaultDisplay();
54-
display.getRealMetrics(metrics);
55-
*/
52+
metrics = new DisplayMetrics();
53+
display.getMetrics(metrics);
5654

57-
// metrics = new DisplayMetrics();
58-
// getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
5955
// display.getRealMetrics(metrics); // API 17 or higher
6056
// display.getRealSize(size);
6157

62-
// display.getMetrics(metrics);
63-
6458
size = new Point();
65-
WindowManager wm = getActivity().getWindowManager();
66-
Display display = wm.getDefaultDisplay();
6759
if (Build.VERSION.SDK_INT >= 17) {
6860
display.getRealSize(size);
6961
} else if (Build.VERSION.SDK_INT >= 14) {
@@ -87,6 +79,10 @@ public int getDisplayHeight() {
8779
// return metrics.heightPixels;
8880
}
8981

82+
public float getDisplayDensity() {
83+
return metrics.density;
84+
}
85+
9086
public int getKind() {
9187
return FRAGMENT;
9288
}

core/src/processing/android/PWallpaper.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.view.SurfaceHolder;
2929
import android.view.WindowManager;
3030
import processing.core.PApplet;
31+
import android.util.DisplayMetrics;
3132
import android.util.Log;
3233
import android.os.Build;
3334
//import android.view.WindowManager;
@@ -38,7 +39,7 @@ public class PWallpaper extends WallpaperService implements AppComponent {
3839
String TAG = "PWallpaper";
3940

4041
protected Point size;
41-
// private DisplayMetrics metrics;
42+
private DisplayMetrics metrics;
4243
protected PEngine engine;
4344

4445
public PWallpaper() {
@@ -48,15 +49,15 @@ public PWallpaper(PApplet sketch) {
4849
}
4950

5051
public void initDimensions() {
51-
// metrics = new DisplayMetrics();
52-
// getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
52+
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
53+
Display display = wm.getDefaultDisplay();
54+
metrics = new DisplayMetrics();
55+
display.getMetrics(metrics);
56+
5357
// display.getRealMetrics(metrics); // API 17 or higher
5458
// display.getRealSize(size);
55-
// display.getMetrics(metrics);
5659

5760
size = new Point();
58-
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
59-
Display display = wm.getDefaultDisplay();
6061
if (Build.VERSION.SDK_INT >= 17) {
6162
display.getRealSize(size);
6263
} else if (Build.VERSION.SDK_INT >= 14) {
@@ -84,6 +85,10 @@ public int getDisplayHeight() {
8485
// return metrics.heightPixels;
8586
}
8687

88+
public float getDisplayDensity() {
89+
return metrics.density;
90+
}
91+
8792
public void setSketch(PApplet sketch) {
8893
// engine.sketch = sketch;
8994
}

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.support.wearable.watchface.CanvasWatchFaceService;
3232
import android.support.wearable.watchface.WatchFaceService;
3333
import android.support.wearable.watchface.WatchFaceStyle;
34+
import android.util.DisplayMetrics;
3435
//import android.util.DisplayMetrics;
3536
import android.view.Display;
3637
import android.view.MotionEvent;
@@ -45,19 +46,19 @@
4546
public class PWatchFaceCanvas extends CanvasWatchFaceService implements AppComponent {
4647
protected PApplet sketch;
4748
protected Point size;
48-
// private DisplayMetrics metrics;
49+
private DisplayMetrics metrics;
4950
protected CEngine engine;
5051

5152
public void initDimensions() {
52-
// metrics = new DisplayMetrics();
53-
// WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
54-
// Display display = wm.getDefaultDisplay();
55-
// display.getRealMetrics(metrics); // only API level 17 o higher
56-
// display.getMetrics(metrics);
57-
58-
size = new Point();
5953
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
6054
Display display = wm.getDefaultDisplay();
55+
metrics = new DisplayMetrics();
56+
display.getMetrics(metrics);
57+
58+
// display.getRealMetrics(metrics); // API 17 or higher
59+
// display.getRealSize(size);
60+
61+
size = new Point();
6162
if (Build.VERSION.SDK_INT >= 17) {
6263
display.getRealSize(size);
6364
} else if (Build.VERSION.SDK_INT >= 14) {
@@ -81,6 +82,10 @@ public int getDisplayHeight() {
8182
// return metrics.heightPixels;
8283
}
8384

85+
public float getDisplayDensity() {
86+
return metrics.density;
87+
}
88+
8489
public int getKind() {
8590
return WATCHFACE;
8691
}

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.support.wearable.watchface.Gles2WatchFaceService;
3232
import android.support.wearable.watchface.WatchFaceService;
3333
import android.support.wearable.watchface.WatchFaceStyle;
34+
import android.util.DisplayMetrics;
3435
//import android.util.DisplayMetrics;
3536
import android.view.MotionEvent;
3637
import android.view.SurfaceHolder;
@@ -43,18 +44,19 @@
4344
public class PWatchFaceGLES extends Gles2WatchFaceService implements AppComponent {
4445
protected PApplet sketch;
4546
protected Point size;
46-
// private DisplayMetrics metrics;
47+
private DisplayMetrics metrics;
4748
protected GLEngine engine;
4849

4950
public void initDimensions() {
50-
// metrics = new DisplayMetrics();
51-
// WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
52-
// Display display = wm.getDefaultDisplay();
53-
// display.getMetrics(metrics);
54-
55-
size = new Point();
5651
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
5752
Display display = wm.getDefaultDisplay();
53+
metrics = new DisplayMetrics();
54+
display.getMetrics(metrics);
55+
56+
// display.getRealMetrics(metrics); // API 17 or higher
57+
// display.getRealSize(size);
58+
59+
size = new Point();
5860
if (Build.VERSION.SDK_INT >= 17) {
5961
display.getRealSize(size);
6062
} else if (Build.VERSION.SDK_INT >= 14) {
@@ -78,6 +80,10 @@ public int getDisplayHeight() {
7880
// return metrics.heightPixels;
7981
}
8082

83+
public float getDisplayDensity() {
84+
return metrics.density;
85+
}
86+
8187
public int getKind() {
8288
return WATCHFACE;
8389
}

core/src/processing/core/PApplet.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,15 @@ public class PApplet extends Object implements PConstants {
160160
public int height = DEFAULT_HEIGHT;
161161

162162
// can't call this because causes an ex, but could set elsewhere
163-
//final float screenDensity = getResources().getDisplayMetrics().density;
163+
/** The logical density of the display from getDisplayMetrics().density
164+
* According to Android's documentation:
165+
* This is a scaling factor for the Density Independent Pixel unit,
166+
* where one DIP is one pixel on an approximately 160 dpi screen
167+
* (for example a 240x320, 1.5"x2" screen), providing the baseline of the
168+
* system's display. Thus on a 160dpi screen this density value will be 1;
169+
* on a 120 dpi screen it would be .75; etc.
170+
*/
171+
public float displayDensity = 1;
164172

165173
/** absolute x position of input on screen */
166174
public int mouseX;
@@ -499,6 +507,8 @@ public void initSurface(LayoutInflater inflater, ViewGroup container,
499507
component.initDimensions();
500508
displayWidth = component.getDisplayWidth();
501509
displayHeight = component.getDisplayHeight();
510+
displayDensity = component.getDisplayDensity();
511+
502512
handleSettings();
503513

504514
if (fullScreen && parentLayout == -1) {

libraries/cardboard/src/processing/cardboard/PCardboard.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public int getDisplayWidth() {
6363
public int getDisplayHeight() {
6464
return metrics.heightPixels;
6565
}
66+
67+
public float getDisplayDensity() {
68+
return metrics.density;
69+
}
6670

6771
public int getKind() {
6872
return CARDBOARD;

0 commit comments

Comments
 (0)