fix: image inside list flushes when loading more or in other situations

This commit is contained in:
pengfei.zhou 2022-06-02 14:40:04 +08:00 committed by osborn
parent 75151d57d4
commit 531e2d9298
7 changed files with 89 additions and 22 deletions

View File

@ -94,7 +94,7 @@ public class ImageNode extends ViewNode<ImageView> {
private JSObject stretchInset = null; private JSObject stretchInset = null;
private float imageScale = DoricUtils.getScreenScale(); private float imageScale = DoricUtils.getScreenScale();
private Animatable2Compat.AnimationCallback animationCallback = null; private Animatable2Compat.AnimationCallback animationCallback = null;
private int scaleType = 0; private int scaleType = 2;
public ImageNode(DoricContext doricContext) { public ImageNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
@ -170,9 +170,11 @@ public class ImageNode extends ViewNode<ImageView> {
if (loadCallback.isString()) { if (loadCallback.isString()) {
this.loadCallbackId = loadCallback.asString().value(); this.loadCallbackId = loadCallback.asString().value();
} }
} }
super.blend(jsObject); super.blend(jsObject);
if (scaleType == 2 && mView.getScaleType() != ImageView.ScaleType.CENTER_CROP) {
mView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
} }
private Drawable getPlaceHolderDrawable() { private Drawable getPlaceHolderDrawable() {
@ -292,16 +294,20 @@ public class ImageNode extends ViewNode<ImageView> {
} }
})); }));
} }
Drawable placeHolderDrawable = getPlaceHolderDrawable(); Drawable placeHolderDrawable = null;
if (mView.getDrawable() != null) {
placeHolderDrawable = mView.getDrawable();
} else {
placeHolderDrawable = getPlaceHolderDrawable();
}
if (placeHolderDrawable != null) { if (placeHolderDrawable != null) {
requestBuilder = requestBuilder.apply(RequestOptions.placeholderOf(placeHolderDrawable)); requestBuilder = requestBuilder.apply(RequestOptions.placeholderOf(placeHolderDrawable));
} }
Drawable errorDrawable = getErrorDrawable(); Drawable errorDrawable = getErrorDrawable();
if (errorDrawable != null) { if (errorDrawable == null) {
requestBuilder = requestBuilder.apply(RequestOptions.errorOf(errorDrawable)); errorDrawable = new ColorDrawable(Color.TRANSPARENT);
} }
requestBuilder = requestBuilder.apply(RequestOptions.errorOf(errorDrawable));
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
DoricLog.e("ImageNode blend error, please check the glide version"); DoricLog.e("ImageNode blend error, please check the glide version");
@ -361,8 +367,8 @@ public class ImageNode extends ViewNode<ImageView> {
} }
if (scaleType == 3) { // image tile if (scaleType == 3) { // image tile
BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(),bitmap); BitmapDrawable drawable = new BitmapDrawable(getContext().getResources(), bitmap);
drawable.setTileModeXY(Shader.TileMode.REPEAT , Shader.TileMode.REPEAT); drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.setDither(true); drawable.setDither(true);
super.setResource(drawable); super.setResource(drawable);
} else if (stretchInset != null) { } else if (stretchInset != null) {
@ -424,6 +430,7 @@ public class ImageNode extends ViewNode<ImageView> {
public void onError(Throwable t) { public void onError(Throwable t) {
t.printStackTrace(); t.printStackTrace();
DoricLog.e("Cannot load resource %s, %s", resource.toString(), t.getLocalizedMessage()); DoricLog.e("Cannot load resource %s, %s", resource.toString(), t.getLocalizedMessage());
view.setImageDrawable(null);
} }
@Override @Override
@ -575,8 +582,7 @@ public class ImageNode extends ViewNode<ImageView> {
@Override @Override
protected void reset() { protected void reset() {
super.reset(); super.reset();
mView.setImageDrawable(null); scaleType = 2;
mView.setScaleType(ImageView.ScaleType.CENTER_CROP);
loadCallbackId = ""; loadCallbackId = "";
isBlur = false; isBlur = false;
placeHolderImage = null; placeHolderImage = null;

View File

@ -23,6 +23,7 @@ import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.StackNode; import pub.doric.shader.StackNode;
import pub.doric.shader.SuperNode;
/** /**
* @Description: com.github.penfeizhou.doric.widget * @Description: com.github.penfeizhou.doric.widget
@ -35,6 +36,11 @@ public class FlowLayoutItemNode extends StackNode {
public FlowLayoutItemNode(DoricContext doricContext) { public FlowLayoutItemNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
}
@Override
public void init(SuperNode<?> superNode) {
super.init(superNode);
this.mReusable = true; this.mReusable = true;
} }

View File

@ -23,6 +23,7 @@ import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.StackNode; import pub.doric.shader.StackNode;
import pub.doric.shader.SuperNode;
/** /**
* @Description: com.github.penfeizhou.doric.widget * @Description: com.github.penfeizhou.doric.widget
@ -35,6 +36,11 @@ public class ListItemNode extends StackNode {
public ListItemNode(DoricContext doricContext) { public ListItemNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
}
@Override
public void init(SuperNode<?> superNode) {
super.init(superNode);
this.mReusable = true; this.mReusable = true;
} }

View File

@ -168,7 +168,8 @@ public class ListNode extends SuperNode<RecyclerView> implements IDoricScrollabl
@Override @Override
public void blend(JSObject jsObject) { public void blend(JSObject jsObject) {
super.blend(jsObject); super.blend(jsObject);
if (jsObject.propertySet().size() > 1 || !jsObject.propertySet().contains("subviews")) { int limit = jsObject.propertySet().contains("loadMoreView") ? 2 : 1;
if (jsObject.propertySet().size() > limit || !jsObject.propertySet().contains("subviews")) {
if (mView != null) { if (mView != null) {
mView.post(new Runnable() { mView.post(new Runnable() {
@SuppressLint("NotifyDataSetChanged") @SuppressLint("NotifyDataSetChanged")

View File

@ -24,6 +24,7 @@ import com.github.pengfeizhou.jscore.JSValue;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.shader.StackNode; import pub.doric.shader.StackNode;
import pub.doric.shader.SuperNode;
/** /**
* @Description: com.github.penfeizhou.doric.widget * @Description: com.github.penfeizhou.doric.widget
@ -36,7 +37,12 @@ public class SlideItemNode extends StackNode {
public SlideItemNode(DoricContext doricContext) { public SlideItemNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
this.mReusable = true; }
@Override
public void init(SuperNode<?> superNode) {
super.init(superNode);
mReusable = true;
} }
@Override @Override

View File

@ -25,7 +25,13 @@ @implementation DoricBundleResource
NSString *path = [self.bundle bundlePath]; NSString *path = [self.bundle bundlePath];
NSString *fullPath = [path stringByAppendingPathComponent:self.identifier]; NSString *fullPath = [path stringByAppendingPathComponent:self.identifier];
NSData *imgData = [[NSData alloc] initWithContentsOfFile:fullPath]; NSData *imgData = [[NSData alloc] initWithContentsOfFile:fullPath];
if (imgData) {
[result setupResult:imgData]; [result setupResult:imgData];
} else {
[result setupError:[NSException exceptionWithName:@"ResourceLoadError"
reason:@"Load resource error"
userInfo:nil]];
}
return result; return result;
} }
@end @end

View File

@ -92,6 +92,7 @@ @interface DoricImageNode ()
@implementation DoricImageNode @implementation DoricImageNode
- (UIImageView *)build { - (UIImageView *)build {
self.scaleType = 2;
self.imageScale = UIScreen.mainScreen.scale; self.imageScale = UIScreen.mainScreen.scale;
return [[DoricImageView new] also:^(UIImageView *it) { return [[DoricImageView new] also:^(UIImageView *it) {
it.clipsToBounds = YES; it.clipsToBounds = YES;
@ -125,9 +126,15 @@ - (void)blend:(NSDictionary *)props {
self.loadCallbackId = it; self.loadCallbackId = it;
}]; }];
[super blend:props]; [super blend:props];
if(self.scaleType == 2 && self.view.contentMode != UIViewContentModeScaleAspectFill) {
self.view.contentMode = UIViewContentModeScaleAspectFill;
}
} }
- (UIImage *)currentPlaceHolderImage { - (UIImage *)currentPlaceHolderImage {
if (self.view.image) {
return self.view.image;
}
if (self.placeHolderImage) { if (self.placeHolderImage) {
return [UIImage imageNamed:self.placeHolderImage]; return [UIImage imageNamed:self.placeHolderImage];
} }
@ -156,7 +163,7 @@ - (UIImage *)currentPlaceHolderImage {
UIColor *color = DoricColor(self.placeHolderColor); UIColor *color = DoricColor(self.placeHolderColor);
CGRect rect = CGRectMake(0, 0, 1, 1); CGRect rect = CGRectMake(0, 0, 1, 1);
self.view.contentMode = UIViewContentModeScaleToFill; self.view.contentMode = UIViewContentModeScaleToFill;
self.scaleType = 0;
if (@available(iOS 10.0, *)) { if (@available(iOS 10.0, *)) {
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init]; UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = [UIScreen mainScreen].scale; format.scale = [UIScreen mainScreen].scale;
@ -212,7 +219,7 @@ - (UIImage *)currentErrorImage {
UIColor *color = DoricColor(self.errorColor); UIColor *color = DoricColor(self.errorColor);
CGRect rect = CGRectMake(0, 0, 1, 1); CGRect rect = CGRectMake(0, 0, 1, 1);
self.view.contentMode = UIViewContentModeScaleToFill; self.view.contentMode = UIViewContentModeScaleToFill;
self.scaleType = 0;
if (@available(iOS 10.0, *)) { if (@available(iOS 10.0, *)) {
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init]; UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = [UIScreen mainScreen].scale; format.scale = [UIScreen mainScreen].scale;
@ -236,7 +243,36 @@ - (UIImage *)currentErrorImage {
return image; return image;
} }
} }
return self.doricContext.driver.registry.defaultErrorImage; UIImage *image = self.doricContext.driver.registry.defaultErrorImage;
if (!image) {
UIColor *color = UIColor.clearColor;
CGRect rect = CGRectMake(0, 0, 1, 1);
self.view.contentMode = UIViewContentModeScaleToFill;
self.scaleType = 0;
if (@available(iOS 10.0, *)) {
UIGraphicsImageRendererFormat *format = [[UIGraphicsImageRendererFormat alloc] init];
format.scale = [UIScreen mainScreen].scale;
UIGraphicsImageRenderer *render = [[UIGraphicsImageRenderer alloc] initWithSize:rect.size format:format];
UIImage *image = [render imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
CGContextRef context = rendererContext.CGContext;
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
}];
return image;
} else {
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
}
return image;
} }
- (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id)prop { - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id)prop {
@ -287,6 +323,7 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
}]; }];
[asyncResult setExceptionCallback:^(NSException *e) { [asyncResult setExceptionCallback:^(NSException *e) {
DoricLog(@"Cannot load resource %@, %@", prop, e.reason); DoricLog(@"Cannot load resource %@, %@", prop, e.reason);
self.view.image = nil;
}]; }];
} else if ([@"imageUrl" isEqualToString:name]) { } else if ([@"imageUrl" isEqualToString:name]) {
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
@ -712,7 +749,7 @@ - (void)stopAnimating {
- (void)reset { - (void)reset {
[super reset]; [super reset];
self.view.image = nil; self.scaleType = 2;
self.loadCallbackId = nil; self.loadCallbackId = nil;
self.placeHolderColor = nil; self.placeHolderColor = nil;
self.placeHolderImage = nil; self.placeHolderImage = nil;
@ -722,7 +759,6 @@ - (void)reset {
self.errorImageBase64 = nil; self.errorImageBase64 = nil;
self.imageScale = UIScreen.mainScreen.scale; self.imageScale = UIScreen.mainScreen.scale;
self.blurEffectView = nil; self.blurEffectView = nil;
self.view.contentMode = UIViewContentModeScaleAspectFill;
if (self.animationEndCallbackId) { if (self.animationEndCallbackId) {
#if DORIC_USE_YYWEBIMAGE #if DORIC_USE_YYWEBIMAGE
[(DoricImageView *) self.view removeObserver:self forKeyPath:@"currentIsPlayingAnimation" context:nil]; [(DoricImageView *) self.view removeObserver:self forKeyPath:@"currentIsPlayingAnimation" context:nil];