change px to proportion

This commit is contained in:
王劲鹏 2020-04-17 15:18:01 +08:00 committed by osborn
parent 36eaf35cd0
commit 4c18171a2b
3 changed files with 19 additions and 11 deletions

View File

@ -68,7 +68,7 @@ public class ImageNode extends ViewNode<ImageView> {
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<ImageView> {
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<ImageView> {
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:

View File

@ -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
}
})

View File

@ -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;
}
}