[java]
package com.example.capture;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick_CaptureScreen(View view){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
View v = getWindow().getDecorView();
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
Bitmap srcBitmap = v.getDrawingCache();
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Point outSize = new Point();
getWindowManager().getDefaultDisplay().getSize(outSize);
int width = outSize.x;
int height = outSize.y;
Bitmap bitmap = Bitmap.createBitmap(srcBitmap, 0, statusBarHeight, width, height – statusBarHeight);
v.destroyDrawingCache();
FileOutputStream fos = null;
try{
File file = File.createTempFile("capture", ".jpg",new File("/sdcard"));
fos = new FileOutputStream(file);
if(null != fos){
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
Toast.makeText(MainActivity.this, "已成功截屏,截屏文件名为:" + file.getName(), Toast.LENGTH_LONG).show();
}
fos.close();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this, "截图失败", Toast.LENGTH_LONG).show();
}
}
}, 2000);
}
@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;
}
}
[/java]
布局文件
[xml]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick_CaptureScreen"
android:text="屏幕截图(保存到SD卡根目录)" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/wp" />
</LinearLayout>
[/xml]
配置文件
[xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.capture"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.capture.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
[/xml]