44import android .app .Activity ;
55import android .content .Context ;
66import android .content .Intent ;
7+ import android .content .res .Resources ;
78import android .graphics .Color ;
89import android .os .Bundle ;
910import android .view .Gravity ;
@@ -79,6 +80,9 @@ private WebView createWebView(String uri, int width, int height) {
7980 */
8081 protected static void showActivity (@ NonNull Context context , @ NonNull TransparentActivityConfig config ) {
8182 assert context != null ;
83+ int screenHeight = Resources .getSystem ().getDisplayMetrics ().heightPixels ;
84+ int screenWidth = Resources .getSystem ().getDisplayMetrics ().widthPixels ;
85+ calculateSize (screenWidth , screenHeight , config );
8286
8387 Intent intent = new Intent (context , TransparentActivity .class );
8488 intent .putExtra (X_KEY , config .x );
@@ -89,4 +93,52 @@ protected static void showActivity(@NonNull Context context, @NonNull Transparen
8993
9094 context .startActivity (intent );
9195 }
96+
97+ private static void calculateSize (int screenWidth , int screenHeight , TransparentActivityConfig config ) {
98+ if (config .xPercent != null ) {
99+ config .x = (int ) (screenWidth * adjustPercent (config .xPercent ));
100+ }
101+ if (config .yPercent != null ) {
102+ config .y = (int ) (screenHeight * adjustPercent (config .yPercent ));
103+ }
104+
105+ int remainingWidth = screenWidth - (config .x != null ? config .x : 0 );
106+ int remainingHeight = screenHeight - (config .y != null ? config .y : 0 );
107+
108+ if (config .widthPercent != null ) {
109+ config .width = (int ) (remainingWidth * adjustPercent (config .widthPercent ));
110+ config .width = Math .min (config .width , remainingWidth );
111+ }
112+
113+ if (config .heightPercent != null ) {
114+ config .height = (int ) (remainingHeight * adjustPercent (config .heightPercent ));
115+ config .height = Math .min (config .height , remainingHeight );
116+ }
117+
118+ //fallback to remaining screen
119+ if (config .width == null ) {
120+ config .width = remainingWidth ;
121+ }
122+ if (config .height == null ) {
123+ config .height = remainingHeight ;
124+ }
125+
126+ //fallback to top left corner
127+ if (config .x == null ) {
128+ config .x = 0 ;
129+ }
130+ if (config .y == null ) {
131+ config .y = 0 ;
132+ }
133+ }
134+
135+ private static Double adjustPercent (Double percent ) {
136+ if (percent > 1 || percent < -1 ) {
137+ percent = percent % 1 ;
138+ }
139+ if (percent < 0 ) {
140+ percent = 1 + percent ;
141+ }
142+ return percent ;
143+ }
92144}
0 commit comments