截取特定布局的屏幕截图
Taking screenshot of a particular layout
我正在尝试截取特定布局的屏幕截图,但问题是,屏幕截图随操作栏一起提供,并且上面还有一个蓝色补丁,虽然在截取屏幕截图时我隐藏了操作栏,但它仍然存在但它对 fab 按钮工作正常。
我提到下面的代码-
try {
actionBar.hide();
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "Hi" + ".jpg";
fabSpeedDial.setVisibility(View.INVISIBLE);
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
fabSpeedDial.setVisibility(View.VISIBLE);
actionBar.show();
return true;
} catch (Throwable e) {
e.printStackTrace();
}
Xml代码-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.ragnar.useless.MainActivity"
android:background="#ff4343"
android:id="@+id/l1">
<ImageView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="180dp"
android:id="@+id/img"
android:layout_height="180dp"
android:src="@drawable/panda"/>
<io.github.yavski.fabspeeddial.FabSpeedDial
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|end"
app:fabGravity="bottom_end"
app:fabMenu="@menu/main_menu"
android:id="@+id/fabspeed"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
app:miniFabDrawableTint="?attr/colorPrimaryDark"
app:miniFabTitleTextColor="?attr/colorPrimaryDark"
>
</io.github.yavski.fabspeeddial.FabSpeedDial>
</RelativeLayout>
我要的是没有获取到actionbar和上面的bluepatch的Relativelayout的截图。
任何建议将不胜感激,而且我在 Whosebug 上看到了类似的问题,但无法理解它的答案,所以请解释你提供的内容。提前谢谢你。
这是问题所在:
View v1 = getWindow().getDecorView().getRootView();
您正在截取根视图的屏幕截图。
您必须在 RelativeLayout 上放置一个 ID,它是 activity 布局的根,如下所示:
android:id="@+id/myview"
然后将我提到的第一行替换为:
View v1 = findViewById(R.id.myview);
您将获得布局的位图,没有操作栏和上面的蓝色条(状态栏的背景)。
让我知道它是否适合你!
我正在尝试截取特定布局的屏幕截图,但问题是,屏幕截图随操作栏一起提供,并且上面还有一个蓝色补丁,虽然在截取屏幕截图时我隐藏了操作栏,但它仍然存在但它对 fab 按钮工作正常。
我提到下面的代码-
try {
actionBar.hide();
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "Hi" + ".jpg";
fabSpeedDial.setVisibility(View.INVISIBLE);
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
fabSpeedDial.setVisibility(View.VISIBLE);
actionBar.show();
return true;
} catch (Throwable e) {
e.printStackTrace();
}
Xml代码-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.ragnar.useless.MainActivity"
android:background="#ff4343"
android:id="@+id/l1">
<ImageView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="180dp"
android:id="@+id/img"
android:layout_height="180dp"
android:src="@drawable/panda"/>
<io.github.yavski.fabspeeddial.FabSpeedDial
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom|end"
app:fabGravity="bottom_end"
app:fabMenu="@menu/main_menu"
android:id="@+id/fabspeed"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
app:miniFabDrawableTint="?attr/colorPrimaryDark"
app:miniFabTitleTextColor="?attr/colorPrimaryDark"
>
</io.github.yavski.fabspeeddial.FabSpeedDial>
</RelativeLayout>
我要的是没有获取到actionbar和上面的bluepatch的Relativelayout的截图。 任何建议将不胜感激,而且我在 Whosebug 上看到了类似的问题,但无法理解它的答案,所以请解释你提供的内容。提前谢谢你。
这是问题所在:
View v1 = getWindow().getDecorView().getRootView();
您正在截取根视图的屏幕截图。 您必须在 RelativeLayout 上放置一个 ID,它是 activity 布局的根,如下所示:
android:id="@+id/myview"
然后将我提到的第一行替换为:
View v1 = findViewById(R.id.myview);
您将获得布局的位图,没有操作栏和上面的蓝色条(状态栏的背景)。 让我知道它是否适合你!