feat:add imagepath and imageRes for Image
This commit is contained in:
parent
7b371ca58b
commit
a6df529f8f
@ -98,12 +98,12 @@ public class ImageNode extends ViewNode<ImageView> {
|
|||||||
|
|
||||||
private Drawable getPlaceHolderDrawable() {
|
private Drawable getPlaceHolderDrawable() {
|
||||||
if (!TextUtils.isEmpty(placeHolderImage)) {
|
if (!TextUtils.isEmpty(placeHolderImage)) {
|
||||||
int resId = getDoricContext().getContext().getResources().getIdentifier(
|
int resId = getContext().getResources().getIdentifier(
|
||||||
placeHolderImage.toLowerCase(),
|
placeHolderImage.toLowerCase(),
|
||||||
"drawable",
|
"drawable",
|
||||||
getDoricContext().getContext().getPackageName());
|
getContext().getPackageName());
|
||||||
if (resId > 0) {
|
if (resId > 0) {
|
||||||
return getDoricContext().getContext().getResources().getDrawable(resId);
|
return getContext().getResources().getDrawable(resId);
|
||||||
} else {
|
} else {
|
||||||
DoricLog.e("Cannot find PlaceHolder Drawable for " + placeHolderImage);
|
DoricLog.e("Cannot find PlaceHolder Drawable for " + placeHolderImage);
|
||||||
return new ColorDrawable(Color.GRAY);
|
return new ColorDrawable(Color.GRAY);
|
||||||
@ -117,12 +117,12 @@ public class ImageNode extends ViewNode<ImageView> {
|
|||||||
|
|
||||||
private Drawable getErrorDrawable() {
|
private Drawable getErrorDrawable() {
|
||||||
if (!TextUtils.isEmpty(errorImage)) {
|
if (!TextUtils.isEmpty(errorImage)) {
|
||||||
int resId = getDoricContext().getContext().getResources().getIdentifier(
|
int resId = getContext().getResources().getIdentifier(
|
||||||
errorImage.toLowerCase(),
|
errorImage.toLowerCase(),
|
||||||
"drawable",
|
"drawable",
|
||||||
getDoricContext().getContext().getPackageName());
|
getContext().getPackageName());
|
||||||
if (resId > 0) {
|
if (resId > 0) {
|
||||||
return getDoricContext().getContext().getResources().getDrawable(resId);
|
return getContext().getResources().getDrawable(resId);
|
||||||
} else {
|
} else {
|
||||||
DoricLog.e("Cannot find Error Drawable for " + errorImage);
|
DoricLog.e("Cannot find Error Drawable for " + errorImage);
|
||||||
return new ColorDrawable(Color.GRAY);
|
return new ColorDrawable(Color.GRAY);
|
||||||
@ -134,55 +134,59 @@ public class ImageNode extends ViewNode<ImageView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadImageUrl(String url) {
|
||||||
|
RequestBuilder<Drawable> requestBuilder = Glide.with(getContext())
|
||||||
|
.load(url);
|
||||||
|
try {
|
||||||
|
if (isBlur) {
|
||||||
|
requestBuilder = requestBuilder
|
||||||
|
.apply(RequestOptions
|
||||||
|
.bitmapTransform(new BlurTransformation(25, 3)));
|
||||||
|
}
|
||||||
|
Drawable placeHolderDrawable = getPlaceHolderDrawable();
|
||||||
|
|
||||||
|
if (placeHolderDrawable != null) {
|
||||||
|
requestBuilder = requestBuilder.apply(RequestOptions.placeholderOf(placeHolderDrawable));
|
||||||
|
}
|
||||||
|
|
||||||
|
Drawable errorDrawable = getErrorDrawable();
|
||||||
|
if (errorDrawable != null) {
|
||||||
|
requestBuilder = requestBuilder.apply(RequestOptions.errorOf(errorDrawable));
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
DoricLog.e("ImageNode blend error, please check the glide version");
|
||||||
|
}
|
||||||
|
|
||||||
|
requestBuilder
|
||||||
|
.listener(new RequestListener<Drawable>() {
|
||||||
|
@Override
|
||||||
|
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
||||||
|
if (!TextUtils.isEmpty(loadCallbackId)) {
|
||||||
|
callJSResponse(loadCallbackId);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.into(mView);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void blend(ImageView view, String name, JSValue prop) {
|
protected void blend(ImageView view, String name, JSValue prop) {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "imageUrl":
|
case "imageUrl":
|
||||||
RequestBuilder<Drawable> requestBuilder = Glide.with(getContext())
|
loadImageUrl(prop.asString().value());
|
||||||
.load(prop.asString().value());
|
|
||||||
try {
|
|
||||||
if (isBlur) {
|
|
||||||
requestBuilder = requestBuilder
|
|
||||||
.apply(RequestOptions
|
|
||||||
.bitmapTransform(new BlurTransformation(25, 3)));
|
|
||||||
}
|
|
||||||
Drawable placeHolderDrawable = getPlaceHolderDrawable();
|
|
||||||
|
|
||||||
if (placeHolderDrawable != null) {
|
|
||||||
requestBuilder = requestBuilder.apply(RequestOptions.placeholderOf(placeHolderDrawable));
|
|
||||||
}
|
|
||||||
|
|
||||||
Drawable errorDrawable = getErrorDrawable();
|
|
||||||
if (errorDrawable != null) {
|
|
||||||
requestBuilder = requestBuilder.apply(RequestOptions.errorOf(errorDrawable));
|
|
||||||
}
|
|
||||||
} catch (Throwable e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
DoricLog.e("ImageNode blend error, please check the glide version");
|
|
||||||
}
|
|
||||||
|
|
||||||
requestBuilder
|
|
||||||
.listener(new RequestListener<Drawable>() {
|
|
||||||
@Override
|
|
||||||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
|
|
||||||
if (!TextUtils.isEmpty(loadCallbackId)) {
|
|
||||||
callJSResponse(loadCallbackId);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@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());
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.into(view);
|
|
||||||
break;
|
break;
|
||||||
case "scaleType":
|
case "scaleType":
|
||||||
int scaleType = prop.asNumber().toInt();
|
int scaleType = prop.asNumber().toInt();
|
||||||
@ -218,6 +222,30 @@ public class ImageNode extends ViewNode<ImageView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "imagePath":
|
||||||
|
String localName = prop.asString().value();
|
||||||
|
loadImageUrl("file:///android_asset/" + localName);
|
||||||
|
break;
|
||||||
|
case "imageRes":
|
||||||
|
int resId = getContext().getResources().getIdentifier(
|
||||||
|
prop.asString().value().toLowerCase(),
|
||||||
|
"drawable",
|
||||||
|
getDoricContext().getContext().getPackageName());
|
||||||
|
if (resId > 0) {
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!TextUtils.isEmpty(loadCallbackId)) {
|
||||||
|
callJSResponse(loadCallbackId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
super.blend(view, name, prop);
|
super.blend(view, name, prop);
|
||||||
break;
|
break;
|
||||||
|
@ -181,6 +181,33 @@ - (void)blendView:(UIImageView *)view forPropName:(NSString *)name propValue:(id
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
} else if ([@"imageRes" isEqualToString:name]) {
|
||||||
|
UIImage *image = [UIImage imageNamed:prop];
|
||||||
|
view.image = image;
|
||||||
|
if (self.loadCallbackId.length > 0) {
|
||||||
|
if (image) {
|
||||||
|
[self callJSResponse:self.loadCallbackId,
|
||||||
|
@{@"width": @(image.size.width), @"height": @(image.size.height)},
|
||||||
|
nil];
|
||||||
|
} else {
|
||||||
|
[self callJSResponse:self.loadCallbackId, nil];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if ([@"imagePath" isEqualToString:name]) {
|
||||||
|
NSString *path = [[NSBundle mainBundle] bundlePath];
|
||||||
|
NSString *fullPath = [path stringByAppendingPathComponent:prop];
|
||||||
|
UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
|
||||||
|
view.image = image;
|
||||||
|
if (self.loadCallbackId.length > 0) {
|
||||||
|
if (image) {
|
||||||
|
[self callJSResponse:self.loadCallbackId,
|
||||||
|
@{@"width": @(image.size.width), @"height": @(image.size.height)},
|
||||||
|
nil];
|
||||||
|
} else {
|
||||||
|
[self callJSResponse:self.loadCallbackId, nil];
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
[super blendView:view forPropName:name propValue:prop];
|
[super blendView:view forPropName:name propValue:prop];
|
||||||
}
|
}
|
||||||
|
@ -1667,7 +1667,11 @@ var Image = /** @class */ (function (_super) {
|
|||||||
__decorate$4([
|
__decorate$4([
|
||||||
Property,
|
Property,
|
||||||
__metadata$4("design:type", String)
|
__metadata$4("design:type", String)
|
||||||
], Image.prototype, "imageBase64", void 0);
|
], Image.prototype, "imagePath", void 0);
|
||||||
|
__decorate$4([
|
||||||
|
Property,
|
||||||
|
__metadata$4("design:type", String)
|
||||||
|
], Image.prototype, "imageRes", void 0);
|
||||||
__decorate$4([
|
__decorate$4([
|
||||||
Property,
|
Property,
|
||||||
__metadata$4("design:type", Number)
|
__metadata$4("design:type", Number)
|
||||||
|
@ -1233,7 +1233,11 @@ __decorate$4([
|
|||||||
__decorate$4([
|
__decorate$4([
|
||||||
Property,
|
Property,
|
||||||
__metadata$4("design:type", String)
|
__metadata$4("design:type", String)
|
||||||
], Image.prototype, "imageBase64", void 0);
|
], Image.prototype, "imagePath", void 0);
|
||||||
|
__decorate$4([
|
||||||
|
Property,
|
||||||
|
__metadata$4("design:type", String)
|
||||||
|
], Image.prototype, "imageRes", void 0);
|
||||||
__decorate$4([
|
__decorate$4([
|
||||||
Property,
|
Property,
|
||||||
__metadata$4("design:type", Number)
|
__metadata$4("design:type", Number)
|
||||||
|
@ -2692,7 +2692,11 @@ __decorate$4([
|
|||||||
__decorate$4([
|
__decorate$4([
|
||||||
Property,
|
Property,
|
||||||
__metadata$4("design:type", String)
|
__metadata$4("design:type", String)
|
||||||
], Image.prototype, "imageBase64", void 0);
|
], Image.prototype, "imagePath", void 0);
|
||||||
|
__decorate$4([
|
||||||
|
Property,
|
||||||
|
__metadata$4("design:type", String)
|
||||||
|
], Image.prototype, "imageRes", void 0);
|
||||||
__decorate$4([
|
__decorate$4([
|
||||||
Property,
|
Property,
|
||||||
__metadata$4("design:type", Number)
|
__metadata$4("design:type", Number)
|
||||||
|
15
doric-js/index.d.ts
vendored
15
doric-js/index.d.ts
vendored
@ -483,6 +483,18 @@ declare module 'doric/lib/src/widget/image' {
|
|||||||
}
|
}
|
||||||
export interface IImage extends IView {
|
export interface IImage extends IView {
|
||||||
imageUrl?: string;
|
imageUrl?: string;
|
||||||
|
/**
|
||||||
|
* Read image from local path
|
||||||
|
* For android,it based on assets dir.
|
||||||
|
* For iOS,it based on main bundle dir.
|
||||||
|
*/
|
||||||
|
imagePath?: string;
|
||||||
|
/**
|
||||||
|
* Read image from resource
|
||||||
|
* For android,it will try to read from drawable.
|
||||||
|
* For iOS,it will try to read from Image.Assets.
|
||||||
|
*/
|
||||||
|
imageRes?: string;
|
||||||
imageBase64?: string;
|
imageBase64?: string;
|
||||||
scaleType?: ScaleType;
|
scaleType?: ScaleType;
|
||||||
isBlur?: boolean;
|
isBlur?: boolean;
|
||||||
@ -497,7 +509,8 @@ declare module 'doric/lib/src/widget/image' {
|
|||||||
}
|
}
|
||||||
export class Image extends View implements IImage {
|
export class Image extends View implements IImage {
|
||||||
imageUrl?: string;
|
imageUrl?: string;
|
||||||
imageBase64?: string;
|
imagePath?: string;
|
||||||
|
imageRes?: string;
|
||||||
scaleType?: ScaleType;
|
scaleType?: ScaleType;
|
||||||
isBlur?: boolean;
|
isBlur?: boolean;
|
||||||
/**
|
/**
|
||||||
|
15
doric-js/lib/src/widget/image.d.ts
vendored
15
doric-js/lib/src/widget/image.d.ts
vendored
@ -7,6 +7,18 @@ export declare enum ScaleType {
|
|||||||
}
|
}
|
||||||
export interface IImage extends IView {
|
export interface IImage extends IView {
|
||||||
imageUrl?: string;
|
imageUrl?: string;
|
||||||
|
/**
|
||||||
|
* Read image from local path
|
||||||
|
* For android,it based on assets dir.
|
||||||
|
* For iOS,it based on main bundle dir.
|
||||||
|
*/
|
||||||
|
imagePath?: string;
|
||||||
|
/**
|
||||||
|
* Read image from resource
|
||||||
|
* For android,it will try to read from drawable.
|
||||||
|
* For iOS,it will try to read from Image.Assets.
|
||||||
|
*/
|
||||||
|
imageRes?: string;
|
||||||
imageBase64?: string;
|
imageBase64?: string;
|
||||||
scaleType?: ScaleType;
|
scaleType?: ScaleType;
|
||||||
isBlur?: boolean;
|
isBlur?: boolean;
|
||||||
@ -21,7 +33,8 @@ export interface IImage extends IView {
|
|||||||
}
|
}
|
||||||
export declare class Image extends View implements IImage {
|
export declare class Image extends View implements IImage {
|
||||||
imageUrl?: string;
|
imageUrl?: string;
|
||||||
imageBase64?: string;
|
imagePath?: string;
|
||||||
|
imageRes?: string;
|
||||||
scaleType?: ScaleType;
|
scaleType?: ScaleType;
|
||||||
isBlur?: boolean;
|
isBlur?: boolean;
|
||||||
/**
|
/**
|
||||||
|
@ -40,7 +40,11 @@ __decorate([
|
|||||||
__decorate([
|
__decorate([
|
||||||
Property,
|
Property,
|
||||||
__metadata("design:type", String)
|
__metadata("design:type", String)
|
||||||
], Image.prototype, "imageBase64", void 0);
|
], Image.prototype, "imagePath", void 0);
|
||||||
|
__decorate([
|
||||||
|
Property,
|
||||||
|
__metadata("design:type", String)
|
||||||
|
], Image.prototype, "imageRes", void 0);
|
||||||
__decorate([
|
__decorate([
|
||||||
Property,
|
Property,
|
||||||
__metadata("design:type", Number)
|
__metadata("design:type", Number)
|
||||||
|
@ -25,6 +25,20 @@ export enum ScaleType {
|
|||||||
|
|
||||||
export interface IImage extends IView {
|
export interface IImage extends IView {
|
||||||
imageUrl?: string
|
imageUrl?: string
|
||||||
|
/**
|
||||||
|
* Read image from local path
|
||||||
|
* For android,it based on assets dir.
|
||||||
|
* For iOS,it based on main bundle dir.
|
||||||
|
*/
|
||||||
|
imagePath?: string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read image from resource
|
||||||
|
* For android,it will try to read from drawable.
|
||||||
|
* For iOS,it will try to read from Image.Assets.
|
||||||
|
*/
|
||||||
|
imageRes?: string
|
||||||
|
|
||||||
imageBase64?: string
|
imageBase64?: string
|
||||||
scaleType?: ScaleType
|
scaleType?: ScaleType
|
||||||
isBlur?: boolean
|
isBlur?: boolean
|
||||||
@ -40,7 +54,10 @@ export class Image extends View implements IImage {
|
|||||||
imageUrl?: string
|
imageUrl?: string
|
||||||
|
|
||||||
@Property
|
@Property
|
||||||
imageBase64?: string
|
imagePath?: string
|
||||||
|
|
||||||
|
@Property
|
||||||
|
imageRes?: string
|
||||||
|
|
||||||
@Property
|
@Property
|
||||||
scaleType?: ScaleType
|
scaleType?: ScaleType
|
||||||
|
Reference in New Issue
Block a user