<span id="Label3"></p><p><p>Https://android-notes.github.io/2016/12/03/android%E9%95%BF%E6%88%AA%E5%B1%8F%E5%8E%9F%E7%90%86/android Long Screen cutting principle</p></p><p><p></p></p><p><p>Xiaomi system comes with a long screenshot should be used by many people, the effect is Good. The ListView automatically scrolls when a long screenshot is held and a complete screenshot is obtained when the Stop screen is Pressed.</p></p><p><p>This article introduces the principle of long-cut screen</p></p><p><p>In the previous article introduced the Android screen sharing implementation, The principle of this article is basically consistent with the previous Article.</p></p>Get view image<p><p>When we want a view image, we can call the system API to get the View's bitmap, but sometimes we may not get it. We can get it in a different way.</p></p><p><p>First create a bitmap of the same size as the view</p></p> <table> <tbody> <tr> <td class="gutter">1</td> <td class="code">Bitmap bmp = Bitmap.createbitmap (view.getwidth (), view.getheight (), Bitmap.Config.RGB_565);</td> </tr> </tbody> </table><p><p>Then draw the view onto the BMP</p></p> <table> <tbody> <tr> <td class="gutter">123456</td> <td class="code">Canvas canvas = <span class="keyword">new Canvas ();</span> canvas.setbitmap (bmp); View.draw (canvas);</td> </tr> </tbody> </table><p><p>After executing the above code, the BMP is the image of the VIEW.</p></p>Manufacturing scrolling events, prompting view scrolling<p><p>We can create a motionevent and then periodically modify the Y value of the motionevent and distribute it to the view, prompting the view to scroll up and down. Of course we can also change the X value periodically to cause the view to scroll left and right.</p></p><p><p>The code is roughly as follows</p></p> <table> <tbody> <tr> <td class="gutter">12345678910111213141516</td> <td class="code"> <span class="keyword">final motionevent motionevent = Motionevent.obtain ( Systemclock.uptimemillis (), systemclock.uptimemillis (), motionevent.action_down, view.getwidth ()/ <span class="number">2, view.getheight ()/<span class="number">2, <span class="number">0); </span> </span> </span> </span> view.postdelayed (<span class="keyword">new Runnable () {</span> <span class="meta"> @Override </span> <span class="function"><span class="keyword">public <span class="keyword">void <span class="title">run <span class="params" () {< span> </span> </span> </span> </span> motionevent.setaction (motionevent.action_move ); motionevent.setlocation ((<span class="keyword">int) motionevent.getx (), (<span class="keyword">int) Motionevent.gety ()-<span class="number">1); </span> </span> </span> <span class="comment">//events distributed to view </span> view.dispatchtouchevent (motionevent); view.postdelayed (<span class="keyword">this, delay); </span>}}, delay); </span></td> </tr> </tbody> </table><p><p>Note: the same Motionevent object is used from the distribution down event to the end, and only the X or Y values need to be changed continuously.</p></p><p><p>Each time the value of x or Y is not too large for the last change, and if it is too large, the actual scrolling distance of the view may not reach the value set for Motionevent (due to the view scrolling lag).</p></p>Screen Cutting<p><p>When the x or Y value set for motionevent is exactly the size of the current view, a new bitmap is created, The view is drawn to the bitmap by the above method, and all bitmap are stitched when you want to stop the Screenshot.</p></p>Note<p><p>When we want to take a long screenshot of the listview, we need to embed a layer and a listview-sized view outside the listview, all of which operate on a nested layer of VIEW. When we call the nested view of draw (new Canvas), the currently seen ListView is drawn to the bmp, regardless of how many layers of the ListView are nested into the current Bmp.</p></p><p><p>Since the ListView is based on whether the distance is greater than the sliding is <code>ViewConfiguration.get(view.getContext()).getScaledTouchSlop() )</code> determined to not scroll, so at the outset we have to special deal with, Why is it <code>ViewConfiguration.get(view.getContext()).getScaledTouchSlop() )</code> possible to view the event distribution related functions of the ListView get (dispatchtouchevent), Let the ListView think that it is beginning to roll, so that the sliding distance and the actual scrolling distance will be Consistent.</p></p><p><p>The ListView also notifies you if scrolling is the last, otherwise if you do not manually stop, the scroll event is still being distributed, but the ListView no longer scrolls, resulting in the final screen after which the end is all Repeated.</p></p>Implementation of approximate mode code, to be optimized <table> <tbody> <tr> <td class="gutter">1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 1581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971 98199200201202203204205206</td> <td class="code"><span class="keyword">package <span class="keyword">com.example.wanjian.test;</span></span><span class="keyword"><span class="keyword">Import android.graphics.Bitmap;</span></span><span class="keyword"><span class="keyword">Import android.graphics.Canvas;</span></span><span class="keyword"><span class="keyword">Import android.graphics.Color;</span></span><span class="keyword"><span class="keyword">Import android.os.Environment;</span></span><span class="keyword"><span class="keyword">Import android.os.SystemClock;</span></span><span class="keyword"><span class="keyword">Import android.view.MotionEvent;</span></span><span class="keyword"><span class="keyword">Import android.view.View;</span></span><span class="keyword"><span class="keyword">Import android.view.ViewConfiguration;</span></span><span class="keyword"><span class="keyword">Import android.widget.LinearLayout;</span></span><span class="keyword"><span class="keyword">Import android.widget.Toast;</span></span><span class="keyword"><span class="keyword">Import java.io.File;</span></span><span class="keyword"><span class="keyword">Import java.io.FileOutputStream;</span></span><span class="keyword"><span class="keyword">Import java.lang.ref.WeakReference;</span></span><span class="keyword"><span class="keyword">Import java.util.ArrayList;</span></span><span class="keyword"><span class="keyword">Import java.util.List;</span></span><span class="comment"><span class="comment">/**</span></span>* Created by Wanjian on 16/8/18.*/<span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">class <span class="title">Scrollableviewrecutil {</span> </span></span> </span></span><span class="keyword"><span class="keyword">public <span class="keyword">static <span class="keyword">final <span class="keyword">int VERTICAL = <span class="number">0;</span> </span> </span> </span> </span></span><span class="keyword"><span class="keyword">private <span class="keyword">static <span class="keyword">final <span class="keyword">int DELAY = <span class="number">2;</span> </span> </span> </span> </span></span><span class="keyword"><span class="keyword">private list<bitmap> bitmaps = <span class="keyword">new arraylist<> ();</span> </span></span><span class="keyword"><span class="keyword">private <span class="keyword">int orientation = VERTICAL;</span> </span></span><span class="keyword"><span class="keyword">Private View view;</span></span> <span class="keyword"><span class="keyword">Private <span class="keyword">boolean isend;</span> </span></span><span class="keyword"><span class="keyword">Private Onrecfinishedlistener listener;</span></span><span class="function"><span class="function"><span class="keyword">public <span class="title">scrollableviewrecutil<span class="params">(view view, <span class="keyword">int Orientation) {</span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">This.view = view;</span></span><span class="keyword"><span class="keyword">this.orientation = orientation;</span></span>}<span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">start<span class="params">(<span class="keyword">final onrecfinishedlistener listener) {</span> </span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">This.listener = listener;</span></span> <span class="keyword"><span class="keyword">final Motionevent motionevent = Motionevent.obtain (systemclock.uptimemillis (), systemclock.uptimemillis (), motionevent.action_down, view.getwidth ()/ <span class="number">2, view.getheight ()/ <span class="number">2, <span class="number">0);</span> </span> </span> </span></span>View.dispatchtouchevent (motionevent); motionevent.setaction (motionevent.action_move);<span class="comment"><span class="comment">//sliding distance greater than Viewconfiguration.get (view.getcontext ()). getscaledtouchslop () when the ListView starts scrolling</span></span>Motionevent.setlocation (motionevent.getx (), motionevent.gety ()-(viewconfiguration.get (view.getContext ()). Getscaledtouchslop () +<span class="number"><span class="number">1));</span></span>View.dispatchtouchevent (motionevent); Motionevent.setlocation (motionevent.getx (), view.getheight ()/<span class="number"><span class="number">2);</span></span>View.postdelayed (<span class="keyword"><span class="keyword">new Runnable () {</span></span><span class="meta"><span class="meta">@Override</span></span><span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">run<span class="params">() {</span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">if (isend) {</span></span><span class="comment"><span class="comment">//stop exactly one screen is all drawn, otherwise the drawing part</span></span><span class="keyword"><span class="keyword">if ((view.getheight ()/ <span class="number">2-(<span class="keyword">int) motionevent.gety ())% view.getheight () = = <span class="number">0) {</span> </span> </span> </span></span>Bitmap Bitmap = Rec (); bitmaps.add (Bitmap);}<span class="keyword"><span class="keyword">else {</span></span>Bitmap Origbitmap = Rec ();<span class="keyword"><span class="keyword">int y = view.getheight ()/ <span class="number">2-(<span class="keyword">int) motionevent.gety ();</span> </span> </span></span>Bitmap Bitmap = Bitmap.createbitmap (origbitmap,<span class="number"><span class="number">0, view.getheight ()-y% view.getheight (), view.getwidth (), y% view.getheight ());</span></span>Bitmaps.add (bitmap); Origbitmap.recycle ();}<span class="comment"><span class="comment">//the last one may be highly less than the height of the view</span></span><span class="keyword"><span class="keyword">int h = view.getheight () * (bitmaps.size ()- <span class="number">1);</span> </span></span>Bitmap Bitmap = Bitmaps.get (bitmaps.size ()-<span class="number"><span class="number">1);</span></span>H = h + bitmap.getheight (); Bitmap result = Bitmap.createbitmap (view.getwidth (), h, Bitmap.Config.RGB_565); Canvas Canvas =<span class="keyword"><span class="keyword">New Canvas ();</span></span>Canvas.setbitmap (result);<span class="keyword">for <span class="keyword">(<span class="keyword">int i = <span class="number">0; i < bitmaps.size (); I++) {</span> </span> </span></span>Bitmap B = Bitmaps.get (i); canvas.drawbitmap (b,<span class="number"><span class="number">0, i * view.getheight (), <span class="keyword">null);</span> </span></span>B.recycle (); } listener.onrecfinish (result);<span class="keyword"><span class="keyword">return;</span></span>}<span class="keyword"><span class="keyword">if ((view.getheight ()/ <span class="number">2-(<span class="keyword">int) motionevent.gety ())% view.getheight () = = <span class="number">0) {</span> </span> </span> </span></span>Bitmap Bitmap = Rec (); bitmaps.add (Bitmap);} Motionevent.setaction (motionevent.action_move);<span class="comment"><span class="comment">//simulate sliding up one pixel at a time, which may cause the scrolling to be particularly slow, which can be modified when actually used, but determine if it's just scrolling</span></span><span class="comment"><span class="comment">//a screen cannot be simply based on (view.getheight ()/2-(int) motionevent.gety ())% view.getheight () = =. </span></span><span class="comment"><span class="comment">//can roll n pixels at a time, when it is found that the next time you scroll n pixels out of a screen can change the value of n, to ensure that the next scrolling is exactly a screen,</span></span><span class="comment"><span class="comment">//this can be based on (view.getheight ()/2-(int) motionevent.gety ())% view.getheight () =% to determine whether to intercept the Screen. </span></span>Motionevent.setlocation ((<span class="keyword"><span class="keyword">int) motionevent.getx (), (<span class="keyword">int) motionevent.gety ()- <span class="number">1);</span> </span> </span></span>View.dispatchtouchevent (motionevent); View.postdelayed (<span class="keyword">this <span class="keyword">, DELAY);</span></span>}}, DELAY);}<span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">stop<span class="params">() {</span> </span> </span> </span></span></span>Isend =<span class="keyword"><span class="keyword">true;</span></span>}<span class="function"><span class="function"><span class="keyword">private Bitmap <span class="title">rec<span class="params">() {</span> </span> </span></span></span>Bitmap film = Bitmap.createbitmap (view.getwidth (), view.getheight (), Bitmap.Config.RGB_565); Canvas Canvas =<span class="keyword"><span class="keyword">New Canvas ();</span></span>Canvas.setbitmap (film); View.draw (canvas);<span class="keyword"><span class="keyword">return film;</span></span>}<span class="keyword"><span class="keyword">public <span class="class"><span class="keyword">Interface <span class="title">Onrecfinishedlistener {</span> </span></span> </span></span><span class="function"><span class="function"><span class="keyword">void <span class="title">onrecfinish<span class="params">(Bitmap Bitmap);</span> </span> </span></span></span>}} ' Activity code '<span class="function"><span class="function">Java</span></span><span class="title"><span class="title">setcontentview<span class="params">(r.layout.activity_main4);</span> </span></span><span class="comment"><span class="comment">//</span></span>listview= (ListView) Findviewbyid (r.id.listview); Listview.setadapter (<span class="keyword"><span class="keyword">new Baseadapter () {</span></span><span class="meta"><span class="meta">@Override</span></span><span class="function"><span class="function"><span class="keyword">public <span class="keyword">int <span class="title">GetCount<span class="params">() {</span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">return <span class="number">;</span> </span></span>}<span class="meta"><span class="meta">@Override</span></span><span class="function">public <span class="function"><span class="keyword">Object <span class="title">getItem<span class="params">(<span class="keyword">int Position) {</span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">return <span class="keyword">null;</span> </span></span>}<span class="meta"><span class="meta">@Override</span></span><span class="function"><span class="function"><span class="keyword">public <span class="keyword">Long <span class="title">getitemid<span class="params">(<span class="keyword">int Position) {</span> </span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">return <span class="number">0;</span> </span></span>}<span class="meta"><span class="meta">@Override</span></span><span class="function">public <span class="function"><span class="keyword">View <span class="title">getView<span class="params">(<span class="keyword">int position, View convertview, viewgroup parent) {</span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">if (convertview==<span class="keyword">null) {</span> </span></span>Button button= (button) layoutinflater.from (getapplication ()). Inflate (r.layout.item,listview,<span class="keyword"><span class="keyword">false);</span></span>Button.settext (<span class="string"><span class="string">"+position");</span></span><span class="keyword"><span class="keyword">return button;</span></span>} ((Button) convertview). SetText (<span class="string"><span class="string">"+position");</span></span><span class="keyword"><span class="keyword">return convertview;</span></span>}});<span class="comment"><span class="comment">//</span></span>File file=<span class="keyword"><span class="keyword">new File (environment.getexternalstoragedirectory (),<span class="string">"aaa");</span> </span></span>File.mkdirs ();<span class="keyword">for <span class="keyword">(File f:file.listfiles ()) {</span></span>F.delete ();} Listview.getviewtreeobserver (). Addongloballayoutlistener (<span class="keyword"><span class="keyword">new Viewtreeobserver.ongloballayoutlistener () {</span></span><span class="meta"><span class="meta">@Override</span></span><span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">ongloballayout<span class="params">() {</span> </span> </span> </span></span></span>Listview.getviewtreeobserver (). Removeglobalonlayoutlistener (<span class="keyword">this <span class="keyword">);</span></span>Start ();}});</td> </tr> </tbody> </table> <table> <tbody> <tr> <td class="gutter">12345678910111213141516171819202122232425262728293031</td> <td class="code"><span class="function"><span class="function"><span class="keyword">private <span class="keyword">void <span class="title">start<span class="params">() {</span> </span> </span> </span></span></span><span class="keyword"><span class="keyword">final View View=findviewbyid (r.id.view);</span></span><span class="keyword"><span class="keyword">final Scrollableviewrecutil scrollableviewrecutil=<span class="keyword">new Scrollableviewrecutil (view, scrollableviewrecutil.vertical);</span> </span></span>Scrollableviewrecutil.start (<span class="keyword"><span class="keyword">new Scrollableviewrecutil.onrecfinishedlistener () {</span></span><span class="meta"><span class="meta">@Override</span></span><span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">onrecfinish<span class="params">(Bitmap Bitmap) {</span> </span> </span> </span></span></span>File f= environment.getexternalstoragedirectory (); System.out.print (f.getabsolutefile (). toString ()); Toast.maketext (getapplicationcontext (), f.getabsolutepath (), toast.length_long). show ();<span class="keyword"><span class="keyword">try {</span></span>Bitmap.compress (Bitmap.CompressFormat.JPEG,<span class="number"><span class="number"><span class="keyword">New FileOutputStream (<span class="keyword">new File (f,<span class="string">"rec" +system.currenttimemillis () +<span class="string">". jpg"</span> </span> </span> </span> )); </span></span>Toast.maketext (getapplicationcontext (),<span class="string"><span class="string">"Success", toast.length_long). Show ();</span></span>}<span class="keyword"><span class="keyword">catch (Exception e) {</span></span>E.printstacktrace ();}});<span class="comment"><span class="comment">//scrollableviewrecutil</span></span>View.postdelayed (<span class="keyword"><span class="keyword">new Runnable () {</span></span><span class="meta"><span class="meta">@Override</span></span><span class="function"><span class="function"><span class="keyword">public <span class="keyword">void <span class="title">run<span class="params">() {</span> </span> </span> </span></span></span>Scrollableviewrecutil.stop ();}},<span class="number"><span class="number">90*<span class="number">);</span> </span></span>}</td> </tr> </tbody> </table><p><p>Layout</p></p> <table> <tbody> <tr> <td class="gutter">123456789101112131415161718</td> <td class="code"><?xml version= "1.0" encoding= "utf-8"?><span class="tag"><span class="tag"><<span class="name">linearlayout <span class="attr">xmlns:android=<span class="string">"http://schemas.android.com/apk/res/android"</span> </span> </span></span></span><span class="attr"><span class="attr">android:layout_width=<span class="string">"match_parent"</span> </span></span><span class="attr"><span class="attr">android:layout_height=<span class="string">"match_parent"</span> </span></span><span class="attr"><span class="attr">android:id=<span class="string">"@+id/view"</span> </span></span><span class="attr"><span class="attr">android:orientation=<span class="string">"vertical"</span> </span></span>><span class="tag"><span class="tag"><<span class="name">ListView</span> </span></span><span class="attr"><span class="attr">android:id=<span class="string">"@+id/listview"</span> </span></span><span class="attr"><span class="attr">android:layout_width=<span class="string">"match_parent"</span> </span></span><span class="attr"><span class="attr">android:layout_height=<span class="string">"match_parent"</span> </span></span><span class="attr"><span class="attr">android:divider=<span class="string">"#e1e1e1"</span> </span></span><span class="attr"><span class="attr">android:dividerheight=<span class="string">"2dp"</span> </span></span>><span class="tag"><span class="tag"></<span class="name">listview></span> </span></span><span class="tag"><span class="tag"></<span class="name">linearlayout></span> </span></span></td> </tr> </tbody> </table> <ul> <ul> <li>Screen<br><span class="caption">Screen</span></li> </ul> </ul> <ul> <ul> <li>Final screenshot</li> </ul> </ul><p><p><span class="caption">Screen Cutting</span></p></p><p><p>Can see no stitching marks.</p></p><p><p>From my Blog</p></p><p><p>http://blog.csdn.net/qingchunweiliang/article/details/52248643</p></p><p><p>Android Long screenshot principle</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service