From 1f6f8c8049a78bc101222f625711ad174b52c059 Mon Sep 17 00:00:00 2001 From: longerian Date: Wed, 16 May 2018 19:48:50 +0800 Subject: [PATCH 1/2] add performance monitor api --- .../vlayout/example/VLayoutActivity.java | 16 ++++++ .../android/vlayout/VirtualLayoutManager.java | 31 +++++++++++ .../vlayout/extend/PerformanceMonitor.java | 52 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 vlayout/src/main/java/com/alibaba/android/vlayout/extend/PerformanceMonitor.java diff --git a/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java b/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java index c6f49441..d17b5522 100644 --- a/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java +++ b/examples/src/main/java/com/alibaba/android/vlayout/example/VLayoutActivity.java @@ -29,6 +29,7 @@ import com.alibaba.android.vlayout.RecyclablePagerAdapter; import com.alibaba.android.vlayout.VirtualLayoutManager; import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams; +import com.alibaba.android.vlayout.extend.PerformanceMonitor; import com.alibaba.android.vlayout.layout.ColumnLayoutHelper; import com.alibaba.android.vlayout.layout.FixLayoutHelper; import com.alibaba.android.vlayout.layout.FloatLayoutHelper; @@ -119,7 +120,22 @@ protected void onCreate(Bundle savedInstanceState) { final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view); final VirtualLayoutManager layoutManager = new VirtualLayoutManager(this); + layoutManager.setPerformanceMonitor(new PerformanceMonitor() { + long start; + long end; + + @Override + public void recordStart(String phase, View view) { + start = System.currentTimeMillis(); + } + + @Override + public void recordEnd(String phase, View view) { + end = System.currentTimeMillis(); + Log.d("VLayoutActivity", view.getClass().getName() + " " + (end - start)); + } + }); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) { diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java index fa29833b..93c266c4 100644 --- a/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/VirtualLayoutManager.java @@ -32,6 +32,7 @@ import java.util.List; import java.util.Map; +import com.alibaba.android.vlayout.extend.PerformanceMonitor; import com.alibaba.android.vlayout.layout.BaseLayoutHelper; import com.alibaba.android.vlayout.layout.DefaultLayoutHelper; import com.alibaba.android.vlayout.layout.FixAreaAdjuster; @@ -67,6 +68,8 @@ public class VirtualLayoutManager extends ExposeLinearLayoutManagerEx implements LayoutManagerHelper { protected static final String TAG = "VirtualLayoutManager"; + private static final String PHASE_MEASURE = "measure"; + private static final String PHASE_LAYOUT = "layout"; private static final String TRACE_LAYOUT = "VLM onLayoutChildren"; private static final String TRACE_SCROLL = "VLM scroll"; @@ -94,6 +97,7 @@ public static void enableDebugging(boolean isDebug) { private int mMaxMeasureSize = -1; + private PerformanceMonitor mPerformanceMonitor; public VirtualLayoutManager(@NonNull final Context context) { this(context, VERTICAL); @@ -121,6 +125,9 @@ public VirtualLayoutManager(@NonNull final Context context, int orientation, boo setHelperFinder(new RangeLayoutHelperFinder()); } + public void setPerformanceMonitor(PerformanceMonitor performanceMonitor) { + mPerformanceMonitor = performanceMonitor; + } public void setNoScrolling(boolean noScrolling) { this.mNoScrolling = noScrolling; @@ -1265,14 +1272,26 @@ public boolean canScrollVertically() { @Override public void layoutChildWithMargins(View child, int left, int top, int right, int bottom) { final ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) child.getLayoutParams(); + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordStart(PHASE_LAYOUT, child); + } layoutDecorated(child, left + lp.leftMargin, top + lp.topMargin, right - lp.rightMargin, bottom - lp.bottomMargin); + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordEnd(PHASE_LAYOUT, child); + } } @Override public void layoutChild(View child, int left, int top, int right, int bottom) { + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordStart(PHASE_LAYOUT, child); + } layoutDecorated(child, left, top, right, bottom); + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordEnd(PHASE_LAYOUT, child); + } } @Override @@ -1390,7 +1409,13 @@ private void measureChildWithDecorations(View child, int widthSpec, int heightSp calculateItemDecorationsForChild(child, mDecorInsets); widthSpec = updateSpecWithExtra(widthSpec, mDecorInsets.left, mDecorInsets.right); heightSpec = updateSpecWithExtra(heightSpec, mDecorInsets.top, mDecorInsets.bottom); + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordStart(PHASE_MEASURE, child); + } child.measure(widthSpec, heightSpec); + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordEnd(PHASE_MEASURE, child); + } } private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec) { @@ -1405,7 +1430,13 @@ private void measureChildWithDecorationsAndMargin(View child, int widthSpec, int heightSpec = updateSpecWithExtra(heightSpec, mDecorInsets.top, mDecorInsets.bottom); } + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordStart(PHASE_MEASURE, child); + } child.measure(widthSpec, heightSpec); + if (mPerformanceMonitor != null) { + mPerformanceMonitor.recordEnd(PHASE_MEASURE, child); + } } /** diff --git a/vlayout/src/main/java/com/alibaba/android/vlayout/extend/PerformanceMonitor.java b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/PerformanceMonitor.java new file mode 100644 index 00000000..b28c0e06 --- /dev/null +++ b/vlayout/src/main/java/com/alibaba/android/vlayout/extend/PerformanceMonitor.java @@ -0,0 +1,52 @@ +package com.alibaba.android.vlayout.extend; + +import android.view.View; + +/** + * Add callback during measure and layout, help you to monitor your view's performance.
+ * Designed as Class instead of Interface is able to extend api in future.
+ * + * Created by longerian on 2018/5/16. + * + * @author longerian + * @date 2018/05/16 + */ +public class PerformanceMonitor { + + /** + * Record the start time + * @param phase + * @param viewType + */ + public void recordStart(String phase, String viewType) { + + } + + /** + * Record the end time + * @param phase + * @param viewType + */ + public void recordEnd(String phase, String viewType) { + + } + + /** + * Record the start time + * @param phase + * @param view + */ + public void recordStart(String phase, View view) { + + } + + /** + * Record the end time + * @param phase + * @param view + */ + public void recordEnd(String phase, View view) { + + } + +} From 1677d73175dc5b73a2ccee4d5cf6b753f11b308b Mon Sep 17 00:00:00 2001 From: longerian Date: Wed, 16 May 2018 19:49:36 +0800 Subject: [PATCH 2/2] update version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 021f2cc5..7c3adaae 100644 --- a/gradle.properties +++ b/gradle.properties @@ -44,7 +44,7 @@ GROUP=com.alibaba.android ARTIFACT=vlayout VERSION=1 -VERSION_NAME=1.2.13 +VERSION_NAME=1.2.14 PACKAGING_TYPE=aar useNewSupportLibrary=true systemProp.compileSdkVersion=25