android screen shot

皇甫学海
2023-12-01
 

方法一:

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;

 


  1. public class ScreenShot {


  2. // 获取指定Activity的截屏,保存到png文件
  3. private static Bitmap takeScreenShot(Activity activity){


  4. //View是你需要截图的View
  5. View view = activity.getWindow().getDecorView();
  6. view.setDrawingCacheEnabled(true);
  7. view.buildDrawingCache();
  8. Bitmap b1 = view.getDrawingCache();


  9. //获取状态栏高度
  10. Rect frame = new Rect();
  11. activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
  12. int statusBarHeight = frame.top;
  13. System.out.println(statusBarHeight);

  14. //获取屏幕长和高
  15. int width = activity.getWindowManager().getDefaultDisplay().getWidth();
  16. int height = activity.getWindowManager().getDefaultDisplay().getHeight();


  17. //去掉标题栏
  18. //Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
  19. Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
  20. view.destroyDrawingCache();
  21. return b;
  22. }


  23. //保存到sdcard
  24. private static void savePic(Bitmap b,String strFileName){
  25. FileOutputStream fos = null;
  26. try {
  27. fos = new FileOutputStream(strFileName);
  28. if (null != fos)
  29. {
  30. b.compress(Bitmap.CompressFormat.PNG, 90, fos);
  31. fos.flush();
  32. fos.close();
  33. }
  34. } catch (FileNotFoundException e) {
  35. e.printStackTrace();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }


  40. //程序入口
  41. public static void shoot(Activity a){
  42. ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
  43. }
  44. }

 

 类似资料:

相关阅读

相关文章

相关问答