├── .gitignore ├── .idea ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── wanjian │ │ └── scrollscreenshots │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── wanjian │ │ │ └── scrollscreenshots │ │ │ ├── AutoScreenShotsAct.java │ │ │ ├── DrawListViewAct.java │ │ │ ├── DrawScrollViewAct.java │ │ │ ├── MainActivity.java │ │ │ └── ScrollAct.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_auto_scroll.xml │ │ ├── activity_auto_scroll_demo.xml │ │ ├── activity_draw_listview.xml │ │ ├── activity_draw_scrollview.xml │ │ ├── activity_main.xml │ │ └── item.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.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 │ │ └── labuladuo.jpeg │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── wanjian │ └── scrollscreenshots │ └── ExampleUnitTest.java ├── auto_cap_demo.gif ├── auto_cap_demo.mp4 ├── auto_scroll.gif ├── auto_scroll.mp4 ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── listview_capture.jpg ├── miui_screen_cap.gif ├── scrollview_capture.jpg └── settings.gradle /.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 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 wanjian 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 跟miui一样的自动滚动截屏 2 | 3 | > 很久之前写过一篇完全不同于其他长截屏方案的的博客,不过很仓促,现在重新整理一下 [android长截屏beta1](http://blog.csdn.net/qingchunweiliang/article/details/52248643) 4 | 5 | 6 | #### miui自动滚动长截屏效果 7 | 8 | ![miui](https://github.com/android-notes/auto-scroll-capture/blob/master/miui_screen_cap.gif?raw=true) 9 | 10 | 11 | 12 | #### 画 13 | 14 | * 给滚动控件外面嵌套一个`FrameLayout`(`LinearLayout`等也可以) 15 | * 手动调用`FrameLayout`的`draw`方法把`view`画到`bitmap`上 16 | 17 | ```java 18 | Bitmap bitmap = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.ARGB_8888); 19 | Canvas canvas = new Canvas(bitmap); 20 | container.draw(canvas); 21 | 22 | ``` 23 | > 具体参考 `DrawScrollViewAct` `DrawListViewAct` 24 | 25 | 26 | #### 滚 27 | 28 | * 通过不断改变motionEvent的y值并手动调用view的`dispatchTouchEvent`方法实现`view`滚动 29 | 30 | ```java 31 | 32 | private void autoScroll() { 33 | final int delay = 16; 34 | final MotionEvent motionEvent = MotionEvent.obtain(SystemClock.uptimeMillis() 35 | , SystemClock.uptimeMillis() 36 | , MotionEvent.ACTION_DOWN 37 | , listView.getWidth() / 2 38 | , listView.getHeight() / 2 39 | , 0); 40 | listView.dispatchTouchEvent(motionEvent);//先分发 MotionEvent.ACTION_DOWN 事件 41 | 42 | listView.postDelayed(new Runnable() { 43 | @Override 44 | public void run() { 45 | motionEvent.setAction(MotionEvent.ACTION_MOVE); //延时分发 MotionEvent.ACTION_MOVE 事件 46 | //改变y坐标,越大滚动越快,但太大可能会导致掉帧 47 | motionEvent.setLocation((int) motionEvent.getX(), (int) motionEvent.getY() - 10); 48 | listView.dispatchTouchEvent(motionEvent); 49 | listView.postDelayed(this, delay); 50 | } 51 | }, delay); 52 | } 53 | 54 | 55 | ``` 56 | 57 | > 参考 ScrollAct 58 | 59 | 自动滚动效果 60 | ![自动滚动](https://github.com/android-notes/auto-scroll-capture/blob/master/auto_scroll.gif?raw=true) 61 | 62 | #### 截屏 63 | 64 | 65 | * 每自动滚动完一屏幕调用`view.draw()`把`view`画到`bitmap`上,最后拼接bitmap 66 | 67 | > 参考 `AutoScreenShotsAct` 68 | 69 | 70 | #### 为什么要嵌套一层view 71 | 72 | listview不嵌套时,不管是否滚动,都能得到正确的结果 73 | 74 | ![listview](https://github.com/android-notes/auto-scroll-capture/blob/master/listview_capture.jpg?raw=true) 75 | 76 | 77 | 但scrollview滚动后即使顶部的已经看不到了,但调用scrollview的draw时还是会把scrollview不可见的地方画进去 78 | ![scrollview](https://github.com/android-notes/auto-scroll-capture/blob/master/scrollview_capture.jpg?raw=true) 79 | 80 | 为了通用起间,我们给view外面嵌套了一层view 81 | 82 | #### 代码实现 83 | 84 | 关键逻辑如下,但有些细节还需要具体对待 85 | 86 | ```java 87 | 88 | private void autoScroll() { 89 | final int delay = 16;//延时16毫秒分发滑动事件 90 | final int step = 10;//每次滑动距离5像素,可以根据需要调整(若卡顿的话实际滚动距离可能小于5) 91 | final MotionEvent motionEvent = MotionEvent.obtain(SystemClock.uptimeMillis() 92 | , SystemClock.uptimeMillis() 93 | , MotionEvent.ACTION_DOWN 94 | , listView.getWidth() / 2 95 | , listView.getHeight() / 2 96 | , 0); 97 | //先分发 MotionEvent.ACTION_DOWN 事件,我们指定为按下位置是listview的中间位置,当然其他位置也可以 98 | listView.dispatchTouchEvent(motionEvent); 99 | /* 100 | 注意: 101 | 查看Listview源码可知 滑动距离大于ViewConfiguration.get(view.getContext()).getScaledTouchSlop()时listview才开始滚动 102 | private boolean startScrollIfNeeded(int x, int y, MotionEvent vtev) { 103 | // Check if we have moved far enough that it looks more like a 104 | // scroll than a tap 105 | final int deltaY = y - mMotionY; 106 | final int distance = Math.abs(deltaY); 107 | final boolean overscroll = mScrollY != 0; 108 | if ((overscroll || distance > mTouchSlop) && (getNestedScrollAxes() & SCROLL_AXIS_VERTICAL) == 0) { 109 | ... 110 | return true; 111 | } 112 | return false; 113 | } 114 | */ 115 | motionEvent.setAction(MotionEvent.ACTION_MOVE); 116 | motionEvent.setLocation(motionEvent.getX(), motionEvent.getY() - (ViewConfiguration.get(listView.getContext()).getScaledTouchSlop())); 117 | listView.dispatchTouchEvent(motionEvent); 118 | 119 | final int startScrollY = (int) motionEvent.getY(); 120 | 121 | listView.postDelayed(new Runnable() { 122 | @Override 123 | public void run() { 124 | if (isScreenShots == false) {//注意:我们无法通过滚动距离来判断是否滚动到了最后,所以需要通过其他方式停止滚动 125 | drawRemainAndAssemble(startScrollY, (int) motionEvent.getY()); 126 | return; 127 | } 128 | //滚动刚好一整屏时画到bitmap上 129 | drawIfNeeded(startScrollY, (int) motionEvent.getY()); 130 | 131 | motionEvent.setAction(MotionEvent.ACTION_MOVE); //延时分发 MotionEvent.ACTION_MOVE 事件 132 | /* 133 | 改变motionEvent的y坐标,nextStep越大滚动越快,但太大可能会导致掉帧,导致实际滚动距离小于我们滑动的距离 134 | 135 | 因为我们是根据(curScrollY - startScrollY) % container.getHeight() == 0来判定是否刚好滚动了一屏幕的, 136 | 所以快要滚动到刚好一屏幕位置时,修改nextStep的值,使下次滚动后刚好是一屏幕的距离。 137 | 当然nextStep也可以一直是1,这时就不需要凑整了,但这样会导致滚动的特别慢 138 | */ 139 | int nextStep; 140 | int gap = (startScrollY - (int) motionEvent.getY() + step) % container.getHeight(); 141 | if (gap > 0 && gap < step) { 142 | nextStep = step - gap; 143 | } else { 144 | nextStep = step; 145 | } 146 | 147 | motionEvent.setLocation((int) motionEvent.getX(), (int) motionEvent.getY() - nextStep); 148 | listView.dispatchTouchEvent(motionEvent); 149 | 150 | listView.postDelayed(this, delay); 151 | } 152 | }, delay); 153 | } 154 | 155 | private void drawRemainAndAssemble(int startScrollY, int curScrollY) { 156 | //最后的可能不足一屏幕,需要单独处理 157 | if ((curScrollY - startScrollY) % container.getHeight() != 0) { 158 | Bitmap film = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.RGB_565); 159 | Canvas canvas = new Canvas(); 160 | canvas.setBitmap(film); 161 | container.draw(canvas); 162 | 163 | int part = (startScrollY - curScrollY) / container.getHeight(); 164 | int remainHeight = startScrollY - curScrollY - container.getHeight() * part; 165 | Bitmap remainBmp = Bitmap.createBitmap(film, 0, container.getHeight() - remainHeight, container.getWidth(), remainHeight); 166 | bitmaps.add(remainBmp); 167 | } 168 | 169 | assembleBmp(); 170 | 171 | } 172 | 173 | private void assembleBmp() { 174 | int h = 0; 175 | for (Bitmap bitmap : bitmaps) { 176 | h += bitmap.getHeight(); 177 | } 178 |        //如果你需要透明度或者对图片质量要求很高的话请使用Config.ARGB_8888 179 |        Bitmap bitmap = Bitmap.createBitmap(container.getWidth(), h, Bitmap.Config.RGB_565); 180 | Canvas canvas = new Canvas(bitmap); 181 | for (Bitmap b : bitmaps) { 182 | canvas.drawBitmap(b, 0, 0, null); 183 | canvas.translate(0, b.getHeight()); 184 | } 185 | ViewGroup.LayoutParams params = img.getLayoutParams(); 186 | params.width = bitmap.getWidth() * 2; 187 | params.height = bitmap.getHeight() * 2; 188 | img.requestLayout(); 189 | img.setImageBitmap(bitmap); 190 | } 191 | 192 | private void drawIfNeeded(int startScrollY, int curScrollY) { 193 | 194 | if ((curScrollY - startScrollY) % container.getHeight() == 0) { 195 | //正好滚动满一屏 196 | 197 | //为了更通用,我们是把ListView的父布局(和ListView宽高相同)画到了bitmap上 198 | Bitmap film = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.RGB_565); 199 | Canvas canvas = new Canvas(); 200 | canvas.setBitmap(film); 201 | container.draw(canvas); 202 | bitmaps.add(film); 203 | } 204 | } 205 | 206 | 207 | ``` 208 | #### 最终效果 209 | 210 | 左边是自动滚动的Listview,右边是停止截屏后的bitmap,可以看到完全没有拼接痕迹 211 | 212 | 213 | ![效果](https://github.com/android-notes/auto-scroll-capture/blob/master/auto_cap_demo.gif?raw=true) 214 | 215 | 216 | 217 | 218 | ```txt 219 | MIT License 220 | 221 | Copyright (c) 2018 wanjian 222 | 223 | Permission is hereby granted, free of charge, to any person obtaining a copy 224 | of this software and associated documentation files (the "Software"), to deal 225 | in the Software without restriction, including without limitation the rights 226 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 227 | copies of the Software, and to permit persons to whom the Software is 228 | furnished to do so, subject to the following conditions: 229 | 230 | The above copyright notice and this permission notice shall be included in all 231 | copies or substantial portions of the Software. 232 | 233 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 234 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 235 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 236 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 237 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 238 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 239 | SOFTWARE. 240 | 241 | 242 | ``` 243 | 244 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 26 5 | defaultConfig { 6 | applicationId "com.wanjian.scrollscreenshots" 7 | minSdkVersion 14 8 | targetSdkVersion 26 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:26.1.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/wanjian/scrollscreenshots/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.scrollscreenshots; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.wanjian.scrollscreenshots", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/scrollscreenshots/AutoScreenShotsAct.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.scrollscreenshots; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.os.Bundle; 7 | import android.os.SystemClock; 8 | import android.support.annotation.Nullable; 9 | import android.view.LayoutInflater; 10 | import android.view.MotionEvent; 11 | import android.view.View; 12 | import android.view.ViewConfiguration; 13 | import android.view.ViewGroup; 14 | import android.widget.BaseAdapter; 15 | import android.widget.ImageView; 16 | import android.widget.ListView; 17 | import android.widget.TextView; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | /** 23 | * Created by wanjian on 2018/1/3. 24 | */ 25 | 26 | public class AutoScreenShotsAct extends Activity { 27 | private ListView listView; 28 | 29 | private ImageView img; 30 | 31 | private ViewGroup container; 32 | 33 | private List bitmaps = new ArrayList<>(); 34 | 35 | private boolean isScreenShots = false; 36 | 37 | @Override 38 | protected void onCreate(@Nullable Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_auto_scroll_demo); 41 | listView = findViewById(R.id.listview); 42 | img = findViewById(R.id.img); 43 | container = findViewById(R.id.container); 44 | 45 | setAdapter(); 46 | 47 | findViewById(R.id.start).setOnClickListener(new View.OnClickListener() { 48 | @Override 49 | public void onClick(View v) { 50 | bitmaps.clear(); 51 | img.setImageBitmap(null); 52 | isScreenShots = true; 53 | autoScroll(); 54 | } 55 | }); 56 | findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() { 57 | @Override 58 | public void onClick(View v) { 59 | isScreenShots = false; 60 | 61 | } 62 | }); 63 | } 64 | 65 | private void autoScroll() { 66 | final int delay = 16;//延时16毫秒分发滑动事件 67 | final int step = 10;//每次滑动距离5像素,可以根据需要调整(若卡顿的话实际滚动距离可能小于5) 68 | final MotionEvent motionEvent = MotionEvent.obtain(SystemClock.uptimeMillis() 69 | , SystemClock.uptimeMillis() 70 | , MotionEvent.ACTION_DOWN 71 | , listView.getWidth() / 2 72 | , listView.getHeight() / 2 73 | , 0); 74 | //先分发 MotionEvent.ACTION_DOWN 事件,我们指定为按下位置是listview的中间位置,当然其他位置也可以 75 | listView.dispatchTouchEvent(motionEvent); 76 | /* 77 | 注意: 78 | 查看Listview源码可知 滑动距离大于ViewConfiguration.get(view.getContext()).getScaledTouchSlop()时listview才开始滚动 79 | private boolean startScrollIfNeeded(int x, int y, MotionEvent vtev) { 80 | // Check if we have moved far enough that it looks more like a 81 | // scroll than a tap 82 | final int deltaY = y - mMotionY; 83 | final int distance = Math.abs(deltaY); 84 | final boolean overscroll = mScrollY != 0; 85 | if ((overscroll || distance > mTouchSlop) && (getNestedScrollAxes() & SCROLL_AXIS_VERTICAL) == 0) { 86 | ... 87 | return true; 88 | } 89 | return false; 90 | } 91 | */ 92 | motionEvent.setAction(MotionEvent.ACTION_MOVE); 93 | motionEvent.setLocation(motionEvent.getX(), motionEvent.getY() - (ViewConfiguration.get(listView.getContext()).getScaledTouchSlop())); 94 | listView.dispatchTouchEvent(motionEvent); 95 | 96 | final int startScrollY = (int) motionEvent.getY(); 97 | 98 | listView.postDelayed(new Runnable() { 99 | @Override 100 | public void run() { 101 | if (isScreenShots == false) {//注意:我们无法通过滚动距离来判断是否滚动到了最后,所以需要通过其他方式停止滚动 102 | drawRemainAndAssemble(startScrollY, (int) motionEvent.getY()); 103 | return; 104 | } 105 | //滚动刚好一整屏时画到bitmap上 106 | drawIfNeeded(startScrollY, (int) motionEvent.getY()); 107 | 108 | motionEvent.setAction(MotionEvent.ACTION_MOVE); //延时分发 MotionEvent.ACTION_MOVE 事件 109 | /* 110 | 改变motionEvent的y坐标,nextStep越大滚动越快,但太大可能会导致掉帧,导致实际滚动距离小于我们滑动的距离 111 | 112 | 因为我们是根据(curScrollY - startScrollY) % container.getHeight() == 0来判定是否刚好滚动了一屏幕的, 113 | 所以快要滚动到刚好一屏幕位置时,修改nextStep的值,使下次滚动后刚好是一屏幕的距离。 114 | 当然nextStep也可以一直是1,这时就不需要凑整了,但这样会导致滚动的特别慢 115 | */ 116 | int nextStep; 117 | int gap = (startScrollY - (int) motionEvent.getY() + step) % container.getHeight(); 118 | if (gap > 0 && gap < step) { 119 | nextStep = step - gap; 120 | } else { 121 | nextStep = step; 122 | } 123 | 124 | motionEvent.setLocation((int) motionEvent.getX(), (int) motionEvent.getY() - nextStep); 125 | listView.dispatchTouchEvent(motionEvent); 126 | 127 | listView.postDelayed(this, delay); 128 | } 129 | }, delay); 130 | } 131 | 132 | private void drawRemainAndAssemble(int startScrollY, int curScrollY) { 133 | //最后的可能不足一屏幕,需要单独处理 134 | if ((curScrollY - startScrollY) % container.getHeight() != 0) { 135 | Bitmap film = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.RGB_565); 136 | Canvas canvas = new Canvas(); 137 | canvas.setBitmap(film); 138 | container.draw(canvas); 139 | 140 | int part = (startScrollY - curScrollY) / container.getHeight(); 141 | int remainHeight = startScrollY - curScrollY - container.getHeight() * part; 142 | Bitmap remainBmp = Bitmap.createBitmap(film, 0, container.getHeight() - remainHeight, container.getWidth(), remainHeight); 143 | bitmaps.add(remainBmp); 144 | } 145 | 146 | assembleBmp(); 147 | 148 | } 149 | 150 | private void assembleBmp() { 151 | int h = 0; 152 | for (Bitmap bitmap : bitmaps) { 153 | h += bitmap.getHeight(); 154 | } 155 | Bitmap bitmap = Bitmap.createBitmap(container.getWidth(), h, Bitmap.Config.RGB_565); 156 | Canvas canvas = new Canvas(bitmap); 157 | for (Bitmap b : bitmaps) { 158 | canvas.drawBitmap(b, 0, 0, null); 159 | canvas.translate(0, b.getHeight()); 160 | } 161 | ViewGroup.LayoutParams params = img.getLayoutParams(); 162 | params.width = bitmap.getWidth() * 2; 163 | params.height = bitmap.getHeight() * 2; 164 | img.requestLayout(); 165 | img.setImageBitmap(bitmap); 166 | } 167 | 168 | private void drawIfNeeded(int startScrollY, int curScrollY) { 169 | 170 | if ((curScrollY - startScrollY) % container.getHeight() == 0) { 171 | //正好滚动满一屏 172 | 173 | //为了更通用,我们是把ListView的父布局(和ListView宽高相同)画到了bitmap上 174 | Bitmap film = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.RGB_565); 175 | Canvas canvas = new Canvas(); 176 | canvas.setBitmap(film); 177 | container.draw(canvas); 178 | bitmaps.add(film); 179 | } 180 | } 181 | 182 | private void setAdapter() { 183 | 184 | listView.setAdapter(new BaseAdapter() { 185 | @Override 186 | public int getCount() { 187 | return 100; 188 | } 189 | 190 | @Override 191 | public Object getItem(int position) { 192 | return null; 193 | } 194 | 195 | @Override 196 | public long getItemId(int position) { 197 | return 0; 198 | } 199 | 200 | @Override 201 | public View getView(int position, View convertView, ViewGroup parent) { 202 | if (convertView == null) { 203 | convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, listView, false); 204 | convertView.setTag(R.id.index, convertView.findViewById(R.id.index)); 205 | } 206 | ((TextView) convertView.getTag(R.id.index)).setText(position + ""); 207 | return convertView; 208 | } 209 | }); 210 | 211 | } 212 | } 213 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/scrollscreenshots/DrawListViewAct.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.scrollscreenshots; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.BaseAdapter; 12 | import android.widget.ImageView; 13 | import android.widget.ListView; 14 | import android.widget.TextView; 15 | 16 | /** 17 | * Created by wanjian on 2018/1/3. 18 | */ 19 | 20 | public class DrawListViewAct extends Activity { 21 | 22 | private ListView listView; 23 | private ViewGroup container; 24 | 25 | private ImageView img1; 26 | private ImageView img2; 27 | private ImageView img3; 28 | 29 | @Override 30 | protected void onCreate(@Nullable Bundle savedInstanceState) { 31 | super.onCreate(savedInstanceState); 32 | setContentView(R.layout.activity_draw_listview); 33 | listView = findViewById(R.id.listview); 34 | container = findViewById(R.id.container); 35 | img1 = findViewById(R.id.img1); 36 | img2 = findViewById(R.id.img2); 37 | img3 = findViewById(R.id.img3); 38 | 39 | setAdapter(); 40 | 41 | img1.setOnClickListener(new View.OnClickListener() { 42 | @Override 43 | public void onClick(View v) { 44 | drawListView(img1); 45 | } 46 | }); 47 | img2.setOnClickListener(new View.OnClickListener() { 48 | @Override 49 | public void onClick(View v) { 50 | drawListView(img2); 51 | } 52 | }); 53 | img3.setOnClickListener(new View.OnClickListener() { 54 | @Override 55 | public void onClick(View v) { 56 | drawListViewContainer(img3); 57 | } 58 | }); 59 | 60 | } 61 | 62 | private void drawListViewContainer(ImageView imageView) { 63 | Bitmap bitmap = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.ARGB_8888); 64 | Canvas canvas = new Canvas(bitmap); 65 | container.draw(canvas); 66 | imageView.setImageBitmap(bitmap); 67 | } 68 | 69 | private void drawListView(ImageView imageView) { 70 | Bitmap bitmap = Bitmap.createBitmap(listView.getWidth(), listView.getHeight(), Bitmap.Config.ARGB_8888); 71 | Canvas canvas = new Canvas(bitmap); 72 | listView.draw(canvas); 73 | imageView.setImageBitmap(bitmap); 74 | } 75 | 76 | private void setAdapter() { 77 | 78 | listView.setAdapter(new BaseAdapter() { 79 | @Override 80 | public int getCount() { 81 | return 30; 82 | } 83 | 84 | @Override 85 | public Object getItem(int position) { 86 | return null; 87 | } 88 | 89 | @Override 90 | public long getItemId(int position) { 91 | return 0; 92 | } 93 | 94 | @Override 95 | public View getView(int position, View convertView, ViewGroup parent) { 96 | if (convertView == null) { 97 | convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, listView, false); 98 | convertView.setTag(R.id.index, convertView.findViewById(R.id.index)); 99 | } 100 | ((TextView) convertView.getTag(R.id.index)).setText(position + ""); 101 | return convertView; 102 | } 103 | }); 104 | 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/scrollscreenshots/DrawScrollViewAct.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.scrollscreenshots; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Bitmap; 5 | import android.graphics.Canvas; 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.ImageView; 12 | import android.widget.ScrollView; 13 | import android.widget.TextView; 14 | 15 | /** 16 | * Created by wanjian on 2018/1/3. 17 | */ 18 | 19 | public class DrawScrollViewAct extends Activity { 20 | 21 | private ScrollView scrollView; 22 | private ViewGroup container; 23 | 24 | private ImageView img1; 25 | private ImageView img2; 26 | private ImageView img3; 27 | 28 | @Override 29 | protected void onCreate(@Nullable Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_draw_scrollview); 32 | scrollView = findViewById(R.id.scrollView); 33 | container = findViewById(R.id.container); 34 | img1 = findViewById(R.id.img1); 35 | img2 = findViewById(R.id.img2); 36 | img3 = findViewById(R.id.img3); 37 | 38 | ViewGroup itemContainer = findViewById(R.id.itemContainer); 39 | for (int i = 0; i < 20; i++) { 40 | View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, itemContainer, false); 41 | ((TextView) view.findViewById(R.id.index)).setText("" + i); 42 | itemContainer.addView(view); 43 | } 44 | 45 | img1.setOnClickListener(new View.OnClickListener() { 46 | @Override 47 | public void onClick(View v) { 48 | drawScrollView(img1); 49 | } 50 | }); 51 | img2.setOnClickListener(new View.OnClickListener() { 52 | @Override 53 | public void onClick(View v) { 54 | drawScrollView(img2); 55 | } 56 | }); 57 | img3.setOnClickListener(new View.OnClickListener() { 58 | @Override 59 | public void onClick(View v) { 60 | drawScrollViewContainer(img3); 61 | } 62 | }); 63 | 64 | } 65 | 66 | private void drawScrollViewContainer(ImageView imageView) { 67 | Bitmap bitmap = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.ARGB_8888); 68 | Canvas canvas = new Canvas(bitmap); 69 | container.draw(canvas); 70 | imageView.setImageBitmap(bitmap); 71 | } 72 | 73 | private void drawScrollView(ImageView imageView) { 74 | Bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), scrollView.getHeight(), Bitmap.Config.ARGB_8888); 75 | Canvas canvas = new Canvas(bitmap); 76 | scrollView.draw(canvas); 77 | imageView.setImageBitmap(bitmap); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/scrollscreenshots/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.scrollscreenshots; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(R.layout.activity_main); 14 | 15 | findViewById(R.id.draw1).setOnClickListener(new View.OnClickListener() { 16 | @Override 17 | public void onClick(View v) { 18 | startActivity(new Intent(getApplicationContext(), DrawListViewAct.class)); 19 | } 20 | }); 21 | findViewById(R.id.draw2).setOnClickListener(new View.OnClickListener() { 22 | @Override 23 | public void onClick(View v) { 24 | startActivity(new Intent(getApplicationContext(), DrawScrollViewAct.class)); 25 | } 26 | }); 27 | findViewById(R.id.scroll).setOnClickListener(new View.OnClickListener() { 28 | @Override 29 | public void onClick(View v) { 30 | startActivity(new Intent(getApplicationContext(), ScrollAct.class)); 31 | } 32 | }); 33 | findViewById(R.id.demo).setOnClickListener(new View.OnClickListener() { 34 | @Override 35 | public void onClick(View v) { 36 | startActivity(new Intent(getApplicationContext(), AutoScreenShotsAct.class)); 37 | } 38 | }); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/scrollscreenshots/ScrollAct.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.scrollscreenshots; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.os.SystemClock; 6 | import android.support.annotation.Nullable; 7 | import android.view.LayoutInflater; 8 | import android.view.MotionEvent; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.BaseAdapter; 12 | import android.widget.ListView; 13 | import android.widget.TextView; 14 | 15 | /** 16 | * Created by wanjian on 2018/1/3. 17 | */ 18 | 19 | public class ScrollAct extends Activity { 20 | 21 | private ListView listView; 22 | 23 | @Override 24 | protected void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_auto_scroll); 27 | listView = findViewById(R.id.listview); 28 | 29 | setAdapter(); 30 | 31 | findViewById(R.id.start).setOnClickListener(new View.OnClickListener() { 32 | @Override 33 | public void onClick(View v) { 34 | autoScroll(); 35 | } 36 | }); 37 | } 38 | 39 | private void autoScroll() { 40 | final int delay = 16; 41 | final MotionEvent motionEvent = MotionEvent.obtain(SystemClock.uptimeMillis() 42 | , SystemClock.uptimeMillis() 43 | , MotionEvent.ACTION_DOWN 44 | , listView.getWidth() / 2 45 | , listView.getHeight() / 2 46 | , 0); 47 | listView.dispatchTouchEvent(motionEvent);//先分发 MotionEvent.ACTION_DOWN 事件 48 | 49 | listView.postDelayed(new Runnable() { 50 | @Override 51 | public void run() { 52 | motionEvent.setAction(MotionEvent.ACTION_MOVE); //延时分发 MotionEvent.ACTION_MOVE 事件 53 | //改变y坐标,越大滚动越快,但太大可能会导致掉帧 54 | motionEvent.setLocation((int) motionEvent.getX(), (int) motionEvent.getY() - 10); 55 | listView.dispatchTouchEvent(motionEvent); 56 | listView.postDelayed(this, delay); 57 | } 58 | }, delay); 59 | } 60 | 61 | private void setAdapter() { 62 | 63 | listView.setAdapter(new BaseAdapter() { 64 | @Override 65 | public int getCount() { 66 | return 100; 67 | } 68 | 69 | @Override 70 | public Object getItem(int position) { 71 | return null; 72 | } 73 | 74 | @Override 75 | public long getItemId(int position) { 76 | return 0; 77 | } 78 | 79 | @Override 80 | public View getView(int position, View convertView, ViewGroup parent) { 81 | if (convertView == null) { 82 | convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, listView, false); 83 | convertView.setTag(R.id.index, convertView.findViewById(R.id.index)); 84 | } 85 | ((TextView) convertView.getTag(R.id.index)).setText(position + ""); 86 | return convertView; 87 | } 88 | }); 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_auto_scroll.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |