android完美截屏



1
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="left: 0px; position: absolute; top: 0px;">android开发中通过View的getDrawingCache方法可以达到截屏的目的,只是缺少状态栏!

原始界面

图1


截屏得到的图片

图1


代码实现

1. 添加权限(AndroidManifest.xml文件里)


?
1
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />

2. 添加1个Button(activity_main.xml文件)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:tools = "http://schemas.android.com/tools"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     tools:context = ".MainActivity" >
 
     < TextView
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_centerHorizontal = "true"
         android:layout_centerVertical = "true"
         android:text = "@string/hello_world" />
     
     < Button
         android:id = "@+id/btn_save"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:text = "Screenshot"
         />
 
</ RelativeLayout >



3. 实现截屏(MainActivity.java文件)


?
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
package com.example.androidtest;
 
import java.io.File;
import java.io.FileOutputStream;
 
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends Activity {
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         
         Button btn = (Button) this .findViewById(R.id.btn_save);
         btn.setOnClickListener( new View.OnClickListener() {
             
             @Override
             public void onClick(View v) {
                 screenshot();
             }
         });
     }
     
     private void screenshot()
     {
         // 获取屏幕
         View dView = getWindow().getDecorView(); 
         dView.setDrawingCacheEnabled( true );  
         dView.buildDrawingCache();  
         Bitmap bmp = dView.getDrawingCache();
         if (bmp != null )
         {
             try {
                 // 获取内置SD卡路径
                 String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                 // 图片文件路径
                 String filePath = sdCardPath + File.separator + "screenshot.png" ;
                 
                 File file = new File(filePath);
                 FileOutputStream os = new FileOutputStream(file);
                 bmp.compress(Bitmap.CompressFormat.PNG, 100 , os);
                 os.flush();
                 os.close();
             } catch (Exception e) {
             }
         }
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return true ;
     }
 
}

1
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="left: 0px; position: absolute; top: 0px;">

android开发中通过View的getDrawingCache方法可以达到截屏的目的,只是缺少状态栏!

原始界面

图1


截屏得到的图片

图1


代码实现

1. 添加权限(AndroidManifest.xml文件里)


?
1
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />

2. 添加1个Button(activity_main.xml文件)



?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:tools = "http://schemas.android.com/tools"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     tools:context = ".MainActivity" >
 
     < TextView
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_centerHorizontal = "true"
         android:layout_centerVertical = "true"
         android:text = "@string/hello_world" />
     
     < Button
         android:id = "@+id/btn_save"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:text = "Screenshot"
         />
 
</ RelativeLayout >



3. 实现截屏(MainActivity.java文件)


?
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
package com.example.androidtest;
 
import java.io.File;
import java.io.FileOutputStream;
 
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends Activity {
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         
         Button btn = (Button) this .findViewById(R.id.btn_save);
         btn.setOnClickListener( new View.OnClickListener() {
             
             @Override
             public void onClick(View v) {
                 screenshot();
             }
         });
     }
     
     private void screenshot()
     {
         // 获取屏幕
         View dView = getWindow().getDecorView(); 
         dView.setDrawingCacheEnabled( true );  
         dView.buildDrawingCache();  
         Bitmap bmp = dView.getDrawingCache();
         if (bmp != null )
         {
             try {
                 // 获取内置SD卡路径
                 String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                 // 图片文件路径
                 String filePath = sdCardPath + File.separator + "screenshot.png" ;
                 
                 File file = new File(filePath);
                 FileOutputStream os = new FileOutputStream(file);
                 bmp.compress(Bitmap.CompressFormat.PNG, 100 , os);
                 os.flush();
                 os.close();
             } catch (Exception e) {
             }
         }
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return true ;
     }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

generallizhong

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值