赞
踩
对笔记进行截图,并保存到相册。
实现第1栏第1个图标”图片“功能,对View进行截图,并保存到图库。
神马笔记最新版本:【神马笔记 版本1.2.0.apk】
图片方式是最能保持原有排版的一种方式,能保证完全一致的阅读体验。并且还具有一定防止修改的能力。
因此,神马笔记非常推荐以图片的方式进行分享。
实现整个功能分成3个步骤:
对View进行截图,有2种方式。
@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方式渲染的阴影。
调用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。
理论上,可以把文件保存到任何一个位置,但是?
在微信发送到朋友圈的时候,遇到找不到图片,无法发送的问题。
把图片保存到系统图库目录,并更新相册图库,问题完美解决。
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。