【发布时间】:2014-06-02 10:12:50
【问题描述】:
我实现了一个由导航抽屉中的项目触发的屏幕截图功能。它工作正常,但我想对我看到的整个页面进行截图没有导航抽屉。
我使用了谷歌教程中建议的布局来实现抽屉导航:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Main content view-->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--Navigation Drawer-->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="@color/sw21Background"/>
</android.support.v4.widget.DrawerLayout>
我的代码从被覆盖的监听器方法开始:
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position, view); // my code, e.g. takeScreenshot(view);
}
}
/**
* Swap fragments in the main content view
*/
private void selectItem(int position, View view) {
Fragment fragment = new Fragment();
if (position == 0) {
...
} else if (position == 3) {
//Feedback
// TODO get proper view of current page not the navigation drawer
Bitmap bitmap = this.takeScreenShot(view.getRootView());
Uri uri = saveBitmap(bitmap);
sendEmail(uri);
} ...
//Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
//Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(navigationItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
当前视图被导航栏部分隐藏,所以下面的代码也对导航栏进行了截图:
private Bitmap takeScreenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
我尝试使用嵌套在 FrameLayout(内容)中的视图,但它没有覆盖操作栏。
【问题讨论】:
-
stackoverflow.com/questions/5321344/… 这应该是你要找的。span>
标签: android navigation drawer