feat:add Image support base64 for Android

This commit is contained in:
pengfei.zhou 2019-11-25 11:13:34 +08:00
parent 844d44a6f0
commit bc12ba9ac1
3 changed files with 40 additions and 1 deletions

View File

@ -15,11 +15,15 @@
*/ */
package pub.doric.shader; package pub.doric.shader;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64;
import android.widget.ImageView; import android.widget.ImageView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
@ -34,6 +38,9 @@ import pub.doric.extension.bridge.DoricPlugin;
import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* @Description: com.github.penfeizhou.doric.widget * @Description: com.github.penfeizhou.doric.widget
* @Author: pengfei.zhou * @Author: pengfei.zhou
@ -96,6 +103,23 @@ public class ImageNode extends ViewNode<ImageView> {
case "loadCallback": case "loadCallback":
this.loadCallbackId = prop.asString().value(); this.loadCallbackId = prop.asString().value();
break; break;
case "imageBase64":
Pattern r = Pattern.compile("data:image/(\\S+?);base64,(\\S+)");
Matcher m = r.matcher(prop.asString().value());
if (m.find()) {
String imageType = m.group(1);
String base64 = m.group(2);
if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) {
try {
byte[] data = Base64.decode(base64, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
view.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
break;
default: default:
super.blend(view, name, prop); super.blend(view, name, prop);
break; break;

File diff suppressed because one or more lines are too long

View File

@ -50,6 +50,7 @@ export enum ScaleType {
export interface IImage extends IView { export interface IImage extends IView {
imageUrl?: string imageUrl?: string
imageBase64?: string
scaleType?: ScaleType scaleType?: ScaleType
loadCallback?: (image: { width: number; height: number } | undefined) => void loadCallback?: (image: { width: number; height: number } | undefined) => void
} }
@ -57,7 +58,8 @@ export interface IImage extends IView {
export class Image extends View implements IImage { export class Image extends View implements IImage {
@Property @Property
imageUrl?: string imageUrl?: string
@Property
imageBase64?: string
@Property @Property
scaleType?: ScaleType scaleType?: ScaleType