当前位置:   article > 正文

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

android保存view图片

一、目标

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

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);         
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
/**
 * @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.  
 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
/**
 * 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.
 */
  • 1
  • 2
  • 3
  • 4
  • 5
/**
 * 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.
 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
/** 
 * @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
 */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

选择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 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/262008?site
推荐阅读
相关标签
  

闽ICP备14008679号