Android实现View截图并保存到相册

本文详细介绍了如何在Android应用中实现View的截图并保存到相册,包括使用View#draw()方法截图,Bitmap保存到文件,以及更新相册图库的步骤。在截图过程中注意了对elevation和translationZ产生的阴影处理的问题。

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

一、目标

对笔记进行截图,并保存到相册。

1. 效果图

在这里插入图片描述

实现第1栏第1个图标”图片“功能,对View进行截图,并保存到图库。

2. 下载地址

神马笔记最新版本:【神马笔记 版本1.2.0.apk

二、需求设计

图片方式是最能保持原有排版的一种方式,能保证完全一致的阅读体验。并且还具有一定防止修改的能力。

因此,神马笔记非常推荐以图片的方式进行分享。

实现整个功能分成3个步骤:

  1. 对View进行截图生成Bitmap
  2. 保存Bitmap图片到文件File
  3. 更新相册图库

三、准备工作

1. 实现View截图

对View进行截图,有2种方式。

  • 通过View#getDrawingCache()获取缓存的Bitmap
  • 通过View#draw()将View绘制到离屏的Bitmap
@Deprecated
public void setDrawingCacheEnabled(boolean enabled)

@Deprecated
public Bitmap getDrawingCache()

@Deprecated
public void buildDrawingCache(boolean autoScale)

@Deprecated
public void destroyDrawingCache()

@Deprecated
public void setDrawingCacheQuality(@DrawingCacheQuality int quality)

@Deprecated
public void setDrawingCacheBackgroundColor(@ColorInt int color);         
/**
 * @deprecated 
 * The view drawing cache was largely made obsolete with the introduction of
 * hardware-accelerated rendering in API 11. With hardware-acceleration, intermediate cache
 * layers are largely unnecessary and can easily result in a net loss in performance due to the
 * cost of creating and updating the layer. In the rare cases where caching layers are useful,
 * such as for alpha animations, {@link #setLayerType(int, Paint)} handles this with hardware rendering.  
 */
/**
 * For software-rendered snapshots of a small part of the View hierarchy or
 * individual Views it is recommended to create a {@link Canvas} from either a {@link Bitmap} or
 * {@link android.graphics.Picture} and call {@link #draw(Canvas)} on the View.
 */
/**
 * However these
 * software-rendered usages are discouraged and have compatibility issues with hardware-only
 * rendering features such as {@link android.graphics.Bitmap.Config#HARDWARE Config.HARDWARE}
 * bitmaps, real-time shadows, and outline clipping. For screenshots of the UI for feedback
 * reports or unit testing the {@link PixelCopy} API is recommended.
 */

Android 9.0(API 28)已经将所有操作DrawingCache的方法标识为Deprecated,不推荐使用。

因此,我们采用第二种方式,通过View#draw(Canvas)实现截图。

正如注释中所描述的,采用软件渲染的方式,在处理阴影和裁剪时会遇到问题。

开发过程也确实遇到问题,View#draw(Canvas)会丢失elevation及translationZ方式渲染的阴影。

2. 保存Bitmap到文件

调用Bitmap#compress(CompressFormat format, int quality, OutputStream stream)即可保存到文件。

这里对format及quality两个参数有些取舍。

public enum CompressFormat {
   
   
    JPEG    (0),
    PNG     (1),
    WEBP    (2);

    CompressFormat(int nativeInt) {
   
   
        this.nativeInt = nativeInt;
    }
    final int nativeInt;
}
/** 
 * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
 *                 small size, 100 meaning compress for max quality. Some
 *                 formats, like PNG which is lossless, will ignore the
 *                 quality setting
 */

选择JPEG格式,还是PNG格式呢?

选择JPEG格式,quality在[0, 100]之间设置多大的数值合适呢?

考虑到图片用于分享,因此选择JPEG格式,同时quality设置为50。

3. 更新相册图库

理论上,可以把文件保存到任何一个位置,但是?

在微信发送到朋友圈的时候,遇到找不到图片,无法发送的问题。

把图片保存到系统图库目录,并更新相册图库,问题完美解决。

MediaStore为我们提供了一个很好的示例。

public static final String insertImage(ContentResolver cr, Bitmap source,
                                       String title, String description) {
   
   
    ContentValues values = new ContentValues();
    values.put(Images.Media.TITLE, title);
    values.put(Images.Media.DESCRIPTION, description);
    values.put(Images.Media.MIME_TYPE, "image/jpeg");

    Uri url = null;
    String stringUrl = null;    /* value to be returned */

    try {
   
   
        url = cr.insert(EXTERNAL_CONTENT_URI, values);

        if (source != null) {
   
   
            OutputStream imageOut = cr.openOutputStream(url);
            try {
   
   
                source.compress(Bitmap.CompressFormat.JPEG, 50, imageOut)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值