diff --git a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java index 20b56d0b..89facfc2 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java @@ -68,7 +68,7 @@ public class ImageNode extends ViewNode { private String errorImage; private int placeHolderColor = Color.TRANSPARENT; private int errorColor = Color.TRANSPARENT; - private Rect stretchInset = null; + private JSObject stretchInset = null; public ImageNode(DoricContext doricContext) { super(doricContext); @@ -237,11 +237,23 @@ public class ImageNode extends ViewNode { Bitmap bitmap = ((BitmapDrawable) resource).getBitmap(); if (stretchInset != null) { + float left = stretchInset.getProperty("left").asNumber().toFloat(); + float top = stretchInset.getProperty("top").asNumber().toFloat(); + float right = stretchInset.getProperty("right").asNumber().toFloat(); + float bottom = stretchInset.getProperty("bottom").asNumber().toFloat(); + + Rect rect = new Rect( + (int) (bitmap.getWidth() * left), + (int) (bitmap.getHeight() * top), + (int) (bitmap.getWidth() * (1 - right)), + (int) (bitmap.getHeight() * (1 - bottom)) + ); + NinePatchDrawable ninePatchDrawable = new NinePatchDrawable( getContext().getResources(), bitmap, - DoricUtils.getNinePatchChunk(stretchInset), - stretchInset, + DoricUtils.getNinePatchChunk(rect), + rect, null ); super.setResource(ninePatchDrawable); @@ -332,11 +344,7 @@ public class ImageNode extends ViewNode { break; case "stretchInset": if (prop.isObject()) { - int left = prop.asObject().getProperty("left").asNumber().toInt(); - int top = prop.asObject().getProperty("top").asNumber().toInt(); - int right = prop.asObject().getProperty("right").asNumber().toInt(); - int bottom = prop.asObject().getProperty("bottom").asNumber().toInt(); - stretchInset = new Rect(left, top, right, bottom); + stretchInset = prop.asObject(); } break; default: diff --git a/doric-demo/src/ImageDemo.ts b/doric-demo/src/ImageDemo.ts index 36a7f160..621e1ecb 100644 --- a/doric-demo/src/ImageDemo.ts +++ b/doric-demo/src/ImageDemo.ts @@ -119,9 +119,9 @@ class ImageDemo extends Panel { scaleType: ScaleType.ScaleToFill, layoutConfig: layoutConfig().just(), stretchInset: { - left: 125, + left: 0.85, top: 0, - right: 22, + right: 0.149, bottom: 0 } }) diff --git a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m index c33a2921..04824c26 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricImageNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricImageNode.m @@ -240,7 +240,7 @@ - (void)afterBlended:(NSDictionary *)props { CGFloat top = [self.stretchInsetDic[@"top"] floatValue]; CGFloat right = [self.stretchInsetDic[@"right"] floatValue]; CGFloat bottom = [self.stretchInsetDic[@"bottom"] floatValue]; - UIImage *result = [self.view.image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch]; + UIImage *result = [self.view.image resizableImageWithCapInsets:UIEdgeInsetsMake(top * self.view.image.size.height, left * self.view.image.size.width, bottom * self.view.image.size.height, right * self.view.image.size.width) resizingMode:UIImageResizingModeStretch]; self.view.image = result; } }