Skip to content

Instantly share code, notes, and snippets.

@seLc7
Last active August 29, 2015 14:18
Show Gist options
  • Save seLc7/cbae056168ef83055df9 to your computer and use it in GitHub Desktop.
Save seLc7/cbae056168ef83055df9 to your computer and use it in GitHub Desktop.
Android-screenshot:屏幕截图
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000"/><!-- 实体设置为透明 -->
<!-- 描边:一般大小都是1dp -->
<stroke android:width="1dp" android:color="#FFFFFF" />
<!-- 边角的圆弧半径 -->
<corners android:radius="20dp" />
<!-- 四周留出来的空白,和xml文件中的pad效果一样,对内起作用 -->
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ssfb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_FRAME_BUFFER"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Runtime.getRuntime().exec(
new String[]{"/system/bin/su", "-c",
"chmod 777 /dev/graphics/fb0"});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat sDay = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sTime = new SimpleDateFormat("HHmmss");
String date = sDay.format(new java.util.Date()) + "_" + sTime.format(new java.util.Date());
DateFormat df = new SimpleDateFormat("HH:mm:ss");
String d = df.format(new Date());
Log.i("out", d);
/* Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month;
int day = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;
String date = "" + year + month + day + hour + minute + second;*/
Log.i("out", date);
String path = "/sdcard/" + date + ".png";
Log.i("out", path);
try {
// screenShot();
// screenCapture("/sdcard/jietu.png");
screenCapture("/sdcard/" + date + ".png");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void startMyService() {
Intent intent = new Intent(this, MyService.class);
startService(intent);
this.finish();
}
/**
* 第一种方法
* 类似于getview那个方法,好像只能获取当前activity的截图,但是缩略图显示确实是当前屏幕截屏
*/
private void screenShot() throws Exception {
Process sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img_shot.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
}
/**
* 第二种方法
*
* @param path 图片保存路径
*/
public void screenCapture(String path) {
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
PrintStream outputStream = null;
try {
outputStream = new PrintStream(new BufferedOutputStream(process.getOutputStream(), 8192));
outputStream.println("screencap -p " + path);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
this.finish();
}
}
/**
* 第三种方法
*/
private void getFromFramebuffer() throws Exception {
// 获取屏幕大小
DisplayMetrics metrics = new DisplayMetrics();
Context mContext = this;
WindowManager WM = (WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE);
Display display = WM.getDefaultDisplay();
display.getMetrics(metrics);
int height = metrics.heightPixels; // 屏幕高
int width = metrics.widthPixels; // 屏幕的宽
int pixelformat = display.getPixelFormat();
PixelFormat localPixelFormat1 = new PixelFormat();
PixelFormat.getPixelFormatInfo(pixelformat, localPixelFormat1);
int deepth = localPixelFormat1.bytesPerPixel;// 位深
/*
* byte[] piex = new byte[height * width * deepth];
*
* InputStream stream = getInputStream(); DataInputStream dStream = new
* DataInputStream(stream); dStream.readFully(piex);
*/
// Bitmap bitmap = Bitmap.createBitmap(data, width, height, 4);
// byte[] piex =new byte[height * with * deepth];
// DataInputStream dStream = new DataInputStream(input);
// dStream.readFully(piex);
InputStream stream = getInputStream();
DataInputStream dStream = new DataInputStream(stream);
DataInput frameBuffer = new DataInputStream(dStream);
int[] data = new int[height * width * deepth / 2];
// convertToRgba32(frameBuffer, data);
convertToRgb565(frameBuffer, data);
Bitmap bm = Bitmap.createBitmap(data, width, height,
Bitmap.Config.ARGB_4444);
// _imageview.setImageBitmap(bm);
File imageFile = new File(Environment.getExternalStorageDirectory()
+ "/截屏.png");
imageFile.createNewFile();
FileOutputStream out = new FileOutputStream(imageFile);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
public static void convertToRgb565(DataInput frameBuffer, int[] into) {
for (int x = 0; x < into.length; x++) {
try {
into[x] = frameBuffer.readShort();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static InputStream getInputStream() throws Exception {
FileInputStream buf = new FileInputStream(new File("/dev/graphics/fb0"));
return buf;
}// get the InputStream from framebuffer
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<style name="dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@drawable/activity_dialog</item>
<item name="android:windowFrame">@null</item>
<!-- activity无黑边 -->
<item name="android:windowIsFloating">true</item>
<!-- activity可滑动 -->
<item name="android:windowIsTranslucent">false</item>
<!-- activity透明 -->
<item name="android:windowNoTitle">true</item>
<!-- activity无标题 -->
<!-- <item name="android:windowBackground">@null</item>-->
<item name="android:backgroundDimEnabled">false</item>
<!-- activity不变暗 -->
<item name="android:windowCloseOnTouchOutside">false</item>
<!-- activity点击空白部分不消失 -->
</style>
@seLc7
Copy link
Author

seLc7 commented Apr 1, 2015

采用的是第二种方法,将Activity缩小透明化,并在执行完成house执行this.finish()。

@seLc7
Copy link
Author

seLc7 commented Apr 1, 2015

activity_dialog.xml 放在res/drawble/ 下,记得在style.xml中添加background,manifest中记得调用style。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment