最近需要做个截屏的小功能,查了下资料,看了别人的文章,然后自己整理下,做个笔记。
package com.SceenCut;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class SceenCut extends Activity {
/** Called when the activity is first created. */
private boolean customTitleSupported;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
ImageView im = (ImageView) findViewById(R.id.img);
Bitmap bmp = Bitmap.createBitmap(800, 480, Config.ARGB_8888);
View cv = getWindow().getDecorView();
cv.draw(new Canvas(bmp));
im.setScaleType(ScaleType.FIT_XY);
im.setImageBitmap(bmp);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("/sdcard/"
+ sdf.format(new Date()) + ".jpg");
if (fos != null) {
bmp.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}