getdrawingcache 废弃_解析View的getDrawingCache方法

本文详细解析了Android中View的getDrawingCache方法,用于将View内容转化为Bitmap。首先介绍了getDrawingCache的基本用法,然后深入探讨了其内部实现,包括buildDrawingCache方法的调用和Bitmap的生成过程。最后提到了自定义View时可能出现的问题,并给出了解决方案,即自定义getBitmap方法。
摘要由CSDN通过智能技术生成

1. View 的getDrawingCache方法

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

2. View的getDrawingCache的具体实现

查看View的getDrawingCache()方法

/***

Calling this method is equivalent to calling getDrawingCache(false).

*

*@returnA non-scaled bitmap representing this view or null if cache is disabled.

*

*@see#getDrawingCache(boolean)*/

publicBitmap getDrawingCache() {return getDrawingCache(false);

}

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

/***

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.

*

*

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.

*

*@paramautoScale Indicates whether the generated bitmap should be scaled based on

* the current density of the screen when the application is in compatibility

* mode.

*

*@returnA bitmap representing this view or null if cache is disabled.

*

*@see#setDrawingCacheEnabled(boolean)

*@see#isDrawingCacheEnabled()

*@see#buildDrawingCache(boolean)

*@see#destroyDrawingCache()*/

public Bitmap getDrawingCache(booleanautoScale) {if ((mViewFlags & WILL_NOT_CACHE_DRAWING) ==WILL_NOT_CACHE_DRAWING) {return null;

}if ((mViewFlags & DRAWI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值