Skip to content

Commit 6a46cb0

Browse files
committed
fix: native android view class to get faster!
1 parent 916e347 commit 6a46cb0

File tree

3 files changed

+85
-45
lines changed

3 files changed

+85
-45
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.nativescript.image;
2+
3+
import com.facebook.drawee.view.SimpleDraweeView;
4+
5+
import android.view.View.MeasureSpec;
6+
import android.content.Context;
7+
8+
public class DraweeView extends SimpleDraweeView {
9+
public int imageWidth = 0;
10+
public int imageHeight = 0;
11+
12+
DraweeView(Context context) {
13+
super(context);
14+
}
15+
@Override
16+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
17+
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
18+
int width = MeasureSpec.getSize(widthMeasureSpec);
19+
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
20+
int height = MeasureSpec.getSize(heightMeasureSpec);
21+
final float aspectRatio = this.getAspectRatio();
22+
if (aspectRatio > 0) {
23+
boolean finiteWidth = widthMode == android.view.View.MeasureSpec.EXACTLY;
24+
boolean finiteHeight = heightMode == android.view.View.MeasureSpec.EXACTLY;
25+
if (imageWidth != 0 && imageHeight != 0) {
26+
if (!finiteWidth) {
27+
widthMeasureSpec = android.view.View.MeasureSpec.makeMeasureSpec((int)(height * aspectRatio), android.view.View.MeasureSpec.EXACTLY);
28+
}
29+
if (!finiteHeight) {
30+
heightMeasureSpec = android.view.View.MeasureSpec.makeMeasureSpec((int)(width / aspectRatio), android.view.View.MeasureSpec.EXACTLY);
31+
}
32+
}
33+
}
34+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
35+
}
36+
}

src/image.android.ts

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -332,49 +332,49 @@ export const needRequestImage = function (target: any, propertyKey: string | Sym
332332
};
333333
};
334334

335-
type DraweeView = new (owner: Img, context) => com.facebook.drawee.view.SimpleDraweeView;
336-
// eslint-disable-next-line no-redeclare
337-
let DraweeView: DraweeView;
338-
function initializeDraweeView() {
339-
if (DraweeView) {
340-
return;
341-
}
342-
@NativeClass
343-
class DraweeViewImpl extends com.facebook.drawee.view.SimpleDraweeView {
344-
constructor(public owner: Img, context: android.content.Context) {
345-
super(context);
346-
return global.__native(this);
347-
}
348-
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
349-
const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
350-
const widthMode = Utils.layout.getMeasureSpecMode(widthMeasureSpec);
351-
const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
352-
const heightMode = Utils.layout.getMeasureSpecMode(heightMeasureSpec);
353-
const aspectRatio = this.getAspectRatio();
354-
CLog(CLogTypes.info, 'onMeasure', this.owner.src, widthMeasureSpec, heightMeasureSpec, width, height, aspectRatio);
355-
if (aspectRatio > 0) {
356-
const finiteWidth: boolean = widthMode === Utils.layout.EXACTLY;
357-
const finiteHeight: boolean = heightMode === Utils.layout.EXACTLY;
358-
// let scale: { width; height };
359-
if ((this as any).imageWidth && (this as any).imageHeight) {
360-
// scale = this.owner.computeScaleFactor(width, height, finiteWidth, finiteHeight, (this as any).imageWidth, (this as any).imageHeight, aspectRatio);
361-
if (!finiteWidth) {
362-
widthMeasureSpec = Utils.layout.makeMeasureSpec(height * aspectRatio, Utils.layout.EXACTLY);
363-
}
364-
if (!finiteHeight) {
365-
heightMeasureSpec = Utils.layout.makeMeasureSpec(width / aspectRatio, Utils.layout.EXACTLY);
366-
}
367-
}
368-
CLog(CLogTypes.info, 'onMeasure scale', this.owner.src, aspectRatio, finiteWidth, finiteHeight, width, height, (this as any).imageWidth, (this as any).imageHeight);
369-
}
370-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
371-
}
372-
}
373-
DraweeView = DraweeViewImpl as any;
374-
}
335+
// type DraweeView = new (owner: Img, context) => com.facebook.drawee.view.SimpleDraweeView;
336+
// // eslint-disable-next-line no-redeclare
337+
// let DraweeView: DraweeView;
338+
// function initializeDraweeView() {
339+
// if (DraweeView) {
340+
// return;
341+
// }
342+
// @NativeClass
343+
// class DraweeViewImpl extends com.facebook.drawee.view.SimpleDraweeView {
344+
// constructor(public owner: Img, context: android.content.Context) {
345+
// super(context);
346+
// return global.__native(this);
347+
// }
348+
// public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number) {
349+
// const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec);
350+
// const widthMode = Utils.layout.getMeasureSpecMode(widthMeasureSpec);
351+
// const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec);
352+
// const heightMode = Utils.layout.getMeasureSpecMode(heightMeasureSpec);
353+
// const aspectRatio = this.getAspectRatio();
354+
// CLog(CLogTypes.info, 'onMeasure', this.owner.src, widthMeasureSpec, heightMeasureSpec, width, height, aspectRatio);
355+
// if (aspectRatio > 0) {
356+
// const finiteWidth: boolean = widthMode === Utils.layout.EXACTLY;
357+
// const finiteHeight: boolean = heightMode === Utils.layout.EXACTLY;
358+
// // let scale: { width; height };
359+
// if ((this as any).imageWidth && (this as any).imageHeight) {
360+
// // scale = this.owner.computeScaleFactor(width, height, finiteWidth, finiteHeight, (this as any).imageWidth, (this as any).imageHeight, aspectRatio);
361+
// if (!finiteWidth) {
362+
// widthMeasureSpec = Utils.layout.makeMeasureSpec(height * aspectRatio, Utils.layout.EXACTLY);
363+
// }
364+
// if (!finiteHeight) {
365+
// heightMeasureSpec = Utils.layout.makeMeasureSpec(width / aspectRatio, Utils.layout.EXACTLY);
366+
// }
367+
// }
368+
// CLog(CLogTypes.info, 'onMeasure scale', this.owner.src, aspectRatio, finiteWidth, finiteHeight, width, height, (this as any).imageWidth, (this as any).imageHeight);
369+
// }
370+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
371+
// }
372+
// }
373+
// DraweeView = DraweeViewImpl as any;
374+
// }
375375

376376
export class Img extends ImageBase {
377-
nativeViewProtected: com.facebook.drawee.view.SimpleDraweeView;
377+
nativeViewProtected: com.nativescript.image.DraweeView;
378378
isLoading = false;
379379

380380
_canRequestImage = true;
@@ -403,14 +403,14 @@ export class Img extends ImageBase {
403403
if (!initialized) {
404404
initialize(initializeConfig);
405405
}
406-
initializeDraweeView();
407-
return new DraweeView(this, this._context);
406+
// initializeDraweeView();
407+
return new com.nativescript.image.DraweeView(this._context);
408408
}
409409
updateViewSize(imageInfo) {
410410
const draweeView = this.nativeViewProtected;
411411
if (imageInfo != null) {
412-
(draweeView as any).imageWidth = imageInfo.getWidth();
413-
(draweeView as any).imageHeight = imageInfo.getHeight();
412+
draweeView.imageWidth = imageInfo.getWidth();
413+
draweeView.imageHeight = imageInfo.getHeight();
414414
}
415415
if (!this.aspectRatio && imageInfo != null) {
416416
const ratio = imageInfo.getWidth() / imageInfo.getHeight();

src/typings/android.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
declare namespace com {
55
export namespace nativescript {
66
export namespace image {
7+
class DraweeView extends facebook.drawee.view.SimpleDraweeView {
8+
imageWidth: number;
9+
imageHeight: number;
10+
}
711
class ScalingBlurPostprocessor extends facebook.imagepipeline.request.BasePostprocessor {
812
public constructor(iterations: number, blurRadius: number, scaleRatio: number);
913
}

0 commit comments

Comments
 (0)