mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-20 07:26:11 +08:00
Merge branch 'master' of github.com:NativeScript/NativeScript
# Conflicts: # apps/automated/src/test-runner.ts # apps/automated/src/ui/bottom-navigation/bottom-navigation-navigation-tests.ts # apps/ui/src/bottom-navigation/events-page.ts # apps/ui/src/main-page.ts # apps/ui/src/test-page-main-view-model.ts # package.json # packages/core/color/color-common.ts # packages/core/color/index.d.ts # packages/core/ui/action-bar/index.android.ts # packages/core/ui/bottom-navigation/index.android.ts # packages/core/ui/core/view/index.ios.ts # packages/core/ui/core/view/view-helper/view-helper-common.ts # packages/core/ui/index.ts # packages/core/ui/styling/background.android.ts # packages/core/ui/tab-navigation-base/tab-strip-item/index.ts # packages/webpack/jasmine-config/reporter.ts
This commit is contained in:
@ -0,0 +1,154 @@
|
||||
package org.nativescript.widgets;
|
||||
|
||||
import android.graphics.BlurMaskFilter;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.graphics.drawable.ShapeDrawable;
|
||||
import android.graphics.drawable.shapes.RectShape;
|
||||
import android.graphics.drawable.shapes.RoundRectShape;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||
public class BoxShadowDrawable extends LayerDrawable {
|
||||
// Static parameters
|
||||
protected final static int DEFAULT_BACKGROUND_COLOR = Color.WHITE;
|
||||
protected final static String TAG = "BoxShadowDrawable";
|
||||
|
||||
// BoxShadow Parameters
|
||||
protected int offsetX = 0;
|
||||
protected int offsetY = 0;
|
||||
protected int blurRadius = 0;
|
||||
protected int spreadRadius = 0;
|
||||
protected int shadowColor = Color.BLACK;
|
||||
|
||||
// Layers
|
||||
protected final ShapeDrawable shadowLayer;
|
||||
protected final ShapeDrawable overlayLayer;
|
||||
protected final Drawable wrappedLayer;
|
||||
|
||||
protected float[] currentCornerRadii;
|
||||
|
||||
public BoxShadowDrawable(Drawable wrappedDrawable, String value) {
|
||||
super(new Drawable[]{});
|
||||
|
||||
Log.d(TAG, "Constructing BoxShadowDrawable!");
|
||||
|
||||
this.shadowLayer = new ShapeDrawable(new RectShape());
|
||||
this.overlayLayer = this.createOverlayLayer();
|
||||
this.wrappedLayer = wrappedDrawable;
|
||||
|
||||
// add our layers
|
||||
this.addLayer(shadowLayer);
|
||||
this.addLayer(overlayLayer);
|
||||
this.addLayer(wrappedLayer);
|
||||
|
||||
this.setValue(value);
|
||||
}
|
||||
|
||||
// to allow applying any bg changes on original Drawable
|
||||
public Drawable getWrappedDrawable() {
|
||||
return this.wrappedLayer;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
try {
|
||||
JSONObject config = new JSONObject(value);
|
||||
offsetX = config.getInt("offsetX");
|
||||
offsetY = config.getInt("offsetY");
|
||||
blurRadius = config.getInt("blurRadius");
|
||||
spreadRadius = config.getInt("spreadRadius");
|
||||
shadowColor = config.getInt("shadowColor");
|
||||
|
||||
float[] outerRadius;
|
||||
|
||||
// if we are wrapping a BorderDrawable - we can get the radii from it
|
||||
if(wrappedLayer instanceof BorderDrawable) {
|
||||
BorderDrawable b = (BorderDrawable) wrappedLayer;
|
||||
outerRadius = new float[]{
|
||||
b.getBorderTopLeftRadius(),
|
||||
b.getBorderTopLeftRadius(),
|
||||
|
||||
b.getBorderTopRightRadius(),
|
||||
b.getBorderTopRightRadius(),
|
||||
|
||||
b.getBorderBottomRightRadius(),
|
||||
b.getBorderBottomRightRadius(),
|
||||
|
||||
b.getBorderBottomLeftRadius(),
|
||||
b.getBorderBottomLeftRadius(),
|
||||
};
|
||||
} else {
|
||||
int cornerRadius = 0;
|
||||
try {
|
||||
cornerRadius = config.getInt("cornerRadius");
|
||||
} catch (JSONException ignore) {}
|
||||
|
||||
outerRadius = new float[8];
|
||||
Arrays.fill(outerRadius, cornerRadius);
|
||||
}
|
||||
|
||||
if(!Arrays.equals(outerRadius, currentCornerRadii)) {
|
||||
Log.d(TAG, "Update layer shape");
|
||||
shadowLayer.setShape(new RoundRectShape(outerRadius, null, null));
|
||||
overlayLayer.setShape(new RoundRectShape(outerRadius, null, null));
|
||||
|
||||
// update current
|
||||
currentCornerRadii = outerRadius;
|
||||
}
|
||||
|
||||
// apply new shadow parameters
|
||||
this.applyShadow();
|
||||
} catch (JSONException exception) {
|
||||
Log.d(TAG, "Caught JSONException...");
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void applyShadow() {
|
||||
Log.d(TAG, "applyShadow: " + this);
|
||||
|
||||
// apply boxShadow
|
||||
shadowLayer.getPaint().setColor(shadowColor);
|
||||
shadowLayer.getPaint().setMaskFilter(new BlurMaskFilter(
|
||||
Float.MIN_VALUE + blurRadius,
|
||||
BlurMaskFilter.Blur.NORMAL
|
||||
));
|
||||
shadowLayer.getPaint().setAntiAlias(true);
|
||||
|
||||
// apply insets that mimic offsets/spread to the shadowLayer
|
||||
int inset = -spreadRadius;
|
||||
Log.d(TAG, "Insets:"
|
||||
+ "\n l: " + (inset + offsetX)
|
||||
+ "\n t: " + (inset + offsetY)
|
||||
+ "\n r: " + (inset - offsetX)
|
||||
+ "\n b: " + (inset - offsetY)
|
||||
);
|
||||
this.setLayerInset(0,
|
||||
inset + offsetX,
|
||||
inset + offsetY,
|
||||
inset - offsetX,
|
||||
inset - offsetY
|
||||
);
|
||||
}
|
||||
|
||||
private ShapeDrawable createOverlayLayer() {
|
||||
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
|
||||
shapeDrawable.getPaint().setColor(DEFAULT_BACKGROUND_COLOR);
|
||||
|
||||
return shapeDrawable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BoxShadowDrawable { oX:" + offsetX + " oY:" + offsetY + " br:" + blurRadius + " sr:" + spreadRadius + " c:" + shadowColor + " }";
|
||||
}
|
||||
}
|
||||
@ -154,11 +154,11 @@ public class StackLayout extends LayoutBase {
|
||||
|
||||
switch (verticalGravity) {
|
||||
case Gravity.CENTER_VERTICAL:
|
||||
childTop = (bottom - top - this._totalLength) / 2 + paddingTop - paddingBottom;
|
||||
childTop = (bottom - top - this._totalLength) / 2 + paddingTop;
|
||||
break;
|
||||
|
||||
case Gravity.BOTTOM:
|
||||
childTop = bottom - top - this._totalLength + paddingTop - paddingBottom;
|
||||
childTop = bottom - top - this._totalLength + paddingTop;
|
||||
break;
|
||||
|
||||
case Gravity.TOP:
|
||||
@ -197,11 +197,11 @@ public class StackLayout extends LayoutBase {
|
||||
|
||||
switch (horizontalGravity) {
|
||||
case Gravity.CENTER_HORIZONTAL:
|
||||
childLeft = (right - left - this._totalLength) / 2 + paddingLeft - paddingRight;
|
||||
childLeft = (right - left - this._totalLength) / 2 + paddingLeft;
|
||||
break;
|
||||
|
||||
case Gravity.RIGHT:
|
||||
childLeft = right - left - this._totalLength + paddingLeft - paddingRight;
|
||||
childLeft = right - left - this._totalLength + paddingLeft;
|
||||
break;
|
||||
|
||||
case Gravity.LEFT:
|
||||
|
||||
@ -0,0 +1,70 @@
|
||||
package org.nativescript.widgets;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class Utils {
|
||||
public static void drawBoxShadow(View view, String value) {
|
||||
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
|
||||
return;
|
||||
}
|
||||
Log.d("BoxShadowDrawable", "drawBoxShadow");
|
||||
|
||||
Drawable currentBg = view.getBackground();
|
||||
|
||||
if(currentBg != null) {
|
||||
Log.d("BoxShadowDrawable", "current BG is: " + currentBg.getClass().getName());
|
||||
}
|
||||
|
||||
if(currentBg == null) {
|
||||
Log.d("BoxShadowDrawable", "view had no background!");
|
||||
currentBg = new ColorDrawable(Color.TRANSPARENT);
|
||||
} else if(currentBg instanceof BoxShadowDrawable) {
|
||||
currentBg = ((BoxShadowDrawable) view.getBackground()).getWrappedDrawable();
|
||||
Log.d("BoxShadowDrawable", "already a BoxShadowDrawable, getting wrapped drawable:" + currentBg.getClass().getName());
|
||||
}
|
||||
|
||||
// replace background
|
||||
Log.d("BoxShadowDrawable", "replacing background with new BoxShadowDrawable...");
|
||||
view.setBackground(new BoxShadowDrawable(currentBg, value));
|
||||
|
||||
Drawable bg = view.getBackground();
|
||||
if(bg != null) {
|
||||
Log.d("BoxShadowDrawable", "new current bg: " + bg.getClass().getName());
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
while (view.getParent() != null && view.getParent() instanceof ViewGroup) {
|
||||
count++;
|
||||
ViewGroup parent = (ViewGroup) view.getParent();
|
||||
parent.setClipChildren(false);
|
||||
parent.setClipToPadding(false);
|
||||
// removing clipping from all breaks the ui
|
||||
if (count == 1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// public static void clearBoxShadow(View view) {
|
||||
// if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
|
||||
// return;
|
||||
// }
|
||||
// Log.d("BoxShadowDrawable", "clearBoxShadow.");
|
||||
//
|
||||
// Drawable bg = view.getBackground();
|
||||
// if(bg != null) {
|
||||
// Log.d("BoxShadowDrawable", "current bg: " + bg.getClass().getName());
|
||||
// }
|
||||
// if(bg instanceof BoxShadowDrawable) {
|
||||
// Drawable original = ((BoxShadowDrawable) view.getBackground()).getWrappedDrawable();
|
||||
// Log.d("BoxShadowDrawable", "BoxShadowDrawable found, resetting to original: " + original.getClass().getName());
|
||||
// view.setBackground(null);
|
||||
//// view.setBackground(original);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user