下面列出了android.webkit.WebView#layout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Create a picture out of {@link WebView}'s whole content
*
* @param webView The WebView to get contents from
* @return A {@link Bitmap} or null
*/
@Nullable
public static Bitmap getBitmapFromWebView(final WebView webView) {
try {
//Measure WebView's content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
webView.measure(widthMeasureSpec, heightMeasureSpec);
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
//Build drawing cache and store its size
webView.buildDrawingCache();
int measuredWidth = webView.getMeasuredWidth();
int measuredHeight = webView.getMeasuredHeight();
//Creates the bitmap and draw WebView's content on in
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
webView.draw(canvas);
webView.destroyDrawingCache();
return bitmap;
} catch (Exception | OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
/**
* Create a picture out of {@link WebView}'s whole content
*
* @param webView The WebView to get contents from
* @return A {@link Bitmap} or null
*/
@Nullable
public static Bitmap getBitmapFromWebView(final WebView webView) {
try {
//Measure WebView's content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
webView.measure(widthMeasureSpec, heightMeasureSpec);
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
//Build drawing cache and store its size
webView.buildDrawingCache();
int measuredWidth = webView.getMeasuredWidth();
int measuredHeight = webView.getMeasuredHeight();
//Creates the bitmap and draw WebView's content on in
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
webView.draw(canvas);
webView.destroyDrawingCache();
return bitmap;
} catch (Exception | OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
/**
* Create a picture out of {@link WebView}'s whole content
*
* @param webView The WebView to get contents from
* @return A {@link Bitmap} or null
*/
@Nullable
public static Bitmap getBitmapFromWebView(WebView webView) {
try {
//Measure WebView's content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
webView.measure(widthMeasureSpec, heightMeasureSpec);
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
//Build drawing cache and store its size
webView.buildDrawingCache();
int measuredWidth = webView.getMeasuredWidth();
int measuredHeight = webView.getMeasuredHeight();
//Creates the bitmap and draw WebView's content on in
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
webView.draw(canvas);
webView.destroyDrawingCache();
return bitmap;
} catch (Exception | OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
/**
* 系统加载
*/
protected void systemLoader(Context context)
{
if (RockySdk.getInstance().getContext() != null)
{
return;
}
Context application = context.getApplicationContext();
// 用于获取浏览器代理
WebView webview = new WebView(context);
webview.layout(0, 0, 0, 0);
WebSettings webSettings = webview.getSettings();
ApplicationInfo applicationInfo = context.getApplicationInfo();
VersionInfo versionInfo = ManifestTools.getVersionInfo(application);
String userAgent = webSettings.getUserAgentString();
// Structure the ClientInfo.
ClientInfo clientInfo = new ClientInfo(application.getPackageName());
//clientInfo.setAppicon(R.drawable.ic_launcher);
clientInfo.setAppicon(applicationInfo.icon);
clientInfo.setAppname(ManifestTools.getApplicationLable(application));
clientInfo.setDeviceType(SdkConfig.Device.PHONE);
clientInfo.setDeviceName(android.os.Build.MODEL);
clientInfo.setAlias(android.os.Build.MODEL);
clientInfo.setSdkVersion(android.os.Build.VERSION.SDK_INT);
clientInfo.setMac(DeviceInfoManager.getMacAddress(application));
// Webkit user-agent
clientInfo.setUserAgent(userAgent);
if (versionInfo != null)
{
clientInfo.setVersionCode(versionInfo.getVersionCode());
clientInfo.setVersionName(versionInfo.getVersionName());
}
// FIXME: Take attention...
clientInfo.addFlags(ClientInfo.FLAG_DEBUG | ClientInfo.FLAG_RELEASE);
// clientContext.addFlags(ClientContext.FLAG_DEBUG);
// TODO: 广告
RockyConfig config = new RockyConfig.Builder(application)
.clientInfo(clientInfo)
.hasAdBanner(false) // 显示积分Banner
.hasAdPointsWall(true) // 显示积分墙
.build();
RockySdk.getInstance().init(config);
}
/**
* 系统初始化
*
* @param context
*/
public static void init(Context context)
{
if (RockySdk.getInstance().getContext() != null)
{
return;
}
Context application = context.getApplicationContext();
// 用于获取浏览器代理
WebView webview = new WebView(context);
webview.layout(0, 0, 0, 0);
WebSettings webSettings = webview.getSettings();
ApplicationInfo applicationInfo = context.getApplicationInfo();
VersionInfo versionInfo = ManifestTools.getVersionInfo(application);
String userAgent = webSettings.getUserAgentString();
// Structure the ClientInfo.
ClientInfo clientInfo = new ClientInfo(application.getPackageName());
//clientInfo.setAppicon(R.drawable.ic_launcher);
clientInfo.setAppicon(applicationInfo.icon);
clientInfo.setAppname(ManifestTools.getApplicationLable(application));
clientInfo.setDeviceType(SdkConfig.Device.PHONE);
clientInfo.setDeviceName(android.os.Build.MODEL);
clientInfo.setAlias(android.os.Build.MODEL);
clientInfo.setSdkVersion(android.os.Build.VERSION.SDK_INT);
clientInfo.setMac(DeviceInfoManager.getMacAddress(application));
// Webkit user-agent
clientInfo.setUserAgent(userAgent);
if (versionInfo != null)
{
clientInfo.setVersionCode(versionInfo.getVersionCode());
clientInfo.setVersionName(versionInfo.getVersionName());
}
// FIXME: Take attention...
clientInfo.addFlags(ClientInfo.FLAG_DEBUG | ClientInfo.FLAG_RELEASE);
// clientContext.addFlags(ClientContext.FLAG_DEBUG);
// TODO: 广告
RockyConfig config = new RockyConfig.Builder(application)
.clientInfo(clientInfo)
.hasAdBanner(false) // 显示积分Banner
.hasAdPointsWall(true) // 显示积分墙
.build();
RockySdk.getInstance().init(config);
}