• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > 解析View的getDrawingCache方法,getdrawingcache

解析View的getDrawingCache方法,getdrawingcache

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了view.getdrawingcache,getdrawingcache,getdrawingcache null,v.getdrawingcache,recyclerview源码解析等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

解析View的getDrawingCache方法,getdrawingcache


1. View 的getDrawingCache方法

  有时候需要将某个view的内容以图片的方式保存下来,感觉就和截图差不多,可以使用View 的getDrawingCache方法,返回一个Bitmap对象。

2. View的getDrawingCache的具体实现

  查看View的getDrawingCache()方法

/** 
 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p> 
 * 
 * @return A non-scaled bitmap representing this view or null if cache is disabled. 
 * 
 * @see #getDrawingCache(boolean) 
 */ 
public Bitmap getDrawingCache() { 
    return getDrawingCache(false); 
}

  看代码继续调用了getDrawingCache(false)方法,继续查看getDrawingCache(false)方法。

/** 
 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap 
 * is null when caching is disabled. If caching is enabled and the cache is not ready, 
 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not 
 * draw from the cache when the cache is enabled. To benefit from the cache, you must 
 * request the drawing cache by calling this method and draw it on screen if the 
 * returned bitmap is not null.</p> 
 * 
 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled, 
 * this method will create a bitmap of the same size as this view. Because this bitmap 
 * will be drawn scaled by the parent ViewGroup, the result on screen might show 
 * scaling artifacts. To avoid such artifacts, you should call this method by setting 
 * the auto scaling to true. Doing so, however, will generate a bitmap of a different 
 * size than the view. This implies that your application must be able to handle this 
 * size.</p> 
 * 
 * @param autoScale Indicates whether the generated bitmap should be scaled based on 
 *        the current density of the screen when the application is in compatibility 
 *        mode. 
 * 
 * @return A bitmap representing this view or null if cache is disabled. 
 * 
 * @see #setDrawingCacheEnabled(boolean) 
 * @see #isDrawingCacheEnabled() 
 * @see #buildDrawingCache(boolean) 
 * @see #destroyDrawingCache() 
 */ 
public Bitmap getDrawingCache(boolean autoScale) { 
    if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) { 
        return null; 
    } 
    if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) { 
        buildDrawingCache(autoScale); 
    } 
    return autoScale ? mDrawingCache : mUnscaledDrawingCache; 
}

  查看getDrawingCache(false)方法,如果该视图的标志是WILL_NOT_CACHE_DEAWING(表示该view没有任何绘图缓存)则直接返回null,如果视图的标志是DRWING_CACHE_ENABLED(表示该view将自己的绘图缓存成一个bitmap),则调用buildDrawingCache(autoScale)方法。

  因为传递过来的autoScale为false,则返回的Bitmap是mUnscaledDrawingCache。

  查看buildDrawingCache(autoScale)方法:

/** 
 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p> 
 * 
 * <p>If you call {@link #buildDrawingCache()} manually without calling 
 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you 
 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p> 
 * 
 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled, 
 * this method will create a bitmap of the same size as this view. Because this bitmap 
 * will be drawn scaled by the parent ViewGroup, the result on screen might show 
 * scaling artifacts. To avoid such artifacts, you should call this method by setting 
 * the auto scaling to true. Doing so, however, will generate a bitmap of a different 
 * size than the view. This implies that your application must be able to handle this 
 * size.</p> 
 * 
 * <p>You should avoid calling this method when hardware acceleration is enabled. If 
 * you do not need the drawing cache bitmap, calling this method will increase memory 
 * usage and cause the view to be rendered in software once, thus negatively impacting 
 * performance.</p> 
 * 
 * @see #getDrawingCache() 
 * @see #destroyDrawingCache() 
 */ 
public void buildDrawingCache(boolean autoScale) { 
    if ((mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == 0 || (autoScale ? 
            mDrawingCache == null : mUnscaledDrawingCache == null)) { 
        if (Trace.isTagEnabled(Trace.TRACE_TAG_VIEW)) { 
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, 
                    "buildDrawingCache/SW Layer for " + getClass().getSimpleName()); 
        } 
        try { 
            buildDrawingCacheImpl(autoScale); 
        } finally { 
            Trace.traceEnd(Trace.TRACE_TAG_VIEW); 
        } 
    } 
}

  如果mPrivateFlags与PFLAG_DRAWING_CACHE_VALID与运算为0,或者mUnscaledDrawingCache为null,则调用buildDrawingCacheImpl(autoScale)方法。

查看buildDrawingCacheImpl(autoScale)方法,

/** 
 * private, internal implementation of buildDrawingCache, used to enable tracing 
 */

 

private void buildDrawingCacheImpl(boolean autoScale) {
    mCachingFailed = false;

    int width = mRight - mLeft;
    int height = mBottom - mTop;

    final AttachInfo attachInfo = mAttachInfo;
    final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;

    if (autoScale && scalingRequired) {
        width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
        height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
    }

    final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
    final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
    final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;

    final long projectedBitmapSize = width * height * (opaque && !use32BitCache ? 2 : 4);
    final long drawingCacheSize =
            ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize();
    if (width <= 0 || height <= 0 || projectedBitmapSize > drawingCacheSize) {
        if (width > 0 && height > 0) {
            Log.w(VIEW_LOG_TAG, getClass().getSimpleName() + " not displayed because it is"
                    + " too large to fit into a software layer (or drawing cache), needs "
                    + projectedBitmapSize + " bytes, only "
            



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 解析View的getDrawingCache方法,getdrawingcache
  • getDrawingCache()和Android中的截图方法简介

相关文章

  • 2017-05-26《第一行代码》目录
  • 2017-05-26android布局不带参数返回,android布局参数
  • 2017-05-26Android pulltorefresh使用,androidpulltorefresh
  • 2017-05-26Android之SQLite数据库篇,androidsqlite
  • 2017-05-26Android ExpandableListView相关介绍
  • 2017-05-26安卓图片加载之使用universalimageloader加载圆形圆角图片
  • 2017-05-26Android中SimpleAdapter的使用—自定义列表,自定义simpleadapter
  • 2017-05-26Android UI:ListView,androiduilistview
  • 2017-05-26linux syslog日志服务器的搭建
  • 2017-05-26Android之利用HTTP网络通信实现与PHP的交互(三),android网络通信

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android Studio 快捷键
    • android的消息机制
    • 我的android学习经历9,android学习经历9
    • android6.0 adbd深入分析(四)adbd usb线拔掉再连接的过程
    • Android——eclipse下运行android项目报错 Conversion to Dalvik format failed with error 1解决,androiddalvik
    • Android--Dialog对话框
    • 编译android源码3---ubuntu安装jdk6
    • 粗暴的解释Android与蓝牙Ble之间的通信,android蓝牙ble
    • Android线程优先级设置方法技巧,android线程优先级
    • Android特效专辑(九)——仿微信雷达搜索好友特效,逻辑清晰实现简单

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有