首页

android 屏幕截图 示例源码

java

2020-7-9

package com.lany.screenshot;

import java.io.File;
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;

public class ScreenShot {

	public static void shoot(Activity a, File filePath) {
		if (filePath == null) {
			return;
		}
		if (!filePath.getParentFile().exists()) {
			filePath.getParentFile().mkdirs();
		}
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(filePath);
			if (null != fos) {
				takeScreenShot(a).compress(Bitmap.CompressFormat.PNG, 100, fos);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.flush();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static Bitmap takeScreenShot(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bitmap = view.getDrawingCache();
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		int width = activity.getWindowManager().getDefaultDisplay().getWidth();
		int height = activity.getWindowManager().getDefaultDisplay()
				.getHeight();
		// 去掉标题栏
		Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,
				height - statusBarHeight);
		view.destroyDrawingCache();
		return b;
	}

}

资源下载此资源下载价格为2D币(VIP免费),请先
资源文件列表
ScreenShot/.classpath , 475
ScreenShot/.project , 846
ScreenShot/AndroidManifest.xml , 917
ScreenShot/bin/AndroidManifest.xml , 917
ScreenShot/bin/classes/com/lany/screenshot/BuildConfig.class , 347
ScreenShot/bin/classes/com/lany/screenshot/MainActivity.class , 2152
ScreenShot/bin/classes/com/lany/screenshot/R$attr.class , 343
ScreenShot/bin/classes/com/lany/screenshot/R$drawable.class , 410
ScreenShot/bin/classes/com/lany/screenshot/R$id.class , 422
ScreenShot/bin/classes/com/lany/screenshot/R$layout.class , 406
ScreenShot/bin/classes/com/lany/screenshot/R$string.class , 436
ScreenShot/bin/classes/com/lany/screenshot/R$style.class , 434
ScreenShot/bin/classes/com/lany/screenshot/R.class , 587
ScreenShot/bin/classes/com/lany/screenshot/ScreenShot.class , 2865
ScreenShot/bin/classes.dex , 818704
ScreenShot/bin/dexedLibs/android-support-v4-dc17ef45b1c8ff0fadf472632c6274ad.jar , 273766
ScreenShot/bin/jarlist.cache , 120
ScreenShot/bin/res/crunch/drawable-hdpi/ic_launcher.png , 1284
ScreenShot/bin/res/crunch/drawable-mdpi/ic_launcher.png , 928
ScreenShot/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 1675
ScreenShot/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 2378
ScreenShot/bin/resources.ap_ , 10103
ScreenShot/bin/ScreenShot.apk , 288477
ScreenShot/gen/com/lany/screenshot/BuildConfig.java , 161
ScreenShot/gen/com/lany/screenshot/R.java , 1935
ScreenShot/ic_launcher-web.png , 16221
ScreenShot/libs/android-support-v4.jar , 758727
ScreenShot/proguard-project.txt , 781
ScreenShot/project.properties , 563
ScreenShot/res/drawable-hdpi/ic_launcher.png , 1114
ScreenShot/res/drawable-mdpi/ic_launcher.png , 736
ScreenShot/res/drawable-xhdpi/ic_launcher.png , 1522
ScreenShot/res/drawable-xxhdpi/ic_launcher.png , 2558
ScreenShot/res/layout/activity_main.xml , 843
ScreenShot/res/values/strings.xml , 173
ScreenShot/res/values/styles.xml , 697
ScreenShot/res/values-v11/styles.xml , 334
ScreenShot/res/values-v14/styles.xml , 391
ScreenShot/src/com/lany/screenshot/MainActivity.java , 1269
ScreenShot/src/com/lany/screenshot/ScreenShot.java , 1605
没有账号? 忘记密码?

社交账号快速登录