View setDrawingCacheEnabled 缓存

本文介绍如何使用setDrawingCacheEnabled方法减少Android应用中视图缩放时的重绘次数,以改善用户体验。该方法通过启用绘制缓存来避免每次缩放时重新绘制视图,从而提高缩放操作的流畅性。

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

项目中用到了缩放,但是没一点缩放就会重绘,导致放大过程非常的卡。老大说View有一个setDrawingCacheEnabled方法可以防止卡顿,listview就是这么处理的。以下:


setDrawingCacheEnabled

Added in  API level 1
void setDrawingCacheEnabled (boolean enabled)

Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling 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 getDrawingCache() and draw it on screen if the returned bitmap is not null.

Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers.

This API can be used to manually generate a bitmap copy of this view, by setting the flag to true and calling getDrawingCache().

Parameters
enabled boolean: true to enable the drawing cache, false otherwise
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值