Skip to content

Commit

Permalink
feat:align Android and iOS treat ImageView's scale type
Browse files Browse the repository at this point in the history
  • Loading branch information
penfeizhou committed Mar 25, 2020
1 parent a7e7e55 commit 0abf853
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
53 changes: 43 additions & 10 deletions doric-android/doric/src/main/java/pub/doric/shader/ImageNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
Expand All @@ -30,14 +31,19 @@
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import jp.wasabeef.glide.transformations.BlurTransformation;
Expand Down Expand Up @@ -68,6 +74,7 @@ public ImageNode(DoricContext doricContext) {
protected ImageView build() {
ImageView imageView = new ImageView(getContext());
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setAdjustViewBounds(true);
return imageView;
}

Expand Down Expand Up @@ -140,6 +147,7 @@ private void loadImageUrl(String url) {
RequestBuilder<Drawable> requestBuilder = Glide.with(getContext())
.load(url);
try {
requestBuilder = requestBuilder.apply(new RequestOptions().override(Target.SIZE_ORIGINAL));
if (isBlur) {
requestBuilder = requestBuilder
.apply(RequestOptions
Expand Down Expand Up @@ -173,15 +181,32 @@ public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Dra
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
if (!TextUtils.isEmpty(loadCallbackId)) {
callJSResponse(loadCallbackId, new JSONBuilder()
.put("width", DoricUtils.px2dp(resource.getIntrinsicWidth()))
.put("height", DoricUtils.px2dp(resource.getIntrinsicHeight()))
.toJSONObject());
if (resource instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
callJSResponse(loadCallbackId, new JSONBuilder()
.put("width", DoricUtils.px2dp(bitmap.getWidth()))
.put("height", DoricUtils.px2dp(bitmap.getHeight()))
.toJSONObject());
} else {
callJSResponse(loadCallbackId, new JSONBuilder()
.put("width", DoricUtils.px2dp(resource.getIntrinsicWidth()))
.put("height", DoricUtils.px2dp(resource.getIntrinsicHeight()))
.toJSONObject());
}
}
return false;
}
})
.into(mView);
}).into(new DrawableImageViewTarget(mView) {
@Override
public void getSize(@NonNull SizeReadyCallback cb) {
cb.onSizeReady(SIZE_ORIGINAL, SIZE_ORIGINAL);
}

@Override
protected void setResource(@Nullable Drawable resource) {
super.setResource(resource);
}
});
}

@Override
Expand Down Expand Up @@ -249,10 +274,18 @@ protected void blend(ImageView view, String name, JSValue prop) {
Drawable drawable = getContext().getResources().getDrawable(resId);
view.setImageResource(resId);
if (!TextUtils.isEmpty(loadCallbackId)) {
callJSResponse(loadCallbackId, new JSONBuilder()
.put("width", DoricUtils.px2dp(drawable.getIntrinsicWidth()))
.put("height", DoricUtils.px2dp(drawable.getIntrinsicHeight()))
.toJSONObject());
if (drawable instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
callJSResponse(loadCallbackId, new JSONBuilder()
.put("width", DoricUtils.px2dp(bitmap.getWidth()))
.put("height", DoricUtils.px2dp(bitmap.getHeight()))
.toJSONObject());
} else {
callJSResponse(loadCallbackId, new JSONBuilder()
.put("width", DoricUtils.px2dp(drawable.getIntrinsicWidth()))
.put("height", DoricUtils.px2dp(drawable.getIntrinsicHeight()))
.toJSONObject());
}
}
} else {
if (!TextUtils.isEmpty(loadCallbackId)) {
Expand Down
49 changes: 48 additions & 1 deletion doric-iOS/Pod/Classes/Shader/DoricImageNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,53 @@
#import "Doric.h"
#import "YYWebImage.h"

@interface DoricImageView : YYAnimatedImageView
@end

@implementation DoricImageView
- (CGSize)measureSize:(CGSize)targetSize {
CGFloat width = self.width;
CGFloat height = self.height;

DoricLayoutConfig *config = self.layoutConfig;
if (!config) {
config = [DoricLayoutConfig new];
}
if (config.widthSpec == DoricLayoutAtMost
|| config.widthSpec == DoricLayoutWrapContent) {
width = targetSize.width - config.margin.left - config.margin.right;
}
if (config.heightSpec == DoricLayoutAtMost
|| config.heightSpec == DoricLayoutWrapContent) {
height = targetSize.height - config.margin.top - config.margin.bottom;
}
DoricPadding padding = self.padding;
CGSize contentSize = [self sizeThatFits:CGSizeMake(
width - padding.left - padding.right,
height - padding.top - padding.bottom)];
if (config.widthSpec == DoricLayoutWrapContent) {
width = contentSize.width + padding.left + padding.right;
if (config.heightSpec != DoricLayoutWrapContent && contentSize.width != 0 && contentSize.height != 0) {
width = contentSize.width / contentSize.height * height + padding.left + padding.right;
}
}
if (config.heightSpec == DoricLayoutWrapContent) {
height = contentSize.height + padding.top + padding.bottom;
if (config.widthSpec != DoricLayoutWrapContent && contentSize.width != 0 && contentSize.height != 0) {
height = contentSize.height / contentSize.width * width + padding.top + padding.bottom;
}
}
if (config.weight) {
if ([self.superview isKindOfClass:[DoricVLayoutView class]]) {
height = self.height;
} else if ([self.superview isKindOfClass:[DoricHLayoutView class]]) {
width = self.width;
}
}
return CGSizeMake(width, height);
}
@end

@interface DoricImageNode ()
@property(nonatomic, copy) NSString *loadCallbackId;
@property(nonatomic, assign) UIViewContentMode contentMode;
Expand All @@ -37,7 +84,7 @@ @interface DoricImageNode ()
@implementation DoricImageNode

- (UIImageView *)build {
return [[YYAnimatedImageView new] also:^(UIImageView *it) {
return [[DoricImageView new] also:^(UIImageView *it) {
it.clipsToBounds = YES;
it.contentMode = UIViewContentModeScaleAspectFill;
}];
Expand Down

0 comments on commit 0abf853

Please sign in to comment.