mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-19 06:10:56 +08:00
fix(android): refactor to make less calls to native
This commit is contained in:
@ -63,6 +63,9 @@ const statePressed = 16842919; // android.R.attr.state_pressed
|
|||||||
const stateEnabled = 16842910; // android.R.attr.state_enabled
|
const stateEnabled = 16842910; // android.R.attr.state_enabled
|
||||||
const styleAnimationDialog = 16973826; // android.R.style.Animation_Dialog
|
const styleAnimationDialog = 16973826; // android.R.style.Animation_Dialog
|
||||||
|
|
||||||
|
const VERTICAL_GRAVITY_MASK = 112; // android.view.Gravity.VERTICAL_GRAVITY_MASK
|
||||||
|
const HORIZONTAL_GRAVITY_MASK = 7; // android.view.Gravity.HORIZONTAL_GRAVITY_MASK
|
||||||
|
|
||||||
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
|
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
|
||||||
|
|
||||||
const modalMap = new Map<number, DialogOptions>();
|
const modalMap = new Map<number, DialogOptions>();
|
||||||
@ -884,30 +887,32 @@ export class View extends ViewCommon {
|
|||||||
[horizontalAlignmentProperty.setNative](value: HorizontalAlignment) {
|
[horizontalAlignmentProperty.setNative](value: HorizontalAlignment) {
|
||||||
const nativeView = this.nativeViewProtected;
|
const nativeView = this.nativeViewProtected;
|
||||||
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
|
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
|
||||||
|
const gravity = lp.gravity;
|
||||||
|
const weight = lp.weight;
|
||||||
// Set only if params gravity exists.
|
// Set only if params gravity exists.
|
||||||
if (lp.gravity !== undefined) {
|
if (lp.gravity !== undefined) {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case 'left':
|
case 'left':
|
||||||
lp.gravity = android.view.Gravity.LEFT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
|
lp.gravity = 3 | (gravity & VERTICAL_GRAVITY_MASK); // android.view.Gravity.LEFT
|
||||||
if (lp.weight < 0) {
|
if (weight < 0) {
|
||||||
lp.weight = -2;
|
lp.weight = -2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'center':
|
case 'center':
|
||||||
lp.gravity = android.view.Gravity.CENTER_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
|
lp.gravity = 1 | (gravity & VERTICAL_GRAVITY_MASK); // android.view.Gravity.CENTER_HORIZONTAL
|
||||||
if (lp.weight < 0) {
|
if (weight < 0) {
|
||||||
lp.weight = -2;
|
lp.weight = -2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'right':
|
case 'right':
|
||||||
lp.gravity = android.view.Gravity.RIGHT | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
|
lp.gravity = 5 | (gravity & VERTICAL_GRAVITY_MASK); // android.view.Gravity.RIGHT
|
||||||
if (lp.weight < 0) {
|
if (weight < 0) {
|
||||||
lp.weight = -2;
|
lp.weight = -2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'stretch':
|
case 'stretch':
|
||||||
lp.gravity = android.view.Gravity.FILL_HORIZONTAL | (lp.gravity & android.view.Gravity.VERTICAL_GRAVITY_MASK);
|
lp.gravity = 7 | (gravity & VERTICAL_GRAVITY_MASK); // android.view.Gravity.FILL_HORIZONTAL
|
||||||
if (lp.weight < 0) {
|
if (weight < 0) {
|
||||||
lp.weight = -1;
|
lp.weight = -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -922,30 +927,32 @@ export class View extends ViewCommon {
|
|||||||
[verticalAlignmentProperty.setNative](value: VerticalAlignment) {
|
[verticalAlignmentProperty.setNative](value: VerticalAlignment) {
|
||||||
const nativeView = this.nativeViewProtected;
|
const nativeView = this.nativeViewProtected;
|
||||||
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
|
const lp: any = nativeView.getLayoutParams() || new org.nativescript.widgets.CommonLayoutParams();
|
||||||
|
const gravity = lp.gravity;
|
||||||
|
const height = lp.height;
|
||||||
// Set only if params gravity exists.
|
// Set only if params gravity exists.
|
||||||
if (lp.gravity !== undefined) {
|
if (gravity !== undefined) {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case 'top':
|
case 'top':
|
||||||
lp.gravity = android.view.Gravity.TOP | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
|
lp.gravity = 48 | (gravity & HORIZONTAL_GRAVITY_MASK); // android.view.Gravity.TOP
|
||||||
if (lp.height < 0) {
|
if (height < 0) {
|
||||||
lp.height = -2;
|
lp.height = -2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'middle':
|
case 'middle':
|
||||||
lp.gravity = android.view.Gravity.CENTER_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
|
lp.gravity = 16 | (gravity & HORIZONTAL_GRAVITY_MASK); // android.view.Gravity.CENTER_VERTICAL
|
||||||
if (lp.height < 0) {
|
if (height < 0) {
|
||||||
lp.height = -2;
|
lp.height = -2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'bottom':
|
case 'bottom':
|
||||||
lp.gravity = android.view.Gravity.BOTTOM | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
|
lp.gravity = 80 | (gravity & HORIZONTAL_GRAVITY_MASK); // android.view.Gravity.BOTTOM
|
||||||
if (lp.height < 0) {
|
if (height < 0) {
|
||||||
lp.height = -2;
|
lp.height = -2;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'stretch':
|
case 'stretch':
|
||||||
lp.gravity = android.view.Gravity.FILL_VERTICAL | (lp.gravity & android.view.Gravity.HORIZONTAL_GRAVITY_MASK);
|
lp.gravity = 112 | (gravity & HORIZONTAL_GRAVITY_MASK); // android.view.Gravity.FILL_VERTICAL
|
||||||
if (lp.height < 0) {
|
if (height < 0) {
|
||||||
lp.height = -1;
|
lp.height = -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user