对屏幕进行截图

/**
 * 对屏幕进行截图
 * @author Administrator
 *
 */
public class ScreenShot {

public static final String RESULTIMGNAME = "test_result.jpg";
public static String RESULT_IMAGE;


// 获取指定Activity的截屏,保存到png文件
    private static Bitmap takeScreenShot(Activity activity, int imageHeight , int titleBarHeight) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();


        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        Log.i("TAG", "" + statusBarHeight);


        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight();
        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight+titleBarHeight, width, imageHeight);
        view.destroyDrawingCache();
        return b;
    }


    // 保存到sdcard
    private static void savePic(Bitmap b, File file) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            if (null != fos) {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static File initImagePath(Activity mContext) throws IOException {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& Environment.getExternalStorageDirectory().exists()) {
    RESULT_IMAGE = Environment.getExternalStorageDirectory().getAbsolutePath() +File.separator +  RESULTIMGNAME;
}
else {
RESULT_IMAGE = mContext.getApplication().getFilesDir().getAbsolutePath() +File.separator +  RESULTIMGNAME;
}
   
    File file = new File(RESULT_IMAGE);
    if(!file.exists()) {
file.createNewFile();
    } else {
    file.delete();
    file.createNewFile();
    }
   
    return file;
    }


    // 程序入口
    public static String shoot(Activity mContext, int imageHeight , int titleBarHeight){
    try {
File file = initImagePath(mContext);
ScreenShot.savePic(takeScreenShot(mContext,imageHeight,titleBarHeight), file);
return file.getAbsolutePath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   
    return null;
    }
}
Android对屏幕进行管理的方式有很多,以下是一些常见的方式: 1. 使用布局文件适配不同尺寸和分辨率的屏幕:在Android开发中,可以使用不同的布局文件来适配不同尺寸和分辨率的屏幕。通过使用不同的布局文件,可以保证在不同的设备上显示效果一致,避免出现界面错位、显示不全等问题。 2. 使用屏幕方向控制屏幕旋转:Android系统可以根据设备的旋转方向自动调整屏幕的方向,同时应用程序也可以通过设置指定的屏幕方向来控制屏幕显示方向。这些方向包括竖屏、横屏、反向竖屏和反向横屏等。 3. 使用亮度和背光控制管理屏幕亮度:Android系统可以根据环境光线自动调整屏幕亮度和背光,同时也可以通过设置手动调整亮度和背光。可以使用`WindowManager.LayoutParams`类来设置屏幕亮度和背光。 4. 使用截图和录屏功能管理屏幕:Android系统可以方便地进行屏幕截图和录屏操作,以方便用户进行界面展示和操作演示。可以使用`MediaProjectionManager`类和`MediaProjection`类来实现屏幕截图和录屏。 5. 使用多屏幕展示管理屏幕:Android系统支持多屏幕展示,可以将同一个应用程序的不同界面分别显示在不同的屏幕上,以实现更加丰富的操作和展示效果。可以使用`Presentation`类来实现多屏幕展示。 以上是一些常见的Android对屏幕进行管理的方式,具体实现方式和API可以查看Android开发文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值