Android 布局转化成图片(bitmap)

博客解决如何将包含ScrollView的复杂布局转换为图片并保存到相册的问题。重点介绍了在onWindowFocusChanged方法中获取View宽高的时机,并指出onCreate等生命周期方法中无法准确获取。还提及了在动态创建View时需等待绘制完成才能获取尺寸,提供了相关资源链接以供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此篇博客是为了解决上一篇中的问题-----无法保存完整的页面(长页面),
https://blog.youkuaiyun.com/qq_39994562/article/details/118760643
在上篇博文中有关于图片保存到系统相册的代码,不再赘述。
在上篇的基础上修改了一下布局文件,如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:orientation="vertical"
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/AllView">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:orientation="vertical">
            <Button
                android:id="@+id/bt_hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"/>
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:src="@mipmap/ic_launcher"
                tools:ignore="MissingConstraints" />
            <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值