Skip to content

Commit c04f195

Browse files
author
farfromrefug
committed
chore(android): code for vector drawables
1 parent be25828 commit c04f195

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

src/image/index.android.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,29 @@ function getUri(src: string | ImageAsset, asNative = true) {
128128
return asNative ? android.net.Uri.parse(uri) : uri;
129129
}
130130

131+
function isVectorDrawable(context: android.content.Context, resId: number) {
132+
const resources = context.getResources();
133+
// VectorDrawable resources are usually stored as "drawable" in XML format
134+
const value = new android.util.TypedValue();
135+
resources.getValue(resId, value, true);
136+
if (value.string.toString().endsWith('.xml')) {
137+
// It's most likely a VectorDrawable
138+
return true;
139+
}
140+
// If it's not a vector, it's probably a BitmapDrawable or another type
141+
return false;
142+
}
143+
function getBitmapFromVectorDrawable(context: android.content.Context, drawableId) {
144+
const drawable = Utils.android.getApplicationContext().getDrawable(drawableId);
145+
const bitmap = android.graphics.Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), android.graphics.Bitmap.Config.ARGB_8888);
146+
const canvas = new android.graphics.Canvas(bitmap);
147+
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
148+
drawable.draw(canvas);
149+
console.log('getBitmapFromVectorDrawable', bitmap, bitmap.getWidth(), bitmap.getHeight);
150+
151+
return new android.graphics.drawable.BitmapDrawable(context.getResources(), bitmap);
152+
}
153+
131154
export class ImagePipeline {
132155
private _android: com.facebook.imagepipeline.core.ImagePipeline;
133156

@@ -596,21 +619,30 @@ export class Img extends ImageBase {
596619
return;
597620
}
598621
if (src) {
599-
let drawable: android.graphics.drawable.BitmapDrawable;
600-
if (src instanceof ImageSource) {
622+
let drawable: android.graphics.drawable.Drawable;
623+
if (typeof src === 'string') {
624+
// disabled for now: loading vector drawables
625+
// if (src.indexOf(Utils.RESOURCE_PREFIX) === 0) {
626+
// const identifier = Utils.android.resources.getDrawableId(src.substring(Utils.RESOURCE_PREFIX.length));
627+
// if (identifier >= 0 && isVectorDrawable(this._context, identifier)) {
628+
// drawable = getBitmapFromVectorDrawable(this._context, identifier);
629+
// }
630+
// } else
631+
if (Utils.isFontIconURI(src)) {
632+
const fontIconCode = src.split('//')[1];
633+
if (fontIconCode !== undefined) {
634+
// support sync mode only
635+
const font = this.style.fontInternal;
636+
const color = this.style.color;
637+
drawable = new android.graphics.drawable.BitmapDrawable(
638+
Utils.android.getApplicationContext().getResources(),
639+
ImageSource.fromFontIconCodeSync(fontIconCode, font, color).android
640+
);
641+
}
642+
}
643+
} else if (src instanceof ImageSource) {
601644
drawable = new android.graphics.drawable.BitmapDrawable(Utils.android.getApplicationContext().getResources(), src.android as android.graphics.Bitmap);
602645
this.updateViewSize(src.android);
603-
} else if (Utils.isFontIconURI(src as string)) {
604-
const fontIconCode = (src as string).split('//')[1];
605-
if (fontIconCode !== undefined) {
606-
// support sync mode only
607-
const font = this.style.fontInternal;
608-
const color = this.style.color;
609-
drawable = new android.graphics.drawable.BitmapDrawable(
610-
Utils.android.getApplicationContext().getResources(),
611-
ImageSource.fromFontIconCodeSync(fontIconCode, font, color).android
612-
);
613-
}
614646
}
615647
if (drawable) {
616648
const hierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy = this.nativeImageViewProtected.getHierarchy();

0 commit comments

Comments
 (0)