feat:add onFrameChangedCallback for DoricPanel

This commit is contained in:
pengfei.zhou 2020-03-13 19:17:10 +08:00 committed by osborn
parent aad9029784
commit aecccabe84
6 changed files with 57 additions and 13 deletions

View File

@ -27,6 +27,8 @@ import androidx.lifecycle.OnLifecycleEvent;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import com.github.penfeizhou.animation.decode.Frame;
import pub.doric.utils.DoricUtils; import pub.doric.utils.DoricUtils;
/** /**
@ -37,6 +39,9 @@ import pub.doric.utils.DoricUtils;
public class DoricPanel extends FrameLayout implements LifecycleObserver { public class DoricPanel extends FrameLayout implements LifecycleObserver {
private DoricContext mDoricContext; private DoricContext mDoricContext;
private FrameChangedListener frameChangedListener;
private int renderedWidth = -1;
private int renderedHeight = -1;
public DoricPanel(@NonNull Context context) { public DoricPanel(@NonNull Context context) {
this(context, null); this(context, null);
@ -80,9 +85,20 @@ public class DoricPanel extends FrameLayout implements LifecycleObserver {
@Override @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); super.onSizeChanged(w, h, oldw, oldh);
if (oldw != w || oldh != h) { if (mDoricContext != null) {
if (mDoricContext != null) { if (w != renderedWidth || h != renderedHeight) {
mDoricContext.init(DoricUtils.px2dp(w), DoricUtils.px2dp(h)); if (renderedWidth == oldw && renderedHeight == oldh) {
//Changed by doric
if (frameChangedListener != null) {
frameChangedListener.onFrameChanged(w, h);
}
renderedWidth = w;
renderedHeight = h;
} else {
mDoricContext.init(DoricUtils.px2dp(w), DoricUtils.px2dp(h));
renderedWidth = w;
renderedHeight = h;
}
} }
} }
} }
@ -107,4 +123,12 @@ public class DoricPanel extends FrameLayout implements LifecycleObserver {
mDoricContext.teardown(); mDoricContext.teardown();
} }
} }
public interface FrameChangedListener {
void onFrameChanged(int width, int height);
}
public void setFrameChangedListener(FrameChangedListener listener) {
this.frameChangedListener = listener;
}
} }

View File

@ -492,6 +492,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
break; break;
default: default:
layoutParams.width = Math.max(0, layoutParams.width);
break; break;
} }
} }
@ -504,6 +505,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
break; break;
default: default:
layoutParams.height = Math.max(0, layoutParams.height);
break; break;
} }
} }

View File

@ -24,6 +24,7 @@
@interface DoricPanel : UIViewController @interface DoricPanel : UIViewController
@property(nonatomic, strong) DoricContext *doricContext; @property(nonatomic, strong) DoricContext *doricContext;
@property(nonatomic, strong) void (^frameChangedBlock)(CGSize frameSize);
- (void)config:(NSString *)script alias:(NSString *)alias extra:(NSString *)extra; - (void)config:(NSString *)script alias:(NSString *)alias extra:(NSString *)extra;
@end @end

View File

@ -20,9 +20,9 @@
#import "DoricPanel.h" #import "DoricPanel.h"
#import "Doric.h" #import "Doric.h"
@interface DoricPanel() @interface DoricPanel ()
@property(nonatomic,assign) CGFloat renderedWidth; @property(nonatomic, assign) CGFloat renderedWidth;
@property(nonatomic,assign) CGFloat renderedHeight; @property(nonatomic, assign) CGFloat renderedHeight;
@end @end
@implementation DoricPanel @implementation DoricPanel
@ -41,13 +41,22 @@ - (void)config:(NSString *)script alias:(NSString *)alias extra:(NSString *)extr
- (void)viewWillLayoutSubviews { - (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews]; [super viewWillLayoutSubviews];
[self.doricContext.rootNode.view also:^(DoricStackView *it) { if (self.doricContext && self.renderedWidth != self.view.width && self.renderedHeight != self.view.height) {
if (it.width != self.renderedWidth || it.height != self.renderedHeight) { self.renderedWidth = self.view.width;
[self.doricContext initContextWithWidth:it.width height:it.height]; self.renderedHeight = self.view.height;
self.renderedWidth = it.width; [self.doricContext initContextWithWidth:self.renderedWidth height:self.renderedHeight];
self.renderedHeight = it.height; } else {
} [self.doricContext.rootNode.view also:^(DoricStackView *it) {
}]; if (it.width != self.renderedWidth || it.height != self.renderedHeight) {
// Frame changed
self.renderedWidth = self.view.width = it.width;
self.renderedHeight = self.view.height = it.height;
if (self.frameChangedBlock) {
self.frameChangedBlock(CGSizeMake(it.width, it.height));
}
}
}];
}
} }
- (void)viewDidAppear:(BOOL)animated { - (void)viewDidAppear:(BOOL)animated {

View File

@ -1676,6 +1676,10 @@ var Image = /** @class */ (function (_super) {
Property, Property,
__metadata$4("design:type", String) __metadata$4("design:type", String)
], Image.prototype, "imageRes", void 0); ], Image.prototype, "imageRes", void 0);
__decorate$4([
Property,
__metadata$4("design:type", String)
], Image.prototype, "imageBase64", void 0);
__decorate$4([ __decorate$4([
Property, Property,
__metadata$4("design:type", Number) __metadata$4("design:type", Number)

View File

@ -2759,6 +2759,10 @@ __decorate$4([
Property, Property,
__metadata$4("design:type", String) __metadata$4("design:type", String)
], Image.prototype, "imageRes", void 0); ], Image.prototype, "imageRes", void 0);
__decorate$4([
Property,
__metadata$4("design:type", String)
], Image.prototype, "imageBase64", void 0);
__decorate$4([ __decorate$4([
Property, Property,
__metadata$4("design:type", Number) __metadata$4("design:type", Number)