小编Mar*_*sch的帖子

如何以编程方式截取webview的屏幕截图,捕获整页?

我认为标题几乎涵盖了它,但我的活动中有一个webview.我已经在网页视图中加载了一个网址,我想要整个页面的截图(视口中的内容以及"下方"的内容).

我有代码可以对视口进行快照,我知道这可以通过在快照之前放大webview来在iOS中完成.我试过在这里使用相同的技术:

    WebView browserView = (WebView) findViewById(R.id.browserView);

    //Resize the webview to the height of the webpage
    int pageHeight = browserView.getContentHeight();
    LayoutParams browserParams = browserView.getLayoutParams();
    browserView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));

    //Capture the webview as a bitmap
    browserView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(browserView.getDrawingCache());
    browserView.setDrawingCacheEnabled(false);

    //Create the filename to use
    String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));
    String filename = Environment.getExternalStorageDirectory().toString() + "/Screenshot_" + randomFilenamepart + ".jpg";
    File imageFile = new File(filename);
    //Stream the file out to external storage as a JPEG
    OutputStream fout = null;
    try { …
Run Code Online (Sandbox Code Playgroud)

android screenshot webview

26
推荐指数
2
解决办法
2万
查看次数

在PopupWindow中正确创建片段

我是Android开发的新手,对如何完成我想要做的事感到困惑.我已经做了一些阅读和学习片段,所以我可以在各种屏幕尺寸设计之间共享布局和代码.我已经创建了几个片段并成功使用了它们.但我有一个情况,我想在手机上显示正常活动中的片段,但想在平板电脑上显示片段在PopupWindow(或类似的东西,如果有更好的选择).

我已经设法弄清楚如何膨胀片段并在单击按钮时在PopupWindow中显示它.我的代码看起来像这样:

public void onClick(View v) {
    LayoutInflater inflater = (LayoutInflater) BrowsingActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupLayout = inflater.inflate(R.layout.serverconnection_fragment, null, false);
    connectionListPopup = new PopupWindow(popupLayout, 300, 470, true);
    connectionListPopup.showAtLocation(BrowsingActivity.this.findViewById(R.id.connectionListImage), Gravity.CENTER, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

弹出窗口显示并包含serverconnection_fragment.xml中描述的UI.问题是通过这种方式创建,Fragment类ServerConnectionFragment.java永远不会被实例化,因此我的UI中的列表中没有项目,按钮上没有监听器,等等.似乎应该有一种方法让我实例化java类,让它正常膨胀片段并附加事件监听器,然后将创建的视图传递给PopupWindow构造函数,但我无法弄清楚如何.谁能帮我吗?

仅供参考,我正在使用Android-support-v4.jar文件为Fragment类构建Android 2.1.

android popupwindow android-fragments

17
推荐指数
1
解决办法
1万
查看次数