方法一:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
/**
* 得到rootView,使用View中提供的方法绘制到Canvas上
* @author dodo
*
*/
public class Main extends Activity {
Button btn;
int screenWidth, screenHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View rootView = findViewById(R.id.linearLayout);
Bitmap newb = Bitmap.createBitmap(screenWidth, screenHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(newb);
rootView.draw(canvas);
File file = new File(Environment.getExternalStorageDirectory()
+ "/" + "1.png");
FileOutputStream f = null;
try {
f = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean b = newb.compress(Bitmap.CompressFormat.PNG, 100, f);
if (b) {
Toast.makeText(Main.this, "截图成功", 1).show();
// 截图成功
}
}
});
}
}
方法二:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;