android实现截屏功能代码
来源:互联网 发布:网上刷火车票软件 编辑:程序博客网 时间:2024/05/18 16:36
android开发中通过View的getDrawingCache方法可以达到截屏的目的,只是缺少状态栏!
原始界面
截屏得到的图片
代码实现
1. 添加权限(AndroidManifest.xml文件里)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. 添加1个Button(activity_main.xml文件)
<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>
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 {@Overrideprotected 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() {@Overridepublic 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) {} }}@Overridepublic 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;}}
0 0
- android代码实现截屏功能
- android实现截屏功能代码
- android实现截屏功能代码
- Android实现截屏功能代码
- android自动更新功能实现 代码
- Android 实现分享功能代码
- android 单行代码实现功能
- android截屏功能实现
- android截屏功能实现
- android截屏功能实现
- 【Android】实现截屏功能
- android实现截屏功能
- Android代码实现图片圆角功能---------------
- Android应用自动更新功能的代码实现
- Android应用自动更新功能的代码实现
- Android应用自动更新功能的代码实现
- Android应用自动更新功能的代码实现
- Android应用自动更新功能的代码实现
- dubbo + zookeeper + spring 实战++
- 数据库中的内连接、自然连接、外连接
- 浙江中医药大学-数据结构与算法期末考试应用题图论补充
- c++智能指针
- OMNET++安装
- android实现截屏功能代码
- ccp协议
- VBA 获取窗口标题 - 在PPT放映窗口与其他窗口间切换
- 把握每一个今天
- 部署 instance 到 OVS flat network - 每天5分钟玩转 OpenStack(135)
- 387. First Unique Character in a String
- Java中的拦截器、过滤器、监听器
- python chr()和ord()
- hibernate配置文件XML与注解