android 截屏

public class PrintScreenActivity extends Activity {

    /** Called when the activity is first created. */

        private static final String TAG = PrintScreenActivity.class.getName();

        private Button btnShort;

        ImageView image = null;

        public static String SCREEN_SHOTS_LOCATION = Environment.getExternalStorageDirectory().getPath();

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.main);     

        image = (ImageView)findViewById(R.id.image);

        btnShort = (Button) findViewById(R.id.btnShots);

        btnShort.setOnClickListener(new Button.OnClickListener() {

                public void onClick(View v) {

                        try {

                                takeScreenShot(getWindow().peekDecorView(),SCREEN_SHOTS_LOCATION);

                        } catch (Exception e) {

                                // TODO Auto-generated catch block

                                Log.e("tag",TAG);

                        }

                }

        });

    }

    public void takeScreenShot(View view) throws Exception {

            takeScreenShot(view, "default");

    }

public void takeScreenShot(View view, String name) throws Exception {

                view.setDrawingCacheEnabled(true);

                view.buildDrawingCache();

                Bitmap bitmap = view.getDrawingCache();

                Canvas canvas = new Canvas(bitmap);

                int w = bitmap.getWidth();

                int h = bitmap.getHeight();

                Paint paint = new Paint();

                paint.setColor(Color.YELLOW);

SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

                String time = simple.format(new Date());

                

                canvas.drawText(time, w-w/2, h-h/10, paint);

                canvas.save();

                canvas.restore();

                FileOutputStream fos = null;

                try {

                        File sddir = new File(SCREEN_SHOTS_LOCATION);

                        if (!sddir.exists()) {

                                sddir.mkdirs();

                        }

                        image.setImageBitmap(bitmap);

                        File file = new File(SCREEN_SHOTS_LOCATION +File.separator+"screen" + ".png");

                        

                        fos = new FileOutputStream(file);

                        if (fos != null) {

                                bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);

                                fos.close();

                        }

                } catch (Exception e) {

                        Log.e("tag",e.getCause().toString());

                        e.printStackTrace();

                }

        }

}

view.buildDrawingCache()有时不一定起作用,因为如果有硬件加速的话,drawingcache是失效的,所以也可以这样

 

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

                Canvas c = new Canvas(bitmap);

                view.dispatchDraw(c);

猜你喜欢

转载自892848153.iteye.com/blog/1995911