├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── drawable-hdpi
│ │ │ │ └── test.png
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── styles.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── strings.xml
│ │ │ ├── layout
│ │ │ │ ├── normal_list_item.xml
│ │ │ │ ├── static_list_item.xml
│ │ │ │ ├── activity_normal_long_string_ui.xml
│ │ │ │ ├── activity_static_long_string_ui.xml
│ │ │ │ ├── activity_static_layout_ui.xml
│ │ │ │ ├── activity_normal_layout_ui.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ └── menu
│ │ │ │ ├── menu_main.xml
│ │ │ │ ├── menu_normal_layout_ui.xml
│ │ │ │ ├── menu_static_layout_ui.xml
│ │ │ │ ├── menu_normal_long_string_ui.xml
│ │ │ │ └── menu_static_long_string_ui.xml
│ │ ├── gen
│ │ │ └── com
│ │ │ │ └── ragnarok
│ │ │ │ └── staticlayouttest
│ │ │ │ └── app
│ │ │ │ ├── R.java
│ │ │ │ ├── Manifest.java
│ │ │ │ └── BuildConfig.java
│ │ ├── main.iml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── ragnarok
│ │ │ │ └── staticlayoutview
│ │ │ │ ├── util
│ │ │ │ ├── Util.java
│ │ │ │ ├── GhostThread.java
│ │ │ │ ├── FpsCalculator.java
│ │ │ │ └── TestSpan.java
│ │ │ │ └── ui
│ │ │ │ ├── NormalLongStringUI.java
│ │ │ │ ├── StaticLongStringUI.java
│ │ │ │ ├── StaticLayoutView.java
│ │ │ │ ├── StaticLayoutManager.java
│ │ │ │ ├── AutoScrollHandler.java
│ │ │ │ ├── NormalLayoutUI.java
│ │ │ │ ├── StaticLayoutUI.java
│ │ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
│ └── androidTest
│ │ ├── java
│ │ └── com
│ │ │ └── ragnarok
│ │ │ └── staticlayouttest
│ │ │ └── app
│ │ │ └── ApplicationTest.java
│ │ └── androidTest1.iml
├── proguard-rules.pro
├── build.gradle
└── app.iml
├── settings.gradle
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── README.md
├── gradle.properties
├── StaticLayoutView.iml
├── LICENSE
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | local.properties
3 | .idea/
4 | .idea/workspace.xml
5 | .idea/libraries
6 | .DS_Store
7 | build/
8 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ragnraok/StaticLayoutView/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/test.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ragnraok/StaticLayoutView/HEAD/app/src/main/res/drawable-hdpi/test.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ragnraok/StaticLayoutView/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ragnraok/StaticLayoutView/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ragnraok/StaticLayoutView/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ragnraok/StaticLayoutView/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/gen/com/ragnarok/staticlayouttest/app/R.java:
--------------------------------------------------------------------------------
1 | /*___Generated_by_IDEA___*/
2 |
3 | package com.ragnarok.staticlayouttest.app;
4 |
5 | /* This stub is only used by the IDE. It is NOT the R class actually packed into the APK */
6 | public final class R {
7 | }
--------------------------------------------------------------------------------
/app/src/main/gen/com/ragnarok/staticlayouttest/app/Manifest.java:
--------------------------------------------------------------------------------
1 | /*___Generated_by_IDEA___*/
2 |
3 | package com.ragnarok.staticlayouttest.app;
4 |
5 | /* This stub is only used by the IDE. It is NOT the Manifest class actually packed into the APK */
6 | public final class Manifest {
7 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Apr 10 15:27:10 PDT 2013
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/normal_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/static_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/gen/com/ragnarok/staticlayouttest/app/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /*___Generated_by_IDEA___*/
2 |
3 | package com.ragnarok.staticlayouttest.app;
4 |
5 | /* This stub is only used by the IDE. It is NOT the BuildConfig class actually packed into the APK */
6 | public final class BuildConfig {
7 | public final static boolean DEBUG = Boolean.parseBoolean(null);
8 | }
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/ragnarok/staticlayouttest/app/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayouttest.app;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_normal_layout_ui.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_static_layout_ui.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/androidTest/androidTest1.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_normal_long_string_ui.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_static_long_string_ui.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | StaticLayoutTest
3 |
4 | Hello world!
5 | Settings
6 | StaticLayoutUI
7 | NormalLayoutUI
8 | StaticLongStringUI
9 | NormalLongStringUI
10 |
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # StaticLayoutView
2 | a pre-render TextView demo
3 |
4 | This demo use StaticLayout to render text in a custom TextView and create the StaticLayout in another thread before enter the UI, and these StaticLayout draw in a dummy canvas which warm up the TextLayoutCache in the system. And these optimization improve the performance of showing complicated text(which may include many spans, emoji) in a ListView
5 |
6 | for the detail of this project, please read this post(in chinese): [http://ragnraok.github.io/textview-pre-render-research.html](http://ragnraok.github.io/textview-pre-render-research.html)
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_normal_long_string_ui.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_static_long_string_ui.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/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/ragnarok/adt-bundle-mac-x86_64-20131030/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 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/StaticLayoutView.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | jcenter()
4 | }
5 | dependencies {
6 | classpath 'com.android.tools.build:gradle:1.1.1'
7 | }
8 | }
9 | apply plugin: 'com.android.application'
10 |
11 | repositories {
12 | jcenter()
13 | }
14 |
15 | android {
16 | compileSdkVersion 22
17 | buildToolsVersion "22.0.1"
18 |
19 | defaultConfig {
20 | applicationId "com.ragnarok.staticlayouttest.app"
21 | minSdkVersion 15
22 | targetSdkVersion 22
23 | versionCode 1
24 | versionName "1.0"
25 | }
26 | buildTypes {
27 | release {
28 | minifyEnabled false
29 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
30 | }
31 | }
32 | }
33 |
34 | dependencies {
35 | compile fileTree(dir: 'libs', include: ['*.jar'])
36 | compile 'com.android.support:appcompat-v7:22.2.0'
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/main/main.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/util/Util.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.util;
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 = 300;
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 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 RagnarokStack
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_static_layout_ui.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
13 |
14 |
20 |
21 |
26 |
27 |
28 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_normal_layout_ui.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
13 |
14 |
20 |
21 |
26 |
27 |
28 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
22 |
23 |
26 |
27 |
30 |
31 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/util/GhostThread.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.util;
2 |
3 | /**
4 | * Created by ragnarok on 15/7/22.
5 | * The GhostThread is used to simulate the heavily jobs behind the UI thread in the
6 | * real world apps
7 | * Currently, the implementation is work by launch several threads and calculate floating point
8 | * multiplication infinity
9 | */
10 | public class GhostThread {
11 | private static final String TAG = "GhostThread";
12 |
13 | private static boolean isStart = false;
14 |
15 | private static Thread[] threads = new Thread[3];
16 |
17 | private static Runnable runnable = new Runnable() {
18 | @Override
19 | public void run() {
20 | for (; ;) {
21 | if (!isStart) {
22 | break;
23 | }
24 | double c = Math.PI * Math.PI * Math.PI;
25 | }
26 | }
27 | };
28 |
29 | public static void start() {
30 | if (isStart) {
31 | return;
32 | }
33 | isStart = true;
34 | for (int i = 0; i < threads.length; i++) {
35 | Thread thread = new Thread(runnable);
36 | thread.setPriority(Thread.NORM_PRIORITY);
37 | thread.start();
38 | threads[i] = thread;
39 | }
40 | }
41 |
42 | public static void stop() {
43 | isStart = false;
44 | for (Thread thread : threads) {
45 | try {
46 | thread.join();
47 | } catch (InterruptedException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/NormalLongStringUI.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
2 |
3 | import android.support.v7.app.ActionBarActivity;
4 | import android.os.Bundle;
5 | import android.view.Menu;
6 | import android.view.MenuItem;
7 | import android.widget.TextView;
8 | import com.ragnarok.staticlayouttest.app.R;
9 | import com.ragnarok.staticlayoutview.util.TestSpan;
10 |
11 | public class NormalLongStringUI extends ActionBarActivity {
12 |
13 | private TextView textView;
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_normal_long_string_ui);
19 |
20 | textView = (TextView) findViewById(R.id.text);
21 | textView.setText(TestSpan.getLongSpanString());
22 | }
23 |
24 | @Override
25 | protected void onStop() {
26 | super.onStop();
27 | }
28 |
29 | @Override
30 | public boolean onCreateOptionsMenu(Menu menu) {
31 | // Inflate the menu; this adds items to the action bar if it is present.
32 | getMenuInflater().inflate(R.menu.menu_normal_long_string_ui, menu);
33 | return true;
34 | }
35 |
36 | @Override
37 | public boolean onOptionsItemSelected(MenuItem item) {
38 | // Handle action bar item clicks here. The action bar will
39 | // automatically handle clicks on the Home/Up button, so long
40 | // as you specify a parent activity in AndroidManifest.xml.
41 | int id = item.getItemId();
42 |
43 | //noinspection SimplifiableIfStatement
44 | if (id == R.id.action_settings) {
45 | return true;
46 | }
47 |
48 | return super.onOptionsItemSelected(item);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/StaticLongStringUI.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
2 |
3 | import android.support.v7.app.ActionBarActivity;
4 | import android.os.Bundle;
5 | import android.view.Menu;
6 | import android.view.MenuItem;
7 | import com.ragnarok.staticlayouttest.app.R;
8 |
9 | public class StaticLongStringUI extends ActionBarActivity {
10 |
11 | private StaticLayoutView staticLayoutView;
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.activity_static_long_string_ui);
17 |
18 | staticLayoutView = (StaticLayoutView) findViewById(R.id.static_layout_view);
19 |
20 | staticLayoutView.setLayout(StaticLayoutManager.getInstance().getLongStringLayout());
21 | }
22 |
23 | @Override
24 | protected void onStop() {
25 | super.onStop();
26 | }
27 |
28 | @Override
29 | public boolean onCreateOptionsMenu(Menu menu) {
30 | // Inflate the menu; this adds items to the action bar if it is present.
31 | getMenuInflater().inflate(R.menu.menu_static_long_string_ui, menu);
32 | return true;
33 | }
34 |
35 | @Override
36 | public boolean onOptionsItemSelected(MenuItem item) {
37 | // Handle action bar item clicks here. The action bar will
38 | // automatically handle clicks on the Home/Up button, so long
39 | // as you specify a parent activity in AndroidManifest.xml.
40 | int id = item.getItemId();
41 |
42 | //noinspection SimplifiableIfStatement
43 | if (id == R.id.action_settings) {
44 | return true;
45 | }
46 |
47 | return super.onOptionsItemSelected(item);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
16 |
17 |
23 |
24 |
30 |
31 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/StaticLayoutView.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.text.Layout;
6 | import android.util.AttributeSet;
7 | import android.view.View;
8 |
9 | /**
10 | * Created by ragnarok on 15/7/16.
11 | */
12 | public class StaticLayoutView extends View {
13 |
14 | private Layout layout = null;
15 |
16 | private int width;
17 | private int height;
18 |
19 | public StaticLayoutView(Context context) {
20 | super(context);
21 | }
22 |
23 | public StaticLayoutView(Context context, AttributeSet attrs) {
24 | super(context, attrs);
25 | }
26 |
27 | public StaticLayoutView(Context context, AttributeSet attrs, int defStyleAttr) {
28 | super(context, attrs, defStyleAttr);
29 | }
30 |
31 | public void setLayout(Layout layout) {
32 | this.layout = layout;
33 | if (this.layout.getWidth() != width || this.layout.getHeight() != height) {
34 | width = this.layout.getWidth();
35 | height = this.layout.getHeight();
36 | requestLayout();
37 | }
38 | }
39 |
40 | @Override
41 | protected void onDraw(Canvas canvas) {
42 | super.onDraw(canvas);
43 |
44 | canvas.save();
45 | if (layout != null) {
46 | layout.draw(canvas, null, null, 0);
47 | }
48 | canvas.restore();
49 | }
50 |
51 | @Override
52 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
53 | super.onLayout(changed, left, top, right, bottom);
54 | }
55 |
56 | @Override
57 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
58 | if (layout != null) {
59 | setMeasuredDimension(layout.getWidth(), layout.getHeight());
60 | } else {
61 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
62 | }
63 |
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/StaticLayoutManager.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
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.*;
9 | import com.ragnarok.staticlayoutview.util.TestSpan;
10 | import com.ragnarok.staticlayoutview.util.Util;
11 |
12 | /**
13 | * Created by ragnarok on 15/7/21.
14 | */
15 | public class StaticLayoutManager {
16 |
17 | private StaticLayout[] layout = new StaticLayout[Util.TEST_LIST_ITEM_COUNT];
18 |
19 | private StaticLayout longStringLayout;
20 |
21 | private TextPaint textPaint;
22 | private TextDirectionHeuristic textDir;
23 | private Layout.Alignment alignment;
24 |
25 | private Canvas dummyCanvas;
26 |
27 | private int hardCodeWidth;
28 |
29 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
30 | public void initLayout(Context context, CharSequence source, CharSequence longString) {
31 |
32 | textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
33 | textPaint.density = context.getResources().getDisplayMetrics().density;
34 | textPaint.setTextSize(Util.fromDPtoPix(context, Util.TEXT_SIZE_DP));
35 |
36 | textDir = TextDirectionHeuristics.LTR;
37 |
38 | alignment = Layout.Alignment.ALIGN_NORMAL;
39 |
40 | hardCodeWidth = Util.getScreenWidth(context);
41 |
42 | longStringLayout = new StaticLayout(longString, textPaint, hardCodeWidth, alignment, 1.0f, 0f, true);
43 |
44 | dummyCanvas = new Canvas();
45 |
46 | longStringLayout.draw(dummyCanvas);
47 |
48 | for (int i = 0; i < layout.length; i++) {
49 | layout[i] = new StaticLayout(TestSpan.getSpanString(i), textPaint, hardCodeWidth, alignment, 1.0f, 0f, true);
50 | layout[i].draw(dummyCanvas);
51 | }
52 | }
53 |
54 | public StaticLayout getLayout(int index) {
55 | return layout[index];
56 | }
57 |
58 | public StaticLayout getLongStringLayout() {
59 | return longStringLayout;
60 | }
61 |
62 | private static StaticLayoutManager INSTANCE = null;
63 |
64 | public static StaticLayoutManager getInstance() {
65 | if (INSTANCE == null) {
66 | INSTANCE = new StaticLayoutManager();
67 | }
68 | return INSTANCE;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/AutoScrollHandler.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
2 |
3 | import android.os.Handler;
4 | import android.os.Looper;
5 | import android.widget.ListView;
6 | import android.widget.Toast;
7 | import com.ragnarok.staticlayoutview.util.FpsCalculator;
8 | import com.ragnarok.staticlayoutview.util.Util;
9 |
10 | import java.util.Timer;
11 | import java.util.TimerTask;
12 |
13 | /**
14 | * Created by ragnarok on 15/7/22.
15 | */
16 | public class AutoScrollHandler {
17 |
18 | private ListView listView;
19 |
20 | private int itemCount;
21 |
22 | private Handler uiHandler = new Handler(Looper.getMainLooper());
23 |
24 | public AutoScrollHandler(ListView listView, int itemCount) {
25 | this.listView = listView;
26 | this.itemCount = itemCount;
27 | }
28 |
29 | public void startAutoScrollDown() {
30 | final Timer timer = new Timer();
31 | FpsCalculator.instance().startCalculate();
32 | timer.scheduleAtFixedRate(new TimerTask() {
33 | @Override
34 | public void run() {
35 | if (listView.getLastVisiblePosition() >= itemCount - 1) {
36 | final int avgFps = FpsCalculator.instance().stopGetAvgFPS();
37 | uiHandler.post(new Runnable() {
38 | @Override
39 | public void run() {
40 | Toast.makeText(listView.getContext(), "Average FPS: " + avgFps, Toast.LENGTH_LONG).show();
41 | }
42 | });
43 | timer.cancel();
44 | }
45 | uiHandler.post(new Runnable() {
46 | @Override
47 | public void run() {
48 | listView.smoothScrollByOffset(Util.AUTO_SCROLL_STEP);
49 | }
50 | });
51 | }
52 | }, 0, Util.AUTO_SCROLL_INTERVAL);
53 |
54 | }
55 |
56 | public void startAutoScrollUp() {
57 | final Timer timer = new Timer();
58 | FpsCalculator.instance().startCalculate();
59 | timer.scheduleAtFixedRate(new TimerTask() {
60 | @Override
61 | public void run() {
62 | if (listView.getFirstVisiblePosition() <= 1) {
63 | final int avgFps = FpsCalculator.instance().stopGetAvgFPS();
64 | uiHandler.post(new Runnable() {
65 | @Override
66 | public void run() {
67 | Toast.makeText(listView.getContext(), "Average FPS: " + avgFps, Toast.LENGTH_LONG).show();
68 | }
69 | });
70 | timer.cancel();
71 | }
72 | uiHandler.post(new Runnable() {
73 | @Override
74 | public void run() {
75 | listView.smoothScrollByOffset(-Util.AUTO_SCROLL_STEP);
76 | }
77 | });
78 | }
79 | }, 0, Util.AUTO_SCROLL_INTERVAL);
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/util/FpsCalculator.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.util;
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/ragnarok/staticlayoutview/util/TestSpan.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.util;
2 |
3 | import android.content.Context;
4 | import android.graphics.BitmapFactory;
5 | import android.text.SpannableString;
6 | import android.text.Spanned;
7 | import android.text.style.ImageSpan;
8 | import com.ragnarok.staticlayouttest.app.R;
9 |
10 | import java.util.Random;
11 |
12 | /**
13 | * Created by ragnarok on 15/7/21.
14 | */
15 | public class TestSpan {
16 |
17 | private static SpannableString testString =
18 | new SpannableString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGH" +
19 | "IJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
20 |
21 | private static SpannableString testLongString =
22 | new SpannableString("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" +
23 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" +
24 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" +
25 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" +
26 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" +
27 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" +
28 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n" +
29 | "AAAAAAAAAAAAAAAAAAAAAAAAAAAA");
30 |
31 | private static SpannableString[] allTestStrings = new SpannableString[Util.TEST_LIST_ITEM_COUNT];
32 |
33 | public static void init(Context context) {
34 | for (int i = 0; i <= testString.length(); i++) {
35 | if (i + 1 <= testString.length()) {
36 | ImageSpan imgSpan = new ImageSpan(context, BitmapFactory.decodeResource(context.getResources(), R.drawable.test));
37 | testString.setSpan(imgSpan, i, i + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
38 | }
39 | }
40 |
41 | Random random = new Random();
42 |
43 | for (int i = 0; i < allTestStrings.length; i++) {
44 | int start = random.nextInt(testString.length() / 2);
45 | int len = random.nextInt(testString.length());
46 | len = len > 1 ? len : 1;
47 | int end = start + len > testString.length() ? testString.length() : start + len;
48 | allTestStrings[i] = (SpannableString) testString.subSequence(start, end);
49 | }
50 |
51 | for (int i = 0; i < testLongString.length(); i++) {
52 | if (i + 1 < testLongString.length()) {
53 | ImageSpan imgSpan = new ImageSpan(context, BitmapFactory.decodeResource(context.getResources(), R.drawable.test));
54 | testLongString.setSpan(imgSpan, i, i + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
55 | }
56 | }
57 | }
58 |
59 | public static SpannableString getSpanString(int index) {
60 | return allTestStrings[index];
61 | }
62 |
63 | public static SpannableString getSpanString() {
64 | return testString;
65 | }
66 |
67 | public static SpannableString getLongSpanString() {
68 | return testLongString;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/NormalLayoutUI.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.v7.app.ActionBarActivity;
6 | import android.util.TypedValue;
7 | import android.view.*;
8 | import android.widget.BaseAdapter;
9 | import android.widget.ListView;
10 | import android.widget.TextView;
11 | import com.ragnarok.staticlayouttest.app.R;
12 | import com.ragnarok.staticlayoutview.util.TestSpan;
13 | import com.ragnarok.staticlayoutview.util.Util;
14 |
15 | public class NormalLayoutUI extends ActionBarActivity {
16 |
17 | private ListView listView;
18 |
19 | private NormalListAdapter adapter;
20 |
21 | private AutoScrollHandler autoScrollHandler;
22 |
23 | @Override
24 | protected void onCreate(Bundle savedInstanceState) {
25 | super.onCreate(savedInstanceState);
26 | setContentView(R.layout.activity_normal_layout_ui);
27 |
28 | listView = (ListView) findViewById(R.id.test_list);
29 |
30 | adapter = new NormalListAdapter(this);
31 |
32 | listView.setAdapter(adapter);
33 |
34 | autoScrollHandler = new AutoScrollHandler(listView, Util.TEST_LIST_ITEM_COUNT);
35 |
36 | findViewById(R.id.scroll_down_button).setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View v) {
39 | autoScrollHandler.startAutoScrollDown();
40 | }
41 | });
42 |
43 | findViewById(R.id.scroll_up_button).setOnClickListener(new View.OnClickListener() {
44 | @Override
45 | public void onClick(View v) {
46 | autoScrollHandler.startAutoScrollUp();
47 | }
48 | });
49 | }
50 |
51 | @Override
52 | protected void onStop() {
53 | super.onStop();
54 | }
55 |
56 | @Override
57 | public boolean onCreateOptionsMenu(Menu menu) {
58 | // Inflate the menu; this adds items to the action bar if it is present.
59 | getMenuInflater().inflate(R.menu.menu_normal_layout_ui, menu);
60 | return true;
61 | }
62 |
63 | @Override
64 | public boolean onOptionsItemSelected(MenuItem item) {
65 | // Handle action bar item clicks here. The action bar will
66 | // automatically handle clicks on the Home/Up button, so long
67 | // as you specify a parent activity in AndroidManifest.xml.
68 | int id = item.getItemId();
69 |
70 | //noinspection SimplifiableIfStatement
71 | if (id == R.id.action_settings) {
72 | return true;
73 | }
74 |
75 | return super.onOptionsItemSelected(item);
76 | }
77 |
78 | private static class NormalListAdapter extends BaseAdapter {
79 |
80 | private Context context;
81 |
82 | public NormalListAdapter(Context context) {
83 | this.context = context;
84 | }
85 |
86 | @Override
87 | public int getCount() {
88 | return Util.TEST_LIST_ITEM_COUNT;
89 | }
90 |
91 | @Override
92 | public Object getItem(int position) {
93 | return position;
94 | }
95 |
96 | @Override
97 | public long getItemId(int position) {
98 | return position;
99 | }
100 |
101 | @Override
102 | public View getView(int position, View convertView, ViewGroup parent) {
103 | if (convertView == null) {
104 | convertView = LayoutInflater.from(context).inflate(R.layout.normal_list_item, parent, false);
105 |
106 | ViewHolder viewHolder = new ViewHolder();
107 | viewHolder.textView = (TextView) convertView.findViewById(R.id.normal_text);
108 |
109 | convertView.setTag(viewHolder);
110 | }
111 |
112 | ViewHolder holder = (ViewHolder) convertView.getTag();
113 | holder.textView.setText(TestSpan.getSpanString(position));
114 | holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Util.fromDPtoPix(context, Util.TEXT_SIZE_DP));
115 |
116 | return convertView;
117 | }
118 |
119 | private class ViewHolder {
120 | TextView textView;
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/StaticLayoutUI.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.v7.app.ActionBarActivity;
6 | import android.view.*;
7 | import android.widget.BaseAdapter;
8 | import android.widget.ListView;
9 | import com.ragnarok.staticlayouttest.app.R;
10 | import com.ragnarok.staticlayoutview.util.Util;
11 |
12 | public class StaticLayoutUI extends ActionBarActivity {
13 |
14 | private static final String TAG = "StaticLayoutUI";
15 |
16 | private ListView listview;
17 |
18 | private StaticListAdapter adapter;
19 |
20 | private AutoScrollHandler autoScrollHandler;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_static_layout_ui);
26 |
27 | listview = (ListView) findViewById(R.id.test_list);
28 |
29 | adapter = new StaticListAdapter(this);
30 |
31 | listview.setAdapter(adapter);
32 |
33 | autoScrollHandler = new AutoScrollHandler(listview, Util.TEST_LIST_ITEM_COUNT);
34 |
35 | findViewById(R.id.scroll_down_button).setOnClickListener(new View.OnClickListener() {
36 | @Override
37 | public void onClick(View v) {
38 | autoScrollHandler.startAutoScrollDown();
39 | }
40 | });
41 |
42 | findViewById(R.id.scroll_up_button).setOnClickListener(new View.OnClickListener() {
43 | @Override
44 | public void onClick(View v) {
45 | autoScrollHandler.startAutoScrollUp();
46 | }
47 | });
48 |
49 | }
50 |
51 | @Override
52 | protected void onStop() {
53 | super.onStop();
54 | }
55 |
56 | @Override
57 | public boolean onCreateOptionsMenu(Menu menu) {
58 | // Inflate the menu; this adds items to the action bar if it is present.
59 | getMenuInflater().inflate(R.menu.menu_static_layout_ui, menu);
60 | return true;
61 | }
62 |
63 | @Override
64 | public boolean onOptionsItemSelected(MenuItem item) {
65 | // Handle action bar item clicks here. The action bar will
66 | // automatically handle clicks on the Home/Up button, so long
67 | // as you specify a parent activity in AndroidManifest.xml.
68 | int id = item.getItemId();
69 |
70 | //noinspection SimplifiableIfStatement
71 | if (id == R.id.action_settings) {
72 | return true;
73 | }
74 |
75 | return super.onOptionsItemSelected(item);
76 | }
77 |
78 | private static class StaticListAdapter extends BaseAdapter {
79 |
80 | private Context context;
81 |
82 | public StaticListAdapter(Context context) {
83 | this.context = context;
84 | }
85 |
86 | @Override
87 | public int getCount() {
88 | return Util.TEST_LIST_ITEM_COUNT;
89 | }
90 |
91 | @Override
92 | public Object getItem(int position) {
93 | return position;
94 | }
95 |
96 | @Override
97 | public long getItemId(int position) {
98 | return position;
99 | }
100 |
101 | @Override
102 | public View getView(int position, View convertView, ViewGroup parent) {
103 | if (convertView == null) {
104 | convertView = LayoutInflater.from(context).inflate(R.layout.static_list_item, parent, false);
105 |
106 | ViewHolder viewHolder = new ViewHolder();
107 | viewHolder.staticLayoutView = (StaticLayoutView) convertView.findViewById(R.id.static_layout_view);
108 |
109 | convertView.setTag(viewHolder);
110 | }
111 |
112 | ViewHolder holder = (ViewHolder) convertView.getTag();
113 | holder.staticLayoutView.setLayout(StaticLayoutManager.getInstance().getLayout(position));
114 | holder.staticLayoutView.invalidate();
115 |
116 | return convertView;
117 | }
118 |
119 | private class ViewHolder {
120 | StaticLayoutView staticLayoutView;
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ragnarok/staticlayoutview/ui/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.ragnarok.staticlayoutview.ui;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.v7.app.ActionBarActivity;
6 | import android.view.Menu;
7 | import android.view.MenuItem;
8 | import android.view.View;
9 | import android.widget.Button;
10 | import android.widget.Toast;
11 | import com.ragnarok.staticlayouttest.app.R;
12 | import com.ragnarok.staticlayoutview.util.FpsCalculator;
13 | import com.ragnarok.staticlayoutview.util.GhostThread;
14 | import com.ragnarok.staticlayoutview.util.TestSpan;
15 |
16 |
17 | public class MainActivity extends ActionBarActivity {
18 |
19 | private Button staticLayoutBtn;
20 | private Button normalLayoutBtn;
21 | private Button staticLongStringBtn;
22 | private Button normalLongStringBtn;
23 |
24 | @Override
25 | protected void onCreate(Bundle savedInstanceState) {
26 | super.onCreate(savedInstanceState);
27 | setContentView(R.layout.activity_main);
28 |
29 | staticLayoutBtn = (Button) findViewById(R.id.static_layout_test);
30 | normalLayoutBtn = (Button) findViewById(R.id.normal_layout_test);
31 | staticLongStringBtn = (Button) findViewById(R.id.static_long_string);
32 | normalLongStringBtn = (Button) findViewById(R.id.normal_long_string);
33 |
34 | GhostThread.start();
35 |
36 | staticLayoutBtn.setOnClickListener(new View.OnClickListener() {
37 | @Override
38 | public void onClick(View v) {
39 | Intent intent = new Intent(MainActivity.this, StaticLayoutUI.class);
40 | startActivity(intent);
41 | }
42 | });
43 |
44 | normalLayoutBtn.setOnClickListener(new View.OnClickListener() {
45 | @Override
46 | public void onClick(View v) {
47 | Intent intent = new Intent(MainActivity.this, NormalLayoutUI.class);
48 | startActivity(intent);
49 | }
50 | });
51 |
52 | staticLongStringBtn.setOnClickListener(new View.OnClickListener() {
53 | @Override
54 | public void onClick(View v) {
55 | Intent intent = new Intent(MainActivity.this, StaticLongStringUI.class);
56 | startActivity(intent);
57 | }
58 | });
59 |
60 | normalLongStringBtn.setOnClickListener(new View.OnClickListener() {
61 | @Override
62 | public void onClick(View v) {
63 | Intent intent = new Intent(MainActivity.this, NormalLongStringUI.class);
64 | startActivity(intent);
65 | }
66 | });
67 |
68 | FpsCalculator.instance().start();
69 |
70 | new Thread(new Runnable() {
71 | @Override
72 | public void run() {
73 | TestSpan.init(MainActivity.this);
74 | StaticLayoutManager.getInstance().initLayout(MainActivity.this, TestSpan.getSpanString(), TestSpan.getLongSpanString());
75 |
76 |
77 | runOnUiThread(new Runnable() {
78 | @Override
79 | public void run() {
80 | Toast.makeText(MainActivity.this, "init layout and span finish", Toast.LENGTH_LONG).show();
81 | }
82 | });
83 | }
84 | }).start();
85 | }
86 |
87 |
88 | @Override
89 | protected void onDestroy() {
90 | super.onDestroy();
91 | FpsCalculator.instance().stop();
92 | GhostThread.stop();
93 | }
94 |
95 | @Override
96 | protected void onResume() {
97 | super.onResume();
98 | }
99 |
100 | @Override
101 | public boolean onCreateOptionsMenu(Menu menu) {
102 | // Inflate the menu; this adds items to the action bar if it is present.
103 | getMenuInflater().inflate(R.menu.menu_main, menu);
104 | return true;
105 | }
106 |
107 | @Override
108 | public boolean onOptionsItemSelected(MenuItem item) {
109 | // Handle action bar item clicks here. The action bar will
110 | // automatically handle clicks on the Home/Up button, so long
111 | // as you specify a parent activity in AndroidManifest.xml.
112 | int id = item.getItemId();
113 |
114 | //noinspection SimplifiableIfStatement
115 | if (id == R.id.action_settings) {
116 | return true;
117 | }
118 |
119 | return super.onOptionsItemSelected(item);
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/app/app.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
--------------------------------------------------------------------------------