找了大部分资源,要不就是截当前屏幕,或者截取长图,没有相关滚动到中间部分,截屏(包含上部分滚过去隐藏的部分+当前界面部分),记录下本需求,代码直接可用,文章最后有保存到本地方法
一、截取当前屏幕
private Bitmap screenShot(Context context) {
Activity activity = (Activity) context;
View decorView = activity.getWindow().getDecorView();
decorView.setDrawingCacheEnabled(true);
decorView.buildDrawingCache();
return Bitmap.createBitmap(decorView.getDrawingCache());
}
二、截长图(webview可用)
private Bitmap getLongImage(WebView mWebView) {
//webView长屏
Picture snapShot = mWebView.capturePicture();
if (snapShot == null || snapShot.getHeight() <= 0 || snapShot.getWidth() <= 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(snapShot.getWidth(),
snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
snapShot.draw(canvas);
return bitmap;
}
三、滚动截图,注意注意!!!(含上部分+当前区域,不含当前显示屏幕一下的内容,使用了ScrollView+WebView)
右上角通过popupWindow实现的,所以代码会多点
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lb.webcore.ui.CustomWebActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/common_primary_color">
<ImageView
android:id="@+id/iv_back"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="@dimen/dp_10"
android:layout_centerVertical="true"
android:src="@drawable/iv_close" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="20sp"
android:textColor="@color/white"
android:text="截屏" />
<Button
android:id="@+id/btn_custom_web"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@drawable/ef_right_menu" />
</RelativeLayout>
<ScrollView
android:id="@+id/custom_scroll"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<WebView
android:id="@+id/custom_web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
</LinearLayout>
public class CustomWebActivity extends AppCompatActivity {
private WebView mWebView;
private ScrollView mScro;
private Button mBtnMenu;
private ImageView mIgBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_web);
mWebView = (WebView) findViewById(R.id.custom_web);
mScro = (ScrollView) findViewById(R.id.custom_scroll);
mBtnMenu = (Button) findViewById(R.id.btn_custom_web);
mIgBack = (ImageView) findViewById(R.id.iv_back);
// 获得用于控制设置的WebSettings对象 * WebView
WebSettings settings = mWebView.getSettings();
mWebView.loadUrl(“file:android_asset/a/a.html”);
if (mWebView != null) {
settings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
settings.setSupportZoom(false);// 用于 设置webview放大
settings.setBuiltInZoomControls(true);
mWebView.requestFocus();
settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
settings.setBlockNetworkImage(true);
// 开启H5(APPCache)缓存功能
settings.setAppCacheEnabled(true);
// 开启 DOM storage 功能
settings.setDomStorageEnabled(true);
// 应用可以有数据库
settings.setDatabaseEnabled(true);
// 可以读取文件缓存(manifest生效)
settings.setAllowFileAccess(true);
settings.setSupportMultipleWindows(true);//支持多窗口打开
settings.setGeolocationEnabled(true);
mWebView.setDrawingCacheEnabled(true);
// 清除缓存和记录 用来处理webview开始载入时候出现白屏
mWebView.clearCache(true);
mWebView.clearHistory();
mWebView.getSettings().setGeolocationEnabled(true);
}
mBtnMenu.setOnClickListener(new View.OnClickListener() {
private PopupWindow popupWindow;
@Override
public void onClick(View view) {
//获取自定义菜单的布局文件
View popupWindow_view = getLayoutInflater().inflate(R.layout.custom_menu, null, false);
//创建实例,设置菜单宽高
popupWindow = new PopupWindow(popupWindow_view, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
//截屏
popupWindow_view.findViewById(R.id.btn_screenshot).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap bitmapByView = ScreenShot.shoot(mScro);
saveBitmap(bitmapByView);
popupWindow.dismiss();
}
});
//截长屏
popupWindow_view.findViewById(R.id.btn_screen_long).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap longImage = getLongImage(mWebView);
saveBitmap(longImage);
popupWindow.dismiss();
}
});
//设置菜单显示在按钮的下面
popupWindow.showAsDropDown(findViewById(R.id.btn_custom_web), 0, 0);
//单机其它位置隐藏菜单
popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//如果菜单存在并且处于显示状态
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();//关闭菜单
popupWindow = null;
}
return false;
}
});
}
});
mIgBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
private Bitmap getLongImage(WebView mWebView) {
//webView长屏
Picture snapShot = mWebView.capturePicture();
if (snapShot == null || snapShot.getHeight() <= 0 || snapShot.getWidth() <= 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(snapShot.getWidth(),
snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
snapShot.draw(canvas);
return bitmap;
}
private void saveBitmap(Bitmap bitmap) {
try {
String saveFileUrl = saveBitmap(bitmap, “保存文件路径” System.currentTimeMillis() + ".png");
if (!TextUtils.isEmpty(saveFileUrl)) {
Toast.makeText(CustomWebActivity.this, "截取成功" + saveFileUrl, Toast.LENGTH_SHORT).show();
Log.d("测试截取路径", saveFileUrl);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//保存文件
public static String saveBitmap(Bitmap bm, String folderName, String fileName) throws IOException {
File dirFile = new File(folderName);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
File myCaptureFile = new File(folderName, fileName);
if (myCaptureFile.exists()) {
myCaptureFile.delete();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
try {
bm.compress(Bitmap.CompressFormat.JPEG, 85, bos);
} catch (Exception e) {
e.printStackTrace();
}
bos.flush();
bos.close();
return myCaptureFile.getAbsolutePath();
}
}