给自己制定了一个计划,今年要开发一款属于自己的android的app,开始从基础学起,翻看《疯狂android讲义》回顾一下android开发的基本知识,同时最近要在安卓上实现一个截图功能,通过书写几个demo实现简单的截图功能
目前android截图的方式有两种,一种应用外截图,一种是通过应用内截图
第一种:系统工具截图
- 1 同时按住关机键+声音下键,或关机键+home键,或者点击设置里面的截图
- 2 用百度手机助手,豌豆荚,应用宝这种电脑截图
第二种:应用内截图:
- 1 android代码中直接调用shell命令(screencap -p)实现截图,这种情况的前提是需要手机root,而且对手机系统也有要求
直接上代码:
MainActivity.java
package com.example.baidu.num1_adbshot;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.BufferedOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button butn;
boolean flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
butn = (Button)this.findViewById(R.id.button);
butn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
screenShotByCommand();
}
});
}
public boolean screenShotByCommand() {
Process process = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
String fd = "/sdcard/screenshot/" + sdf.format(new Date()) + ".png";
try{
process = Runtime.getRuntime().exec("su",null,null);
OutputStream outputStream = null;
//PrintStream outputStream = null;
try {
outputStream = process.getOutputStream();
outputStream.write(("screencap -p " + fd).getBytes("utf-8"));
System.out.println("kanty add screencap");
//outputStream = new PrintStream(new BufferedOutputStream(process.getOutputStream(), 8192));
//outputStream.println("screencap -p " + fd);
outputStream.flush();
}catch(Exception e){
// 没有权限
//BNSettingManager.setRootScreenshotState(false);
return false;
} finally {
if (outputStream != null) {
outputStream.close();
}
}
process.waitFor();
}catch(Exception e){
System.out.println("fhdjfd");
}finally {
if(process != null){
process.destroy();
}
}
return true;
}
}
AndroidManifet.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.baidu.num1_adbshot">
<uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.baidu.num1_adbshot.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="jietu"
/>
</RelativeLayout>
第二种:基于view截图
app应用内截图也分为两种方式,一种是基于SurfaceView,另一种是基于GLSurfaceView,普通应用surfaceview,而通常由于是通过OpenGL实现,故这类就属于基于GLSurfaceView
- 1、普通view截图方式
直接上代码:
MainAcitivity.java
package com.example.baidu.sdkscreenshot;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.graphics.Bitmap.Config;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.my_button);
this.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
screenshot();
}
});
}
public void screenshot(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
String fname = "/sdcard/" + sdf.format(new Date()) + ".png";
//View view = v.getRootView();
View view = getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap != null) {
System.out.println("bit map got");
FileOutputStream out = null;
try {
out = new FileOutputStream(fname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
System.out.println("file" + fname + "output done");
} else {
System.out.println("bitmap is null");
}
}
}
AndroidManifest.xml
<?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.baidu.sdkscreenshot">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.baidu.sdkscreenshot.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="helloworild"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nicebutton"
android:id="@+id/my_button"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
2、GLsurfaceview截图方式
后续补充