android之截圖(包括擷取scrollview與listview的)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
public class ScreenShot
{ //
獲取指定Activity的截圖,儲存到png檔案 public static Bitmap
takeScreenShot(Activity activity) { //
View是你需要截圖的View View
view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled( true ); view.buildDrawingCache(); Bitmap
b1 = view.getDrawingCache(); //
獲取狀態列高度 Rect
frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight
= frame.top; System.out.println(statusBarHeight); //
獲取螢幕長和高 int width
= activity.getWindowManager().getDefaultDisplay().getWidth(); int height
= activity.getWindowManager().getDefaultDisplay() .getHeight(); //
去掉標題欄 //
Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455); Bitmap
b = Bitmap.createBitmap(b1, 0 ,
statusBarHeight, width, height -
statusBarHeight); view.destroyDrawingCache(); savePic(b, "/sdcard/screen_test.png" ); return b; } //
儲存到sdcard public static void savePic(Bitmap
b, String strFileName) { FileOutputStream
fos = null ; try { fos
= new FileOutputStream(strFileName); if ( null !=
fos) { b.compress(Bitmap.CompressFormat.PNG, 90 ,
fos); fos.flush(); fos.close(); } } catch (FileNotFoundException
e) { e.printStackTrace(); } catch (IOException
e) { e.printStackTrace(); } } /** *
把View物件轉換成bitmap *
*/ public static Bitmap
convertViewToBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec( 0 ,
MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec( 0 ,
MeasureSpec.UNSPECIFIED)); view.layout( 0 , 0 ,
view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap
bitmap = view.getDrawingCache(); if (bitmap
!= null )
{ System.out.println( "這不是nullde1" ); Log.d( "nullde1" , "nullde1" ); } else { System.out.println( "這nullnulllnulnlul" ); } return bitmap; } //
程式入口1 public static void shoot(Activity
a) { ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/sdcard/screen_test.png" ); } //
程式入口2 public static void shootView(View
view) { ScreenShot.savePic(ScreenShot.convertViewToBitmap(view), "sdcard/xx.png" ); } public static Bitmap
getViewBitmap(View v) { v.clearFocus(); v.setPressed( false ); boolean willNotCache
= v.willNotCacheDrawing(); v.setWillNotCacheDrawing( false ); //
Reset the drawing cache background color to fully transparent //
for the duration of this operation int color
= v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor( 0 ); if (color
!= 0 )
{ v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap
cacheBitmap = v.getDrawingCache(); if (cacheBitmap
== null )
{ Log.e( "TTTTTTTTActivity" , "failed
getViewBitmap(" +
v + ")" , new RuntimeException()); return null ; } Bitmap
bitmap = Bitmap.createBitmap(cacheBitmap); //
Restore the view v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); return bitmap; } /** *
擷取scrollview的螢幕 *
**/ public static Bitmap
getBitmapByView(ScrollView scrollView) { int h
= 0 ; Bitmap
bitmap = null ; //
獲取listView實際高度 for ( int i
= 0 ;
i < scrollView.getChildCount(); i++) { h
+= scrollView.getChildAt(i).getHeight(); scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3); } Log.d(TAG, "實際高度:" +
h); Log.d(TAG, "
高度:" +
scrollView.getHeight()); //
建立對應大小的bitmap bitmap
= Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas
canvas = new Canvas(bitmap); scrollView.draw(canvas); //
測試輸出 FileOutputStream
out = null ; try { out
= new FileOutputStream( "/sdcard/screen_test.png" ); } catch (FileNotFoundException
e) { e.printStackTrace(); } try { if ( null !=
out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100 ,
out); out.flush(); out.close(); } } catch (IOException
e) { //
TODO: handle exception } return bitmap; } private static String
TAG = "Listview
and ScrollView item 截圖:" ; /** *
截圖listview *
**/ public static Bitmap
getbBitmap(ListView listView) { int h
= 0 ; Bitmap
bitmap = null ; //
獲取listView實際高度 for ( int i
= 0 ;
i < listView.getChildCount(); i++) { h
+= listView.getChildAt(i).getHeight(); } Log.d(TAG, "實際高度:" +
h); Log.d(TAG, "list
高度:" +
listView.getHeight()); //
建立對應大小的bitmap bitmap
= Bitmap.createBitmap(listView.getWidth(), h, Bitmap.Config.ARGB_8888); final Canvas
canvas = new Canvas(bitmap); listView.draw(canvas); //
測試輸出 FileOutputStream
out = null ; try { out
= new FileOutputStream( "/sdcard/screen_test.png" ); } catch (FileNotFoundException
e) { e.printStackTrace(); } try { if ( null !=
out) { bitmap.compress(Bitmap.CompressFormat.PNG, 100 ,
out); out.flush(); out.close(); } } catch (IOException
e) { //
TODO: handle exception } return bitmap; } } |
相關文章
- Android 普通View截圖 RecyclerView截圖 ScrollView截圖分享AndroidView
- android listview and scrollviewAndroidView
- TableView ScrollreView 截圖 擷取全屏 圖片模糊View
- ScrollView與ListView合用問題(正確計算Listview的高度)View
- 在Windows 10中擷取截圖的6種方式 簡介Windows
- Android-GridView與ScrollView共用的衝突問題(類似ListView)AndroidView
- ListView與ScrollView衝突,計算ListView的高度Item,然後重繪View
- Win8截圖工具快速擷取彈出選單的方法
- Android之ListView與SimpleAdapter的使用AndroidViewAPT
- ScrollView 與ListView 滑動衝突解決View
- 檔案路徑類 字串的各種擷取方式,包括擷取到檔名字串
- ScrollView中用ListView的事件衝突View事件
- Android之ListViewAndroidView
- 直播平臺軟體開發,完整擷取整個螢幕的截圖方式
- Java 圖片裁剪,擷取Java
- android ListView非同步載入圖片(雙快取)AndroidView非同步快取
- js擷取影片的封面圖片JS
- Arcgis For Android 中MapView 截圖獲取BitmapAndroidView
- Android獲取當前Activity的螢幕截圖Android
- 利用PhantomJS進行網頁截圖,完美解決擷取高度的問題JS網頁
- 如何用Chrome自帶的截圖功能擷取超過一個螢幕的網頁Chrome網頁
- 求擷取圖片等比公式公式
- ImageView長圖擷取部分展示View
- Android 截圖Android
- 如何使用 gnome-screenshot 截圖命令來擷取螢幕、視窗或指定區
- ScrollView中巢狀GridView,Listview的辦法View巢狀
- split使用和特殊使用(包括擷取第一個字元後的資料)字元
- android listview獲取選中項AndroidView
- 完美解決ScrollView巢狀ListView滑動衝突(有圖有真相)View巢狀
- Android截圖方案Android
- Android下截圖Android
- android 在擷取指定View的時候坑AndroidView
- Android截圖和指定View生成截圖分享AndroidView
- Android入門之ListView (二)AndroidView
- Android入門之ListView (一)AndroidView
- android 幾個經常用到的字串的擷取Android字串
- Android 截圖與 WebView 長圖分享經驗總結AndroidWebView
- Android:ListView的擴充與進階AndroidView