Android 截屏并保存

时间:2022-06-07 15:29:59

可能大家的手机都有截屏的功能,所以觉得这个截屏功能有点鸡肋,但是玩过天天酷跑的,应该都知道有这个分享功能,基于游戏中的那些截屏功能,花了两个多小时来写这个简单的 截屏功能,这篇就用来抛砖迎玉。

思路:1.获得屏幕的大小,再将屏幕以bitmap的形式保存下来

2。用bitmap。compress。将图片保存为文件

基本的思路就是这样,我想看到这儿大家都应该明白怎么写了,也许大家有别的思路,欢迎留言,谢谢

先代码吧;


public class MainActivity extends Activity {
    private Button button;
    private ImageView imageview;
    private String filename;
    File file;
    Bitmap bitmap2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WindowManager windowmanager= (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION; // 设置window type
        wmParams.gravity = Gravity.RIGHT| Gravity. CENTER_VERTICAL; // 调整悬浮窗口至右侧中间
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height =WindowManager.LayoutParams.WRAP_CONTENT;
        final Button button=new Button(getApplicationContext());
        windowmanager.addView(button,wmParams);
        filename = "sss.jpg";
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BitmapDrawable bd = new BitmapDrawable(shot());
                try {
                    File file = new File(Environment.getExternalStorageDirectory() + "/" + filename);
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);


                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }


            }
        });
    }


    //获得bitmap
    private Bitmap shot() {

        View view = getWindow().getDecorView();
        Toast.makeText(getApplicationContext(), "已经截屏", Toast.LENGTH_SHORT).show();
        Display display = this.getWindowManager().getDefaultDisplay();
        view.layout(0, 0, display.getWidth(), display.getHeight());
        view.setDrawingCacheEnabled(true);
        bitmap2 = Bitmap.createBitmap(view.getDrawingCache());
        Toast.makeText(getApplicationContext(), "已经截屏", Toast.LENGTH_SHORT).show();

        return bitmap2;
    }

} 

大家应该可以看得出这里用了windowmanager;这是对窗口的管理,比较经典的例子就是360的浮动窗口,但是这儿不一样,这个是应用内部的,这区别在于

final WindowManager windowmanager= (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION       ;

首先,大家应该都知道context这玩意,也都知道getapplicationcontext以及mainactivity.this的区别,在这儿,如果你跟我也是用

 final WindowManager windowmanager= (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

那么,就是在应用内部,创造那个按钮,也就是说你的按钮的范围只能在你当前的那个activity里面,无法向360那样直接突破应用层,至于LayoutParams的type类型,在sdk的文档里面有讲到,希望大家自己去查查看sdk,附上windwomanager的layoutparams位置:D:\android\sdk\docs\reference\android\view\WindowManager.LayoutParams.html

关于type类型,sdk里面有讲到,但是我是看不懂的,所以只能一个一个的去试,希望你们看得懂。

最后,加上所需要的权限


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