java - 屏幕截图与显示 View 不相似

标签 java android image screenshot rounded-corners

我截取了布局的屏幕截图,但提供的图像与显示运行时 View 不相似。 我使用 com.github.chrisbanes.photoview.PhotoView 库在图像上进行捏合缩放手势。

<LinearLayout
            android:id="@+id/ContentLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/random"
            android:orientation="horizontal"
            android:weightSum="2">

            <androidx.cardview.widget.CardView
                android:id="@+id/ImageViewLeftCard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:cardCornerRadius="20dp"
                android:layout_margin="1dp"
                android:layout_weight="1">
            <com.github.chrisbanes.photoview.PhotoView
                android:id="@+id/ImageViewLeft"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/blackcolor"/>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/ImageViewRightCard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:cardCornerRadius="20dp"
                android:layout_margin="1dp"
                android:layout_weight="1">
            <com.github.chrisbanes.photoview.PhotoView
                android:id="@+id/ImageViewRight"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/blackcolor" />
            </androidx.cardview.widget.CardView>

</LinearLayout>

enter image description here

我需要屏幕截图后的图像,与显示 View 相同。

我正在使用此代码截取屏幕截图。

private void screenshot(View v){
    v.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
}

我面临的问题..用简单的措辞来说,我需要屏幕截图后带有圆角的图像。

最佳答案

试试这个

private Bitmap extractBitmap(ViewGroup iViewGroup) {

int width = iViewGroup.getWidth();
int height = iViewGroup.getWidth();

Bitmap createBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(createBitmap);

iViewGroup.draw(canvas);

return createBitmap;

  }
}

关于java - 屏幕截图与显示 View 不相似,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62452995/

相关文章:

java - Volley JSONObject POST 请求没有响应

java - 无法将 JMX 与 Spring 应用程序集成

java - 使用 AJAX、JAVA WS-RS @Post 发送 Xml

javascript - 如何自动调用 RESTful Web 服务

android - 在 eclipse 中启动 avd 时缺少特定于 arch 的模拟器程序错误

python - 如何获取多个字节的最低有效位?

html - 我需要在 img2 上使用这些样式吗?

java - GWT 隐藏 DataGrid 中的分组数据

android - 使用什么来存储目录应用程序的项目更好? SQL 数据库还是其他类型的数据结构?

image - React Native 图像无法使用特定 URL