├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ ├── lsjwzh │ │ └── test │ │ │ ├── AutoScrollHandler.java │ │ │ ├── Const.java │ │ │ ├── FastTextLayoutView.java │ │ │ ├── FastTextView.java │ │ │ ├── FpsCalculator.java │ │ │ ├── GhostThread.java │ │ │ ├── StaticLayoutManager.java │ │ │ ├── TestSingleLineTextView.java │ │ │ ├── TestSpan.java │ │ │ ├── TestStats.java │ │ │ ├── TestTextView.java │ │ │ ├── TextLineView.java │ │ │ └── Util.java │ │ └── wechat │ │ └── testdemo │ │ ├── EllipseFragment.java │ │ ├── FastTextViewListTestFragment.java │ │ ├── MainActivity.java │ │ ├── MainActivityFragment.java │ │ ├── NormalLayoutTestFragment.java │ │ ├── ReadMoreFragment.java │ │ ├── ReadMoreListTestFragment.java │ │ ├── StaticLayoutCacheTestFragment.java │ │ └── TestListAdapter.java │ └── res │ ├── anim │ ├── popup_enter.xml │ └── popup_exit.xml │ ├── drawable-hdpi │ └── test.png │ ├── drawable-xhdpi │ └── ic_ab_back_holo_dark_am.png │ ├── drawable │ └── popup_background.xml │ ├── layout │ ├── activity_main.xml │ ├── ellipse_demo.xml │ ├── fast_layout_ui.xml │ ├── fast_list_item.xml │ ├── fragment_main.xml │ ├── layout_cache_demo.xml │ ├── normal_layout_ui.xml │ ├── normal_list_item.xml │ ├── read_more_demo.xml │ ├── readmore_list_item.xml │ ├── spinner_item.xml │ ├── static_layout_ui.xml │ └── static_list_item.xml │ ├── menu │ └── menu_main.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── values-v23 │ └── styles.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── ellipsis.png ├── gradle.properties ├── gradlew ├── gradlew.bat ├── readmore.gif ├── settings.gradle ├── text.Textline ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── android │ └── text │ ├── Directions.java │ ├── ITextLine.java │ ├── TextLineCompat.java │ ├── TextLineImpl15.java │ └── TextLineImpl23.java └── widget.FastTextView ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java ├── android │ └── text │ │ ├── EllipsisSpannedContainer.java │ │ ├── LayoutUtils.java │ │ ├── SpanSetCompat.java │ │ ├── StaticLayoutBuilderCompat.java │ │ ├── TextLayoutCache.java │ │ └── TextLayoutWarmer.java └── com │ └── lsjwzh │ └── widget │ └── text │ ├── ClickableSpanLayoutView.java │ ├── ClickableSpanUtil.java │ ├── FastTextLayoutView.java │ ├── FastTextView.java │ ├── ItalicReplacementSpan.java │ ├── LineUtil.java │ ├── NestReplacementSpan.java │ ├── ReadMoreTextView.java │ ├── SingleLineTextView.java │ ├── StrokableTextView.java │ ├── StrokeReplacementSpan.java │ ├── StrokeSpan.java │ ├── StrokeSpanUtil.java │ ├── TextMeasureUtil.java │ └── TextViewAttrsHelper.java └── res └── values └── attrs.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FastTextView 2 | 3 | FastTextView is faster than Android TextView. 4 | FastTextView use StaticLayout to render Spanned String, 5 | so it support most features of Android TextView. 6 | 7 | Inspired by : 8 | 9 | https://engineering.instagram.com/improving-comment-rendering-on-android-a77d5db3d82e 10 | 11 | http://ragnraok.github.io/textview-pre-render-research.html 12 | 13 | ## Features 14 | 1.Faster than Android TextView 15 | 16 | 2.More flexibility 17 | 18 | 3.Support Stroke Text 19 | (More accurate method to measure stroke text and italic text) 20 | 21 | 4.Correct Ellipsis handle with ImageSpan 22 | 23 | ![](ellipsis.png) 24 | 25 | 5.Custom Read More Support 26 | 27 | ![](readmore.gif) 28 | 29 | 30 | ## FastTextView vs Android TextView 31 | Rendering a SpannableString of 389 chars with ClickableSpan and ImageSpan. 32 | Call 'onMeasure'、'onDraw' 1000 times. 33 | Here are the test results on MI MAX (Android 6.0.1): 34 | 35 | ``` 36 | D/FastTextLayoutView: FastTextLayoutView onMeasure cost:0 37 | D/FastTextView: FastTextView onMeasure cost:1 38 | D/TestTextView: TestTextView measure cost:104 39 | D/FastTextLayoutView: FastTextLayoutView onDraw cost:271 40 | D/FastTextView: FastTextView onDraw cost:250 41 | D/TestTextView: TestTextView onDraw cost:249 42 | ``` 43 | You can see FastTextView's 'onMeasure' almost no time consuming. 44 | 45 | ## Basic Usage 46 | ``` 47 | repositories { 48 | ... 49 | jcenter() 50 | ... 51 | } 52 | 53 | dependencies { 54 | ... 55 | compile 'com.lsjwzh.widget:FastTextView:1.2.15' 56 | ... 57 | } 58 | ``` 59 | java code 60 | ``` 61 | FastTextView fastTextView = (FastTextView) mRootView.findViewById(R.id.fast_tv2); 62 | fastTextView.setText(spannableString); 63 | ``` 64 | 65 | ## Advance Usage 66 | ### Use Layout directly 67 | java code 68 | ``` 69 | TextPaint textPaint = new TextPaint(); 70 | textPaint.setAntiAlias(true); 71 | textPaint.setColor(Color.WHITE); 72 | float textSize = TypedValue.applyDimension( 73 | TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()); 74 | textPaint.setTextSize(textSize); 75 | FastTextLayoutView fastTextLayoutView = (FastTextLayoutView) mRootView.findViewById(R.id.fast_tv); 76 | int width = Layout.getDesiredWidth(spannableStringBuilder, textPaint); 77 | 78 | StaticLayout layout = new StaticLayout(spannableStringBuilder, textPaint, 79 | Math.min(width, getResources().getDisplayMetrics().widthPixels), Layout.Alignment.ALIGN_NORMAL, 80 | 1.0f, 0.0f, true); 81 | fastTextLayoutView.setTextLayout(layout); 82 | ``` 83 | 84 | ### Single Line Stroke Text 85 | java code 86 | ``` 87 | ... ... 88 | StrokeSpan strokeSpan = new StrokeSpan(Color.BLUE, Color.YELLOW, 20); 89 | spannableStringBuilder.setSpan(strokeSpan, 0, spannableStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 90 | FastTextView fastTextView = (FastTextView) mRootView.findViewById(R.id.fast_tv2); 91 | fastTextView.setText(spannableStringBuilder); 92 | ``` 93 | 94 | ### Read More 95 | ``` 96 | 97 | ReadMoreTextView readMoreTextView = (ReadMoreTextView) mRootView.findViewById(R.id.readmore_tv); 98 | readMoreTextView.setText(spannableStringBuilder); 99 | readMoreTextView.setCustomEllipsisSpan(new ReadMoreTextView.EllipsisSpan(" Read More")); 100 | readMoreTextView.setCustomCollapseSpan(new ReadMoreTextView.EllipsisSpan(" Collapse")); 101 | ``` 102 | 103 | # Build Project 104 | https://github.com/anggrayudi/android-hidden-api 105 | 106 | You should use 'android-hidden-api' to build project. 107 | 108 | 1.Go to /platforms/. 109 | 110 | 2.Copy, paste and replace the downloaded hidden API file into this directory, e.g. android-25/android.jar. 111 | 112 | 3.Change compileSdkVersion and targetSdkVersion to 25 (for example). 113 | 114 | 4.Finally, rebuild your project. 115 | 116 | # TODO List 117 | 1.Layout Cache (For List Scene) 118 | 119 | 2.ColorState background 120 | 121 | 3.ReadMore 122 | 123 | 4.Ellipsis 124 | 125 | 5.AutoSize 126 | 127 | 6.ColorState textColor 128 | 129 | 130 | # License 131 | ``` 132 | Copyright 2017 lsjwzh 133 | 134 | Licensed under the Apache License, Version 2.0 (the "License"); 135 | you may not use this file except in compliance with the License. 136 | You may obtain a copy of the License at 137 | 138 | http://www.apache.org/licenses/LICENSE-2.0 139 | 140 | Unless required by applicable law or agreed to in writing, software 141 | distributed under the License is distributed on an "AS IS" BASIS, 142 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 143 | See the License for the specific language governing permissions and 144 | limitations under the License. 145 | ``` -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion "28.0.3" 6 | defaultConfig { 7 | applicationId "com.wechat.testdemo" 8 | minSdkVersion 15 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | lintOptions { 21 | abortOnError false 22 | } 23 | } 24 | 25 | dependencies { 26 | implementation fileTree(dir: 'libs', include: ['*.jar']) 27 | implementation 'com.android.support:appcompat-v7:28.0.0' 28 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 29 | implementation 'com.android.support:design:28.0.0' 30 | implementation project(':widget.FastTextView') 31 | // compile project(':text.Textline') 32 | // compile 'com.lsjwzh.widget:FastTextView:0.9.3' 33 | 34 | } 35 | 36 | task cleanAndroidMock(type: Delete) { 37 | description = 'Deletes the mockable Android jar' 38 | 39 | delete fileTree("${project.buildDir}/generated") { 40 | include 'mockable-android*.jar' 41 | } 42 | } 43 | 44 | project.afterEvaluate { 45 | tasks['createMockableJar'].dependsOn cleanAndroidMock 46 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/wenye/android_sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/AutoScrollHandler.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | import android.widget.AbsListView; 6 | import android.widget.ListView; 7 | 8 | /** 9 | * Created by ragnarok on 15/7/22. 10 | */ 11 | public class AutoScrollHandler { 12 | 13 | private ListView listView; 14 | 15 | private int itemCount; 16 | 17 | private Handler uiHandler = new Handler(Looper.getMainLooper()); 18 | 19 | public AutoScrollHandler(ListView listView, int itemCount) { 20 | this.listView = listView; 21 | this.itemCount = itemCount; 22 | } 23 | 24 | public void startAutoScrollDown(final Callback callback) { 25 | FpsCalculator.instance().startCalculate(); 26 | final int position = listView.getAdapter().getCount() - 1; 27 | startPositionAndTrack(position, callback); 28 | } 29 | 30 | public void startAutoScrollUp(final Callback callback) { 31 | FpsCalculator.instance().startCalculate(); 32 | startPositionAndTrack(0, callback); 33 | } 34 | 35 | private void startPositionAndTrack(final int position, final Callback callback) { 36 | FpsCalculator.instance().startCalculate(); 37 | listView.smoothScrollToPosition(position); 38 | listView.setOnScrollListener(new AbsListView.OnScrollListener() { 39 | @Override 40 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int 41 | totalItemCount) { 42 | 43 | } 44 | 45 | @Override 46 | public void onScrollStateChanged(AbsListView view, int scrollState) { 47 | if (scrollState == SCROLL_STATE_IDLE 48 | && (position == 0 ? listView.getFirstVisiblePosition() == 0 : 49 | listView.getLastVisiblePosition() == position)) { 50 | listView.setOnScrollListener(null); 51 | final int fps = FpsCalculator.instance().stopGetAvgFPS(); 52 | callback.callback(fps); 53 | } 54 | } 55 | }); 56 | } 57 | 58 | public interface Callback { 59 | void callback(int fps); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/Const.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | /** 4 | * Created by wenye on 2017/11/6. 5 | */ 6 | 7 | public class Const { 8 | public final static int LOOP_COUNT = 1; 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/FastTextLayoutView.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.os.Build; 6 | import android.os.SystemClock; 7 | import android.support.annotation.Nullable; 8 | import android.support.annotation.Px; 9 | import android.support.annotation.RequiresApi; 10 | import android.util.AttributeSet; 11 | import android.util.Log; 12 | 13 | /** 14 | * A custom view for rendering layout directly. 15 | */ 16 | public class FastTextLayoutView extends com.lsjwzh.widget.text.ClickableSpanLayoutView { 17 | private static final String TAG = FastTextLayoutView.class.getSimpleName(); 18 | private boolean mIsDebug = false; 19 | public static final TestStats TEST_STATS = new TestStats(); 20 | 21 | public FastTextLayoutView(Context context) { 22 | super(context); 23 | } 24 | 25 | public FastTextLayoutView(Context context, @Nullable AttributeSet attrs) { 26 | super(context, attrs); 27 | } 28 | 29 | public FastTextLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 30 | super(context, attrs, defStyleAttr); 31 | } 32 | 33 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 34 | public FastTextLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 35 | super(context, attrs, defStyleAttr, defStyleRes); 36 | } 37 | 38 | @Override 39 | protected void onDraw(Canvas canvas) { 40 | TEST_STATS.drawStart(); 41 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 42 | // TODO for test 43 | super.onDraw(canvas); 44 | } 45 | TEST_STATS.drawEnd(); 46 | if (mIsDebug) { 47 | Log.d(TAG, TAG + " onDraw cost:" + TEST_STATS.getDrawCost()); 48 | } 49 | } 50 | 51 | @Override 52 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 53 | TEST_STATS.measuretart(); 54 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 55 | // TODO for test 56 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 57 | } 58 | TEST_STATS.measureEnd(); 59 | if (mIsDebug) { 60 | Log.d(TAG, TAG + " onMeasure cost:" + TEST_STATS.getMeasureCost()); 61 | } 62 | } 63 | 64 | @Override 65 | public void layout(@Px int l, @Px int t, @Px int r, @Px int b) { 66 | TEST_STATS.layoutStart(); 67 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 68 | // TODO for test 69 | super.layout(l, t, r, b); 70 | } 71 | TEST_STATS.layoutEnd(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/FastTextView.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.os.Build; 6 | import android.os.SystemClock; 7 | import android.support.annotation.Nullable; 8 | import android.support.annotation.Px; 9 | import android.support.annotation.RequiresApi; 10 | import android.util.AttributeSet; 11 | import android.util.Log; 12 | 13 | /** 14 | * Simple and Fast TextView. 15 | */ 16 | public class FastTextView extends com.lsjwzh.widget.text.FastTextView { 17 | private static final String TAG = FastTextView.class.getSimpleName(); 18 | private boolean mIsDebug = false; 19 | public static final TestStats TEST_STATS = new TestStats(); 20 | 21 | public FastTextView(Context context) { 22 | super(context); 23 | } 24 | 25 | public FastTextView(Context context, @Nullable AttributeSet attrs) { 26 | super(context, attrs); 27 | } 28 | 29 | public FastTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 30 | super(context, attrs, defStyleAttr); 31 | } 32 | 33 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 34 | public FastTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 35 | super(context, attrs, defStyleAttr, defStyleRes); 36 | } 37 | 38 | @Override 39 | protected void onDraw(Canvas canvas) { 40 | TEST_STATS.drawStart(); 41 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 42 | super.onDraw(canvas); 43 | } 44 | TEST_STATS.drawEnd(); 45 | if (mIsDebug) { 46 | Log.d(TAG, TAG + " onDraw cost:" + TEST_STATS.getDrawCost()); 47 | } 48 | } 49 | 50 | @Override 51 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 52 | TEST_STATS.measuretart(); 53 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 54 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 55 | } 56 | TEST_STATS.measureEnd(); 57 | if (mIsDebug) { 58 | Log.d(TAG, TAG + " onMeasure cost:" + TEST_STATS.getMeasureCost()); 59 | } 60 | } 61 | 62 | @Override 63 | public void layout(@Px int l, @Px int t, @Px int r, @Px int b) { 64 | TEST_STATS.layoutStart(); 65 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 66 | // TODO for test 67 | super.layout(l, t, r, b); 68 | } 69 | TEST_STATS.layoutEnd(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/FpsCalculator.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.annotation.TargetApi; 4 | import android.os.Build; 5 | import android.util.Log; 6 | import android.view.Choreographer; 7 | import android.view.Choreographer.FrameCallback; 8 | 9 | import java.lang.reflect.Field; 10 | import java.util.concurrent.atomic.AtomicInteger; 11 | 12 | public class FpsCalculator { 13 | 14 | private final static String TAG = "FpsCalculator"; 15 | 16 | private long mFrameIntervalNanos; 17 | 18 | private boolean mRunning = false; 19 | 20 | private static FpsCalculator instance; 21 | 22 | private AtomicInteger atom = new AtomicInteger(0); 23 | private Thread syncCheckThread = null; 24 | 25 | static { 26 | instance = new FpsCalculator(); 27 | } 28 | 29 | public static FpsCalculator instance() { 30 | return instance; 31 | } 32 | 33 | private int totalFps; 34 | private int fpsCalculateCount; 35 | private boolean isCalculatingFPS; 36 | 37 | // calculate the average fps 38 | 39 | public void startCalculate() { 40 | totalFps = 0; 41 | fpsCalculateCount = 0; 42 | isCalculatingFPS = true; 43 | } 44 | 45 | public int stopGetAvgFPS() { 46 | isCalculatingFPS = false; 47 | int avgFPS = totalFps / fpsCalculateCount; 48 | totalFps = 0; 49 | fpsCalculateCount = 0; 50 | return avgFPS; 51 | } 52 | 53 | private void syncCheckThread(){ 54 | if(!mRunning){ 55 | return; 56 | } 57 | int val = atom.getAndSet(0); 58 | if (isCalculatingFPS) { 59 | totalFps += val; 60 | fpsCalculateCount++; 61 | } 62 | android.util.Log.i(TAG, "FPS: " + val); 63 | try { 64 | Thread.sleep(1000); 65 | } catch (InterruptedException e) { 66 | e.printStackTrace(); 67 | } 68 | } 69 | 70 | private FrameCallback frameCallback = new FrameCallback() { 71 | 72 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 73 | @Override 74 | public void doFrame(long frameTimeNanos) { 75 | 76 | if (!mRunning) { 77 | return; 78 | } 79 | 80 | Choreographer.getInstance().postFrameCallback(frameCallback); 81 | 82 | atom.incrementAndGet(); 83 | }}; 84 | 85 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 86 | public void start() { 87 | Log.d(TAG, "start vsync detect"); 88 | if (mRunning) { 89 | return; 90 | } 91 | 92 | mRunning = true; 93 | 94 | syncCheckThread = new Thread(new Runnable() { 95 | @Override 96 | public void run() { 97 | for (;;) { 98 | if (!mRunning) { 99 | break; 100 | } 101 | syncCheckThread(); 102 | } 103 | } 104 | }); 105 | syncCheckThread.start(); 106 | 107 | Choreographer chor = Choreographer.getInstance(); 108 | Field field; 109 | try { 110 | field = chor.getClass().getDeclaredField("mFrameIntervalNanos"); 111 | field.setAccessible(true); 112 | mFrameIntervalNanos = field.getLong(chor); 113 | Log.d(TAG, "mFrameIntervalNanos " + mFrameIntervalNanos); 114 | } catch (Exception e) { 115 | Log.e(TAG, "error: " + e.getMessage()); 116 | } 117 | chor.postFrameCallback(frameCallback); 118 | 119 | } 120 | 121 | public void stop() { 122 | mRunning = false; 123 | if (syncCheckThread != null) { 124 | try { 125 | syncCheckThread.join(); 126 | } catch (InterruptedException e) { 127 | e.printStackTrace(); 128 | } 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/GhostThread.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | 4 | import android.util.Log; 5 | 6 | import java.util.Random; 7 | 8 | /** 9 | * Created by ragnarok on 15/7/22. 10 | * The GhostThread is used to simulate the heavily jobs behind the UI thread in the 11 | * real world apps 12 | * Currently, the implementation is work by launch several threads and calculate floating point 13 | * multiplication infinity 14 | */ 15 | public class GhostThread { 16 | private static final String TAG = "GhostThread"; 17 | 18 | private static boolean isStart = false; 19 | 20 | private static Thread[] threads = new Thread[0]; 21 | 22 | private static Random random = new Random(); 23 | private static Runnable runnable = new Runnable() { 24 | @Override 25 | public void run() { 26 | for (; ;) { 27 | if (!isStart) { 28 | break; 29 | } 30 | double c = Math.PI * Math.PI * Math.PI * random.nextFloat(); 31 | // Log.e("test", "v:" + c); 32 | } 33 | } 34 | }; 35 | 36 | public static void start() { 37 | if (isStart) { 38 | return; 39 | } 40 | isStart = true; 41 | for (int i = 0; i < threads.length; i++) { 42 | Thread thread = new Thread(runnable); 43 | thread.setPriority(Thread.NORM_PRIORITY); 44 | thread.start(); 45 | threads[i] = thread; 46 | } 47 | } 48 | 49 | public static void stop() { 50 | isStart = false; 51 | for (Thread thread : threads) { 52 | try { 53 | thread.join(); 54 | } catch (InterruptedException e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/StaticLayoutManager.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.graphics.Canvas; 6 | import android.graphics.Paint; 7 | import android.os.Build; 8 | import android.text.Layout; 9 | import android.text.StaticLayout; 10 | import android.text.TextDirectionHeuristic; 11 | import android.text.TextDirectionHeuristics; 12 | import android.text.TextLayoutWarmer; 13 | import android.text.TextPaint; 14 | 15 | /** 16 | * Created by ragnarok on 15/7/21. 17 | */ 18 | public class StaticLayoutManager { 19 | public final static TextLayoutWarmer sLayoutWarmer = new TextLayoutWarmer<>(); 20 | 21 | private StaticLayout[] layout = new StaticLayout[Util.TEST_LIST_ITEM_COUNT]; 22 | 23 | private StaticLayout longStringLayout; 24 | 25 | private TextPaint textPaint; 26 | private TextDirectionHeuristic textDir; 27 | private Layout.Alignment alignment; 28 | 29 | private Canvas dummyCanvas; 30 | 31 | private int hardCodeWidth; 32 | 33 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) 34 | public void initLayout(Context context, CharSequence source, CharSequence longString) { 35 | 36 | textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 37 | textPaint.density = context.getResources().getDisplayMetrics().density; 38 | textPaint.setTextSize(Util.fromDPtoPix(context, Util.TEXT_SIZE_DP)); 39 | 40 | textDir = TextDirectionHeuristics.LTR; 41 | 42 | alignment = Layout.Alignment.ALIGN_NORMAL; 43 | 44 | hardCodeWidth = Util.getScreenWidth(context); 45 | 46 | longStringLayout = new StaticLayout(longString, textPaint, hardCodeWidth, alignment, 1.0f, 0f, true); 47 | 48 | dummyCanvas = new Canvas(); 49 | 50 | longStringLayout.draw(dummyCanvas); 51 | 52 | for (int i = 0; i < layout.length; i++) { 53 | layout[i] = new StaticLayout(TestSpan.getSpanString(i), textPaint, hardCodeWidth, alignment, 1.0f, 0f, true); 54 | layout[i].draw(dummyCanvas); 55 | } 56 | } 57 | 58 | public StaticLayout getLayout(int index) { 59 | return layout[index] != null ? layout[index] 60 | : sLayoutWarmer.getLayout(TestSpan.getSpanString(index)); 61 | } 62 | 63 | public StaticLayout getLongStringLayout() { 64 | return longStringLayout; 65 | } 66 | 67 | private static StaticLayoutManager INSTANCE = null; 68 | 69 | public static StaticLayoutManager getInstance() { 70 | if (INSTANCE == null) { 71 | INSTANCE = new StaticLayoutManager(); 72 | } 73 | return INSTANCE; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/TestSingleLineTextView.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.os.Build; 6 | import android.support.annotation.Nullable; 7 | import android.support.annotation.RequiresApi; 8 | import android.util.AttributeSet; 9 | import android.util.Log; 10 | 11 | import com.lsjwzh.widget.text.SingleLineTextView; 12 | 13 | /** 14 | * Created by wenye on 2017/11/5. 15 | */ 16 | 17 | public class TestSingleLineTextView extends SingleLineTextView { 18 | private static final String TAG = "TestSingleLineTextView"; 19 | private boolean mIsDebug = true; 20 | 21 | public TestSingleLineTextView(Context context) { 22 | super(context); 23 | } 24 | 25 | public TestSingleLineTextView(Context context, @Nullable AttributeSet attrs) { 26 | super(context, attrs); 27 | } 28 | 29 | public TestSingleLineTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 30 | super(context, attrs, defStyleAttr); 31 | } 32 | 33 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 34 | public TestSingleLineTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 35 | super(context, attrs, defStyleAttr, defStyleRes); 36 | } 37 | 38 | 39 | @Override 40 | protected void onDraw(Canvas canvas) { 41 | long start = System.currentTimeMillis(); 42 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 43 | super.onDraw(canvas); 44 | } 45 | long end = System.currentTimeMillis(); 46 | if (mIsDebug) { 47 | Log.d(TAG, TAG + " onDraw cost:" + (end - start)); 48 | } 49 | } 50 | 51 | @Override 52 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 53 | long start = System.currentTimeMillis(); 54 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 55 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 56 | } 57 | long end = System.currentTimeMillis(); 58 | if (mIsDebug) { 59 | Log.d(TAG, TAG + " onMeasure cost:" + (end - start)); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/TestStats.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.os.SystemClock; 4 | 5 | /** 6 | * Created by wenye on 2017/12/15. 7 | */ 8 | public class TestStats { 9 | private volatile long drawCost = 0; 10 | private volatile long drawCount = 0; 11 | private volatile long measureCost = 0; 12 | private volatile long measureCount = 0; 13 | private volatile long layoutCost = 0; 14 | private volatile long layoutCount = 0; 15 | 16 | private long drawStart = 0; 17 | private long measureStart = 0; 18 | private long layoutStart = 0; 19 | 20 | @Override 21 | public String toString() { 22 | return "TestStats{" + 23 | "drawCost=" + drawCost + 24 | ", drawCount=" + drawCount + 25 | ", measureCost=" + measureCost + 26 | ", measureCount=" + measureCount + 27 | ", layoutCost=" + layoutCost + 28 | ", layoutCount=" + layoutCount + 29 | '}'; 30 | } 31 | 32 | public void reset() { 33 | drawCost = 0; 34 | drawCount = 0; 35 | measureCost = 0; 36 | measureCount = 0; 37 | layoutCost = 0; 38 | layoutCount = 0; 39 | } 40 | 41 | public long getDrawCost() { 42 | return drawCost; 43 | } 44 | 45 | public long getDrawCount() { 46 | return drawCount; 47 | } 48 | 49 | public long getMeasureCost() { 50 | return measureCost; 51 | } 52 | 53 | public long getMeasureCount() { 54 | return measureCount; 55 | } 56 | 57 | public long getLayoutCost() { 58 | return layoutCost; 59 | } 60 | 61 | public long getLayoutCount() { 62 | return layoutCount; 63 | } 64 | 65 | public void drawStart() { 66 | drawStart = SystemClock.elapsedRealtime(); 67 | } 68 | 69 | public void drawEnd() { 70 | drawCost += SystemClock.elapsedRealtime() - drawStart; 71 | drawCount++; 72 | } 73 | 74 | public void measuretart() { 75 | measureStart = SystemClock.elapsedRealtime(); 76 | } 77 | 78 | public void measureEnd() { 79 | measureCost += SystemClock.elapsedRealtime() - measureStart; 80 | measureCount++; 81 | } 82 | 83 | public void layoutStart() { 84 | layoutStart = SystemClock.elapsedRealtime(); 85 | } 86 | 87 | public void layoutEnd() { 88 | layoutCost += SystemClock.elapsedRealtime() - layoutStart; 89 | layoutCount++; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/TestTextView.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.os.SystemClock; 6 | import android.support.annotation.Nullable; 7 | import android.support.annotation.Px; 8 | import android.util.AttributeSet; 9 | import android.widget.TextView; 10 | 11 | /** 12 | * Created by wenye on 2017/10/12. 13 | */ 14 | 15 | public class TestTextView extends TextView { 16 | private static final String TAG = TestTextView.class.getSimpleName(); 17 | public static final TestStats TEST_STATS = new TestStats(); 18 | 19 | public TestTextView(Context context) { 20 | super(context); 21 | } 22 | 23 | public TestTextView(Context context, @Nullable AttributeSet attrs) { 24 | super(context, attrs); 25 | } 26 | 27 | public TestTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 28 | super(context, attrs, defStyleAttr); 29 | } 30 | 31 | @Override 32 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 33 | TEST_STATS.measuretart(); 34 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 35 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 36 | } 37 | TEST_STATS.measureEnd(); 38 | } 39 | 40 | @Override 41 | protected void onDraw(Canvas canvas) { 42 | TEST_STATS.drawStart(); 43 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 44 | super.onDraw(canvas); 45 | } 46 | TEST_STATS.drawEnd(); 47 | // Log.d(TAG, TAG + " onDraw cost:" + (end - start)); 48 | } 49 | 50 | @Override 51 | public void layout(@Px int l, @Px int t, @Px int r, @Px int b) { 52 | TEST_STATS.layoutStart(); 53 | for (int i = 0; i < Const.LOOP_COUNT; i++) { 54 | // TODO for test 55 | super.layout(l, t, r, b); 56 | } 57 | TEST_STATS.layoutEnd(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/TextLineView.java: -------------------------------------------------------------------------------- 1 | //package com.lsjwzh.test; 2 | // 3 | //import android.content.Context; 4 | //import android.graphics.Canvas; 5 | //import android.graphics.Color; 6 | //import android.graphics.Paint; 7 | //import android.os.Build; 8 | //import android.os.SystemClock; 9 | //import android.support.annotation.Nullable; 10 | //import android.support.annotation.RequiresApi; 11 | //import android.text.Directions; 12 | //import android.text.Layout; 13 | //import android.text.TextLineCompat; 14 | //import android.text.TextPaint; 15 | //import android.util.AttributeSet; 16 | //import android.util.Log; 17 | //import android.view.View; 18 | // 19 | ///** 20 | // * Just for test. 21 | // * Created by wenye on 2017/11/5. 22 | // */ 23 | //public class TextLineView extends View { 24 | // private static final String TAG = "TextLineView"; 25 | // private CharSequence mText; 26 | // private TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 27 | // private final Paint.FontMetricsInt mFontMetric = new Paint.FontMetricsInt(); 28 | // TextLineCompat mTextLineCompat; 29 | // 30 | // public TextLineView(Context context) { 31 | // super(context); 32 | // } 33 | // 34 | // public TextLineView(Context context, @Nullable AttributeSet attrs) { 35 | // super(context, attrs); 36 | // } 37 | // 38 | // public TextLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 39 | // super(context, attrs, defStyleAttr); 40 | // } 41 | // 42 | // @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 43 | // public TextLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 44 | // super(context, attrs, defStyleAttr, defStyleRes); 45 | // } 46 | // 47 | // public void setText(CharSequence text) { 48 | // mText = text; 49 | // mTextPaint.setTextSize(20); 50 | // mTextPaint.setColor(Color.RED); 51 | // mTextLineCompat = TextLineCompat.obtain(); 52 | // mTextLineCompat.set(mTextPaint, mText, 0, mText.length(), Layout.DIR_LEFT_TO_RIGHT, Directions.DIRS_ALL_LEFT_TO_RIGHT, false, null); 53 | // mTextPaint.getFontMetricsInt(mFontMetric); 54 | // } 55 | // 56 | // @Override 57 | // protected void onDraw(Canvas canvas) { 58 | // long start = SystemClock.elapsedRealtime(); 59 | // for (int i = 0; i < Const.LOOP_COUNT; i++) { 60 | // super.onDraw(canvas); 61 | // int offset = getHeight() - (mFontMetric.bottom - mFontMetric.top); 62 | // int baseLine = offset / 2 - mFontMetric.top; 63 | // mTextLineCompat.draw(canvas, 0, 0, baseLine, baseLine + mFontMetric.bottom); 64 | // } 65 | // long end = SystemClock.elapsedRealtime(); 66 | // Log.d(TAG, TAG + " onDraw cost:" + (end - start)); 67 | // } 68 | // 69 | // @Override 70 | // protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 71 | // long start = SystemClock.elapsedRealtime(); 72 | // for (int i = 0; i < Const.LOOP_COUNT; i++) { 73 | // super.onMeasure(widthMeasureSpec, heightMeasureSpec); 74 | // } 75 | // long end = SystemClock.elapsedRealtime(); 76 | // Log.d(TAG, TAG + " measure cost:" + (end - start)); 77 | // 78 | // } 79 | // 80 | // 81 | //} 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/lsjwzh/test/Util.java: -------------------------------------------------------------------------------- 1 | package com.lsjwzh.test; 2 | 3 | import android.content.Context; 4 | import android.graphics.Point; 5 | import android.view.WindowManager; 6 | 7 | /** 8 | * Created by ragnarok on 15/7/21. 9 | */ 10 | public class Util { 11 | 12 | public static final int TEST_LIST_ITEM_COUNT = 500; 13 | 14 | public static final int TEXT_SIZE_DP = 25; 15 | 16 | public static final int AUTO_SCROLL_INTERVAL = 1; 17 | 18 | public static final int AUTO_SCROLL_STEP = 10; 19 | 20 | public static float fromDPtoPix(Context context, int dp) { 21 | return context.getResources().getDisplayMetrics().density * dp; 22 | } 23 | 24 | public static int getScreenWidth(Context context) { 25 | WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 26 | 27 | Point size = new Point(); 28 | windowManager.getDefaultDisplay().getSize(size); 29 | 30 | return size.x; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/EllipseFragment.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.graphics.Canvas; 4 | import android.graphics.Color; 5 | import android.graphics.Paint; 6 | import android.graphics.Rect; 7 | import android.graphics.drawable.Drawable; 8 | import android.os.Bundle; 9 | import android.os.SystemClock; 10 | import android.support.annotation.IntRange; 11 | import android.support.annotation.NonNull; 12 | import android.support.v4.app.Fragment; 13 | import android.text.Layout; 14 | import android.text.SpannableStringBuilder; 15 | import android.text.Spanned; 16 | import android.text.StaticLayout; 17 | import android.text.StaticLayoutBuilderCompat; 18 | import android.text.TextPaint; 19 | import android.text.TextUtils; 20 | import android.text.style.ImageSpan; 21 | import android.util.Log; 22 | import android.util.TypedValue; 23 | import android.view.LayoutInflater; 24 | import android.view.View; 25 | import android.view.ViewGroup; 26 | import android.widget.TextView; 27 | 28 | import com.lsjwzh.test.TestSingleLineTextView; 29 | import com.lsjwzh.widget.text.FastTextLayoutView; 30 | import com.lsjwzh.widget.text.FastTextView; 31 | import com.lsjwzh.widget.text.ReadMoreTextView; 32 | import com.lsjwzh.widget.text.StrokeSpan; 33 | 34 | /** 35 | * A placeholder fragment containing a simple view. 36 | */ 37 | public class EllipseFragment extends Fragment { 38 | 39 | private View mRootView; 40 | 41 | @Override 42 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 43 | Bundle savedInstanceState) { 44 | if (mRootView != null) { 45 | return mRootView; 46 | } 47 | mRootView = inflater.inflate(R.layout.ellipse_demo, container, false); 48 | SpannableStringBuilder spannableStringBuilder = getSpannable(); 49 | FastTextView fastTextView = (FastTextView) mRootView.findViewById(R.id.fast_tv2); 50 | fastTextView.setText(spannableStringBuilder); 51 | // fastTextView.setCustomEllipsisSpan(new ImageSpan(drawable)); 52 | 53 | TextView tv = (TextView) mRootView.findViewById(R.id.system_tv); 54 | tv.setText(spannableStringBuilder); 55 | // tv.setMovementMethod(LinkMovementMethod.getInstance()); 56 | 57 | 58 | return mRootView; 59 | } 60 | 61 | private StaticLayout getStaticLayout(SpannableStringBuilder spannableStringBuilder, TextPaint textPaint, float textSize, Rect bounds, String text) { 62 | long start = SystemClock.elapsedRealtime(); 63 | for (int i = 0; i < 1000; i++) { 64 | textPaint.getTextBounds(text, 0, spannableStringBuilder.length(), bounds); 65 | } 66 | long end = SystemClock.elapsedRealtime(); 67 | float withWithTextBounds = bounds.width(); 68 | Log.d("test", "withWithTextBounds:" + withWithTextBounds + " offset:" + 0.5f * textSize + " cost:" + (end - start)); 69 | 70 | start = SystemClock.elapsedRealtime(); 71 | float withWithMeasureText = 0; 72 | for (int i = 0; i < 1000; i++) { 73 | withWithMeasureText = textPaint.measureText(spannableStringBuilder, 0, spannableStringBuilder.length()); 74 | } 75 | end = SystemClock.elapsedRealtime(); 76 | Log.d("test", "withWithMeasureText:" + withWithMeasureText + " cost:" + (end - start)); 77 | 78 | start = SystemClock.elapsedRealtime(); 79 | float withWithDesiredWidth = 0; 80 | for (int i = 0; i < 1000; i++) { 81 | withWithDesiredWidth = Layout.getDesiredWidth(spannableStringBuilder, textPaint); 82 | } 83 | end = SystemClock.elapsedRealtime(); 84 | Log.d("test", "withWithDesiredWidth:" + withWithDesiredWidth + " cost:" + (end - start)); 85 | 86 | 87 | start = SystemClock.elapsedRealtime(); 88 | for (int i = 0; i < 1000; i++) { 89 | spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), StrokeSpan.class); 90 | } 91 | end = SystemClock.elapsedRealtime(); 92 | Log.d("test", "getSpans cost:" + (end - start)); 93 | 94 | int width = (int) Math.ceil(Math.max(Math.max(withWithTextBounds, withWithMeasureText), withWithDesiredWidth)); 95 | 96 | return StaticLayoutBuilderCompat.obtain(spannableStringBuilder, 0, spannableStringBuilder.length(), 97 | textPaint, Math.min(width, getResources().getDisplayMetrics().widthPixels)) 98 | .setAlignment(Layout.Alignment.ALIGN_NORMAL) 99 | .setLineSpacing(0f, 1f) 100 | .setEllipsize(TextUtils.TruncateAt.END) 101 | .setMaxLines(2).setIncludePad(true).build(); 102 | } 103 | 104 | @NonNull 105 | private SpannableStringBuilder getSpannable() { 106 | SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getResources().getString(R.string.content_cn)); 107 | Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher); 108 | drawable.setBounds(0, 0, 35, 35); 109 | 110 | spannableStringBuilder.setSpan(new ImageSpan(drawable) 111 | , 36, 37, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 112 | 113 | spannableStringBuilder.setSpan(new ImageSpan(drawable) 114 | , 37, 38, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 115 | 116 | spannableStringBuilder.setSpan(new ImageSpan(drawable) 117 | , 38, 39, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 118 | 119 | spannableStringBuilder.setSpan(new ImageSpan(drawable) 120 | , 39, 40, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 121 | // ItalicReplacementSpan italicSpan = new ItalicReplacementSpan(-0.25f); 122 | // StrokeSpan strokeSpan = new StrokeSpan(Color.BLUE, Color.YELLOW, 20); 123 | // spannableStringBuilder.setSpan(strokeSpan, 0, spannableStringBuilder.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 124 | return spannableStringBuilder; 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/FastTextViewListTestFragment.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.os.SystemClock; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.app.Fragment; 8 | import android.util.Log; 9 | import android.util.TypedValue; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | import android.widget.BaseAdapter; 14 | import android.widget.ListView; 15 | import android.widget.TextView; 16 | import android.widget.Toast; 17 | 18 | import com.lsjwzh.test.AutoScrollHandler; 19 | import com.lsjwzh.test.FastTextView; 20 | import com.lsjwzh.test.TestSpan; 21 | import com.lsjwzh.test.TestTextView; 22 | import com.lsjwzh.test.Util; 23 | 24 | public class FastTextViewListTestFragment extends Fragment { 25 | 26 | private ListView listView; 27 | 28 | private ListAdapter adapter; 29 | 30 | private AutoScrollHandler autoScrollHandler; 31 | 32 | @Nullable 33 | @Override 34 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 35 | Bundle savedInstanceState) { 36 | View viewRoot = inflater.inflate(R.layout.normal_layout_ui, container, false); 37 | 38 | listView = (ListView) viewRoot.findViewById(R.id.test_list); 39 | 40 | adapter = new ListAdapter(getActivity()); 41 | 42 | listView.setAdapter(adapter); 43 | 44 | autoScrollHandler = new AutoScrollHandler(listView, Util.TEST_LIST_ITEM_COUNT); 45 | 46 | viewRoot.findViewById(R.id.scroll_down_button).setOnClickListener(new View.OnClickListener() { 47 | @Override 48 | public void onClick(View v) { 49 | FastTextView.TEST_STATS.reset(); 50 | adapter.bindCost = 0; 51 | autoScrollHandler.startAutoScrollDown(new AutoScrollHandler.Callback() { 52 | @Override 53 | public void callback(int fps) { 54 | Toast.makeText(listView.getContext(), "Average FPS: " + fps, Toast.LENGTH_LONG).show(); 55 | Log.e("drawFps", "TestFastTextView.avgFps" + fps); 56 | Log.e("bindCost", "bindCost" + adapter.bindCost); 57 | Log.e("TestFastTextViewStats", FastTextView.TEST_STATS.toString()); 58 | } 59 | }); 60 | } 61 | }); 62 | 63 | viewRoot.findViewById(R.id.scroll_up_button).setOnClickListener(new View.OnClickListener() { 64 | @Override 65 | public void onClick(View v) { 66 | FastTextView.TEST_STATS.reset(); 67 | adapter.bindCost = 0; 68 | autoScrollHandler.startAutoScrollUp(new AutoScrollHandler.Callback() { 69 | @Override 70 | public void callback(int fps) { 71 | Toast.makeText(listView.getContext(), "Average FPS: " + fps, Toast.LENGTH_LONG).show(); 72 | Log.e("drawFps", "TestFastTextView.avgFps" + fps); 73 | Log.e("bindCost", "bindCost" + adapter.bindCost); 74 | Log.e("TestFastTextViewStats", FastTextView.TEST_STATS.toString()); 75 | } 76 | }); 77 | } 78 | }); 79 | return viewRoot; 80 | } 81 | 82 | private static class ListAdapter extends TestListAdapter { 83 | 84 | private ListAdapter(Context context) { 85 | super(context); 86 | } 87 | 88 | @Override 89 | public View bindView(int position, View convertView, ViewGroup parent) { 90 | 91 | if (convertView == null) { 92 | convertView = LayoutInflater.from(context).inflate(R.layout.fast_list_item, parent, 93 | false); 94 | 95 | ViewHolder viewHolder = new ViewHolder(); 96 | viewHolder.textView = (FastTextView) convertView.findViewById(R.id.fast_text_view); 97 | convertView.setTag(viewHolder); 98 | } 99 | 100 | ViewHolder holder = (ViewHolder) convertView.getTag(); 101 | holder.textView.setText(TestSpan.getSpanString(position)); 102 | return convertView; 103 | } 104 | 105 | private class ViewHolder { 106 | FastTextView textView; 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.design.widget.FloatingActionButton; 5 | import android.support.design.widget.Snackbar; 6 | import android.support.v4.app.FragmentActivity; 7 | import android.view.View; 8 | import android.view.Menu; 9 | import android.view.MenuItem; 10 | 11 | import com.lsjwzh.test.FpsCalculator; 12 | import com.lsjwzh.test.GhostThread; 13 | 14 | public class MainActivity extends FragmentActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | // Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 21 | // setSupportActionBar(toolbar); 22 | 23 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 24 | fab.setOnClickListener(new View.OnClickListener() { 25 | @Override 26 | public void onClick(View view) { 27 | getSupportFragmentManager().popBackStackImmediate(); 28 | } 29 | }); 30 | getSupportFragmentManager().beginTransaction() 31 | .replace(R.id.fragment, new MainActivityFragment()) 32 | .commit(); 33 | 34 | // GhostThread.start(); 35 | FpsCalculator.instance().start(); 36 | } 37 | 38 | @Override 39 | public void onBackPressed() { 40 | super.onBackPressed(); 41 | } 42 | 43 | @Override 44 | public boolean onCreateOptionsMenu(Menu menu) { 45 | // Inflate the menu; this adds items to the action bar if it is present. 46 | getMenuInflater().inflate(R.menu.menu_main, menu); 47 | return true; 48 | } 49 | 50 | @Override 51 | public boolean onOptionsItemSelected(MenuItem item) { 52 | // Handle action bar item clicks here. The action bar will 53 | // automatically handle clicks on the Home/Up button, so long 54 | // as you specify a parent activity in AndroidManifest.xml. 55 | int id = item.getItemId(); 56 | 57 | //noinspection SimplifiableIfStatement 58 | if (id == R.id.action_settings) { 59 | return true; 60 | } 61 | 62 | return super.onOptionsItemSelected(item); 63 | } 64 | 65 | @Override 66 | protected void onDestroy() { 67 | super.onDestroy(); 68 | FpsCalculator.instance().stop(); 69 | GhostThread.stop(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/MainActivityFragment.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.graphics.Paint; 4 | import android.os.Bundle; 5 | import android.support.v4.app.Fragment; 6 | import android.text.Layout; 7 | import android.text.StaticLayout; 8 | import android.text.TextLayoutCache; 9 | import android.text.TextLayoutWarmer; 10 | import android.text.TextPaint; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.Toast; 15 | 16 | import com.lsjwzh.test.StaticLayoutManager; 17 | import com.lsjwzh.test.TestSpan; 18 | import com.lsjwzh.test.Util; 19 | 20 | /** 21 | * A placeholder fragment containing a simple view. 22 | */ 23 | public class MainActivityFragment extends Fragment { 24 | 25 | private View mRootView; 26 | 27 | @Override 28 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 29 | Bundle savedInstanceState) { 30 | if (mRootView != null) { 31 | return mRootView; 32 | } 33 | mRootView = inflater.inflate(R.layout.fragment_main, container, false); 34 | mRootView.findViewById(R.id.demo_measure_test).setOnClickListener(new View.OnClickListener() { 35 | @Override 36 | public void onClick(View v) { 37 | getFragmentManager().beginTransaction() 38 | .replace(R.id.fragment, new EllipseFragment()) 39 | .addToBackStack(null) 40 | .commit(); 41 | } 42 | }); 43 | mRootView.findViewById(R.id.demo_ellipsis).setOnClickListener(new View.OnClickListener() { 44 | @Override 45 | public void onClick(View v) { 46 | getFragmentManager().beginTransaction() 47 | .replace(R.id.fragment, new EllipseFragment()) 48 | .addToBackStack(null) 49 | .commitAllowingStateLoss(); 50 | } 51 | }); 52 | mRootView.findViewById(R.id.demo_read_more).setOnClickListener(new View.OnClickListener() { 53 | @Override 54 | public void onClick(View v) { 55 | getFragmentManager().beginTransaction() 56 | .replace(R.id.fragment, new ReadMoreFragment()) 57 | .addToBackStack(null) 58 | .commitAllowingStateLoss(); 59 | } 60 | }); 61 | mRootView.findViewById(R.id.demo_read_more_list).setOnClickListener(new View.OnClickListener() { 62 | @Override 63 | public void onClick(View v) { 64 | getFragmentManager().beginTransaction() 65 | .replace(R.id.fragment, new ReadMoreListTestFragment()) 66 | .addToBackStack(null) 67 | .commitAllowingStateLoss(); 68 | } 69 | }); 70 | 71 | mRootView.findViewById(R.id.prepare_layout_cache).setOnClickListener(new View.OnClickListener 72 | () { 73 | @Override 74 | public void onClick(View v) { 75 | StaticLayoutManager.sLayoutWarmer.setLayoutFactory(new TextLayoutWarmer 76 | .LayoutFactory() { 77 | 78 | @Override 79 | public StaticLayout makeLayout(CharSequence text) { 80 | // 公用TextPaint会导致fps下降? why 81 | final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); 82 | textPaint.setTextSize(Util.fromDPtoPix(getActivity(), Util.TEXT_SIZE_DP)); 83 | int width = (int) Layout.getDesiredWidth(text, textPaint); 84 | return new StaticLayout(text, textPaint, 85 | Math.min(width, Util.getScreenWidth(getActivity())), 86 | Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, true); 87 | } 88 | }); 89 | StaticLayoutManager.sLayoutWarmer.addListener(new TextLayoutWarmer 90 | .WarmListener() { 91 | int count = 0; 92 | 93 | @Override 94 | public void onWarmComplete(CharSequence text, TextLayoutWarmer.WarmerTask 95 | warmerTask) { 96 | TextLayoutCache.STATIC_LAYOUT_CACHE.put(text, warmerTask.mLayout); 97 | count++; 98 | if (count == Util.TEST_LIST_ITEM_COUNT) { 99 | mRootView.post(new Runnable() { 100 | @Override 101 | public void run() { 102 | Toast.makeText(getActivity(), "init layout and span finish", Toast.LENGTH_LONG) 103 | .show(); 104 | } 105 | }); 106 | } 107 | } 108 | }); 109 | 110 | for (int i = 0; i < Util.TEST_LIST_ITEM_COUNT; i++) { 111 | StaticLayoutManager.sLayoutWarmer.addText(TestSpan.getSpanString(i)); 112 | } 113 | // new Thread(new Runnable() { 114 | // @Override 115 | // public void run() { 116 | // StaticLayoutManager.getInstance().initLayout(getActivity(), TestSpan.getSpanString 117 | // (), TestSpan.getLongSpanString()); 118 | // getActivity().runOnUiThread(new Runnable() { 119 | // @Override 120 | // public void run() { 121 | // Toast.makeText(getActivity(), "init layout and span finish", Toast.LENGTH_LONG) 122 | // .show(); 123 | // } 124 | // }); 125 | // } 126 | // }).start(); 127 | 128 | 129 | } 130 | }); 131 | mRootView.findViewById(R.id.demo_layout_cache).setOnClickListener(new View.OnClickListener() { 132 | @Override 133 | public void onClick(View v) { 134 | getFragmentManager().beginTransaction() 135 | .replace(R.id.fragment, new StaticLayoutCacheTestFragment()) 136 | .addToBackStack(null) 137 | .commitAllowingStateLoss(); 138 | } 139 | }); 140 | mRootView.findViewById(R.id.demo_fast_tv).setOnClickListener(new View.OnClickListener() { 141 | @Override 142 | public void onClick(View v) { 143 | getFragmentManager().beginTransaction() 144 | .replace(R.id.fragment, new FastTextViewListTestFragment()) 145 | .addToBackStack(null) 146 | .commitAllowingStateLoss(); 147 | } 148 | }); 149 | mRootView.findViewById(R.id.demo_normal_list).setOnClickListener(new View.OnClickListener() { 150 | @Override 151 | public void onClick(View v) { 152 | getFragmentManager().beginTransaction() 153 | .replace(R.id.fragment, new NormalLayoutTestFragment()) 154 | .addToBackStack(null) 155 | .commitAllowingStateLoss(); 156 | } 157 | }); 158 | TestSpan.init(getActivity()); 159 | return mRootView; 160 | } 161 | 162 | 163 | } 164 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/NormalLayoutTestFragment.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.os.SystemClock; 6 | import android.support.annotation.Nullable; 7 | import android.support.v4.app.Fragment; 8 | import android.text.method.LinkMovementMethod; 9 | import android.util.Log; 10 | import android.util.TypedValue; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.BaseAdapter; 15 | import android.widget.ListView; 16 | import android.widget.TextView; 17 | import android.widget.Toast; 18 | 19 | import com.lsjwzh.test.AutoScrollHandler; 20 | import com.lsjwzh.test.FastTextView; 21 | import com.lsjwzh.test.TestSpan; 22 | import com.lsjwzh.test.TestTextView; 23 | import com.lsjwzh.test.Util; 24 | 25 | public class NormalLayoutTestFragment extends Fragment { 26 | 27 | private ListView listView; 28 | 29 | private NormalListAdapter adapter; 30 | 31 | private AutoScrollHandler autoScrollHandler; 32 | 33 | 34 | @Nullable 35 | @Override 36 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 37 | Bundle savedInstanceState) { 38 | View viewRoot = inflater.inflate(R.layout.normal_layout_ui, container, false); 39 | 40 | listView = (ListView) viewRoot.findViewById(R.id.test_list); 41 | 42 | adapter = new NormalListAdapter(getActivity()); 43 | 44 | listView.setAdapter(adapter); 45 | 46 | autoScrollHandler = new AutoScrollHandler(listView, Util.TEST_LIST_ITEM_COUNT); 47 | 48 | viewRoot.findViewById(R.id.scroll_down_button).setOnClickListener(new View.OnClickListener() { 49 | @Override 50 | public void onClick(View v) { 51 | TestTextView.TEST_STATS.reset(); 52 | adapter.bindCost = 0; 53 | autoScrollHandler.startAutoScrollDown(new AutoScrollHandler.Callback() { 54 | @Override 55 | public void callback(int fps) { 56 | Toast.makeText(listView.getContext(), "Average FPS: " + fps, Toast.LENGTH_LONG).show(); 57 | Log.e("drawFps", "TestTextView.avgFps" + fps); 58 | Log.e("bindCost", "bindCost" + adapter.bindCost); 59 | Log.e("TestTextViewStats", TestTextView.TEST_STATS.toString()); 60 | } 61 | }); 62 | } 63 | }); 64 | 65 | viewRoot.findViewById(R.id.scroll_up_button).setOnClickListener(new View.OnClickListener() { 66 | @Override 67 | public void onClick(View v) { 68 | TestTextView.TEST_STATS.reset(); 69 | adapter.bindCost = 0; 70 | autoScrollHandler.startAutoScrollUp(new AutoScrollHandler.Callback() { 71 | @Override 72 | public void callback(int fps) { 73 | Toast.makeText(listView.getContext(), "Average FPS: " + fps, Toast.LENGTH_LONG).show(); 74 | Log.e("drawFps", "TestTextView.avgFps" + fps); 75 | Log.e("bindCost", "bindCost" + adapter.bindCost); 76 | Log.e("TestTextViewStats", TestTextView.TEST_STATS.toString()); 77 | 78 | } 79 | }); 80 | } 81 | }); 82 | return viewRoot; 83 | } 84 | 85 | private static class NormalListAdapter extends TestListAdapter { 86 | 87 | private NormalListAdapter(Context context) { 88 | super(context); 89 | } 90 | 91 | @Override 92 | public View bindView(int position, View convertView, ViewGroup parent) { 93 | 94 | if (convertView == null) { 95 | convertView = LayoutInflater.from(context).inflate(R.layout.normal_list_item, parent, 96 | false); 97 | 98 | ViewHolder viewHolder = new ViewHolder(); 99 | viewHolder.textView = (TextView) convertView.findViewById(R.id.normal_text); 100 | 101 | convertView.setTag(viewHolder); 102 | } 103 | 104 | ViewHolder holder = (ViewHolder) convertView.getTag(); 105 | holder.textView.setMovementMethod(LinkMovementMethod.getInstance()); 106 | holder.textView.setText(TestSpan.getSpanString(position)); 107 | return convertView; 108 | } 109 | 110 | private class ViewHolder { 111 | TextView textView; 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/ReadMoreFragment.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.graphics.Canvas; 4 | import android.graphics.Color; 5 | import android.graphics.Paint; 6 | import android.graphics.drawable.Drawable; 7 | import android.os.Bundle; 8 | import android.support.annotation.IntRange; 9 | import android.support.annotation.NonNull; 10 | import android.support.v4.app.Fragment; 11 | import android.text.SpannableStringBuilder; 12 | import android.text.Spanned; 13 | import android.text.TextPaint; 14 | import android.text.style.ClickableSpan; 15 | import android.text.style.ImageSpan; 16 | import android.view.LayoutInflater; 17 | import android.view.View; 18 | import android.view.ViewGroup; 19 | import android.widget.Toast; 20 | 21 | import com.lsjwzh.widget.text.ReadMoreTextView; 22 | 23 | /** 24 | * A placeholder fragment containing a simple view. 25 | */ 26 | public class ReadMoreFragment extends Fragment { 27 | 28 | private View mRootView; 29 | 30 | @Override 31 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 32 | Bundle savedInstanceState) { 33 | if (mRootView != null) { 34 | return mRootView; 35 | } 36 | mRootView = inflater.inflate(R.layout.read_more_demo, container, false); 37 | SpannableStringBuilder spannableStringBuilder = getSpannable(); 38 | final ReadMoreTextView readMoreTextView = mRootView.findViewById(R.id.readmore_tv); 39 | readMoreTextView.setText(spannableStringBuilder); 40 | readMoreTextView.setCustomEllipsisSpan(new ReadMoreTextView.EllipsisSpan(" Read More") { 41 | @Override 42 | public void draw(@NonNull Canvas canvas, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, float x, int top, int y, int bottom, @NonNull Paint paint) { 43 | int oldColor = paint.getColor(); 44 | paint.setColor(Color.RED); 45 | super.draw(canvas, text, start, end, x, top, y, bottom, paint); 46 | paint.setColor(oldColor); 47 | } 48 | }); 49 | readMoreTextView.setCustomCollapseSpan(new ReadMoreTextView.EllipsisSpan(" Collapse") { 50 | @Override 51 | public void draw(@NonNull Canvas canvas, CharSequence text, @IntRange(from = 0) int start, @IntRange(from = 0) int end, float x, int top, int y, int bottom, @NonNull Paint paint) { 52 | int oldColor = paint.getColor(); 53 | paint.setColor(Color.RED); 54 | super.draw(canvas, text, start, end, x, top, y, bottom, paint); 55 | paint.setColor(oldColor); 56 | } 57 | }); 58 | readMoreTextView.setOnLongClickListener(new View.OnLongClickListener() { 59 | @Override 60 | public boolean onLongClick(View v) { 61 | readMoreTextView.setTextColor(Color.RED); 62 | return false; 63 | } 64 | }); 65 | return mRootView; 66 | } 67 | 68 | @NonNull 69 | private SpannableStringBuilder getSpannable() { 70 | SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(getResources().getString(R.string.content_cn)); 71 | return spannableStringBuilder; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/ReadMoreListTestFragment.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | import android.util.Log; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.ListView; 12 | import android.widget.Toast; 13 | 14 | import com.lsjwzh.test.AutoScrollHandler; 15 | import com.lsjwzh.test.FastTextView; 16 | import com.lsjwzh.test.TestSpan; 17 | import com.lsjwzh.test.Util; 18 | import com.lsjwzh.widget.text.ReadMoreTextView; 19 | 20 | public class ReadMoreListTestFragment extends Fragment { 21 | 22 | private ListView listView; 23 | 24 | private ListAdapter adapter; 25 | 26 | private AutoScrollHandler autoScrollHandler; 27 | 28 | @Nullable 29 | @Override 30 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 31 | Bundle savedInstanceState) { 32 | View viewRoot = inflater.inflate(R.layout.normal_layout_ui, container, false); 33 | 34 | listView = (ListView) viewRoot.findViewById(R.id.test_list); 35 | 36 | adapter = new ListAdapter(getActivity()); 37 | 38 | listView.setAdapter(adapter); 39 | 40 | autoScrollHandler = new AutoScrollHandler(listView, Util.TEST_LIST_ITEM_COUNT); 41 | 42 | viewRoot.findViewById(R.id.scroll_down_button).setOnClickListener(new View.OnClickListener() { 43 | @Override 44 | public void onClick(View v) { 45 | FastTextView.TEST_STATS.reset(); 46 | adapter.bindCost = 0; 47 | autoScrollHandler.startAutoScrollDown(new AutoScrollHandler.Callback() { 48 | @Override 49 | public void callback(int fps) { 50 | Toast.makeText(listView.getContext(), "Average FPS: " + fps, Toast.LENGTH_LONG).show(); 51 | Log.e("drawFps", "TestFastTextView.avgFps" + fps); 52 | Log.e("bindCost", "bindCost" + adapter.bindCost); 53 | Log.e("TestFastTextViewStats", FastTextView.TEST_STATS.toString()); 54 | } 55 | }); 56 | } 57 | }); 58 | 59 | viewRoot.findViewById(R.id.scroll_up_button).setOnClickListener(new View.OnClickListener() { 60 | @Override 61 | public void onClick(View v) { 62 | FastTextView.TEST_STATS.reset(); 63 | adapter.bindCost = 0; 64 | autoScrollHandler.startAutoScrollUp(new AutoScrollHandler.Callback() { 65 | @Override 66 | public void callback(int fps) { 67 | Toast.makeText(listView.getContext(), "Average FPS: " + fps, Toast.LENGTH_LONG).show(); 68 | Log.e("drawFps", "TestFastTextView.avgFps" + fps); 69 | Log.e("bindCost", "bindCost" + adapter.bindCost); 70 | Log.e("TestFastTextViewStats", FastTextView.TEST_STATS.toString()); 71 | } 72 | }); 73 | } 74 | }); 75 | return viewRoot; 76 | } 77 | 78 | private static class ListAdapter extends TestListAdapter { 79 | 80 | private ListAdapter(Context context) { 81 | super(context); 82 | } 83 | 84 | @Override 85 | public View bindView(int position, View convertView, ViewGroup parent) { 86 | 87 | if (convertView == null) { 88 | convertView = LayoutInflater.from(context).inflate(R.layout.readmore_list_item, parent, 89 | false); 90 | 91 | ViewHolder viewHolder = new ViewHolder(); 92 | viewHolder.textView = (ReadMoreTextView) convertView.findViewById(R.id.fast_text_view); 93 | 94 | convertView.setTag(viewHolder); 95 | } 96 | 97 | ViewHolder holder = (ViewHolder) convertView.getTag(); 98 | holder.textView.setText(TestSpan.getSpanString(position)); 99 | return convertView; 100 | } 101 | 102 | private class ViewHolder { 103 | ReadMoreTextView textView; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/StaticLayoutCacheTestFragment.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | import android.util.Log; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.ListView; 12 | import android.widget.Toast; 13 | 14 | import com.lsjwzh.test.AutoScrollHandler; 15 | import com.lsjwzh.test.StaticLayoutManager; 16 | import com.lsjwzh.test.Util; 17 | import com.lsjwzh.widget.text.FastTextLayoutView; 18 | 19 | public class StaticLayoutCacheTestFragment extends Fragment { 20 | 21 | private static final String TAG = "StaticLayoutUI"; 22 | 23 | private ListView listview; 24 | 25 | private StaticListAdapter adapter; 26 | 27 | private AutoScrollHandler autoScrollHandler; 28 | 29 | @Nullable 30 | @Override 31 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 32 | Bundle savedInstanceState) { 33 | View viewRoot = inflater.inflate(R.layout.static_layout_ui, container, false); 34 | listview = (ListView) viewRoot.findViewById(R.id.test_list); 35 | 36 | adapter = new StaticListAdapter(getActivity()); 37 | 38 | listview.setAdapter(adapter); 39 | 40 | autoScrollHandler = new AutoScrollHandler(listview, Util.TEST_LIST_ITEM_COUNT); 41 | 42 | viewRoot.findViewById(R.id.scroll_down_button).setOnClickListener(new View.OnClickListener() { 43 | @Override 44 | public void onClick(View v) { 45 | com.lsjwzh.test.FastTextLayoutView.TEST_STATS.reset(); 46 | autoScrollHandler.startAutoScrollDown(new AutoScrollHandler.Callback() { 47 | @Override 48 | public void callback(int avgFps) { 49 | Toast.makeText(getActivity(), "Average FPS: " + avgFps, Toast.LENGTH_LONG).show(); 50 | Log.e("drawFps", "FastTextLayoutView.avgFps" + avgFps); 51 | Log.e("bindCost", "bindCost" + adapter.bindCost); 52 | Log.e("FastTextLayoutViewStats", com.lsjwzh.test.FastTextLayoutView.TEST_STATS 53 | .toString()); 54 | } 55 | }); 56 | } 57 | }); 58 | 59 | viewRoot.findViewById(R.id.scroll_up_button).setOnClickListener(new View.OnClickListener() { 60 | @Override 61 | public void onClick(View v) { 62 | com.lsjwzh.test.FastTextLayoutView.TEST_STATS.reset(); 63 | autoScrollHandler.startAutoScrollUp(new AutoScrollHandler.Callback() { 64 | @Override 65 | public void callback(int avgFps) { 66 | Toast.makeText(getActivity(), "Average FPS: " + avgFps, Toast.LENGTH_LONG).show(); 67 | Log.e("drawFps", "FastTextLayoutView.avgFps" + avgFps); 68 | Log.e("bindCost", "bindCost" + adapter.bindCost); 69 | Log.e("FastTextLayoutViewStats", com.lsjwzh.test.FastTextLayoutView.TEST_STATS 70 | .toString()); 71 | } 72 | }); 73 | } 74 | }); 75 | 76 | return viewRoot; 77 | } 78 | 79 | private static class StaticListAdapter extends TestListAdapter { 80 | 81 | private StaticListAdapter(Context context) { 82 | super(context); 83 | } 84 | 85 | @Override 86 | public View bindView(int position, View convertView, ViewGroup parent) { 87 | if (convertView == null) { 88 | convertView = LayoutInflater.from(context).inflate(R.layout.static_list_item, parent, 89 | false); 90 | 91 | ViewHolder viewHolder = new ViewHolder(); 92 | viewHolder.staticLayoutView = (FastTextLayoutView) convertView.findViewById(R.id 93 | .static_layout_view); 94 | 95 | convertView.setTag(viewHolder); 96 | } 97 | 98 | ViewHolder holder = (ViewHolder) convertView.getTag(); 99 | holder.staticLayoutView.setTextLayout(StaticLayoutManager.getInstance().getLayout(position)); 100 | holder.staticLayoutView.requestLayout(); 101 | return convertView; 102 | } 103 | 104 | private class ViewHolder { 105 | FastTextLayoutView staticLayoutView; 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /app/src/main/java/com/wechat/testdemo/TestListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.wechat.testdemo; 2 | 3 | import android.content.Context; 4 | import android.os.SystemClock; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | 9 | import com.lsjwzh.test.Util; 10 | 11 | public abstract class TestListAdapter extends BaseAdapter { 12 | 13 | protected Context context; 14 | public long bindCost = 0; 15 | 16 | public TestListAdapter(Context context) { 17 | this.context = context; 18 | } 19 | 20 | @Override 21 | public int getCount() { 22 | return Util.TEST_LIST_ITEM_COUNT; 23 | } 24 | 25 | @Override 26 | public Object getItem(int position) { 27 | return position; 28 | } 29 | 30 | @Override 31 | public long getItemId(int position) { 32 | return position; 33 | } 34 | 35 | @Override 36 | public final View getView(int position, View convertView, ViewGroup parent) { 37 | long start = SystemClock.elapsedRealtime(); 38 | convertView = bindView(position, convertView, parent); 39 | long end = SystemClock.elapsedRealtime(); 40 | bindCost += (end - start); 41 | return convertView; 42 | } 43 | 44 | 45 | public abstract View bindView(int position, View convertView, ViewGroup parent); 46 | 47 | } -------------------------------------------------------------------------------- /app/src/main/res/anim/popup_enter.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/anim/popup_exit.xml: -------------------------------------------------------------------------------- 1 | 2 | 16 | 18 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lsjwzh/FastTextView/c6234059f97c3583cbbcc69b3cbf4efe755136a3/app/src/main/res/drawable-hdpi/test.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_ab_back_holo_dark_am.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lsjwzh/FastTextView/c6234059f97c3583cbbcc69b3cbf4efe755136a3/app/src/main/res/drawable-xhdpi/ic_ab_back_holo_dark_am.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/popup_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 14 | 15 | 21 | 22 | 23 | 24 | 29 | 30 | 31 | 32 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /app/src/main/res/layout/ellipse_demo.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 20 | 21 | 29 | 30 | 31 | 35 | 36 | 46 | 47 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fast_layout_ui.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 12 | 13 |