博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Drawable Mutations(Drawable 变异)--摘自--android.doc.resources.articles
阅读量:5823 次
发布时间:2019-06-18

本文共 1963 字,大约阅读时间需要 6 分钟。

Drawable is a pluggable drawing container that is usually associated with a View. For instance, a  is used to display images, a  to draw shapes and gradients, and so on. You can even combine them to create complex renderings.

As a matter of fact, they are so convenient that most of the default Android apps and widgets are built using drawables; there are about 700 drawables used in the core Android framework. Because drawables are used so extensively throughout the system, Android optimizes them when they are loaded from resources. For instance, every time you create a , a new drawable is loaded from the framework resources (android.R.drawable.btn_default). This means all buttons across all the apps use a different drawable instance as their background. However, all these drawables share a common state, called the "constant state." The content of this state varies according to the type of drawable you are using, but it usually contains all the properties that can be defined by a resource. In the case of a button, the constant state contains a bitmap image. This way, all buttons across all applications share the same bitmap, which saves a lot of memory.

two drawables are created but they both share the same constant state, hence the same bitmap

This state sharing feature is great to avoid wasting memory but it can cause problems when you try to modify the properties of a drawable. 

使用下面代码  所有的Drawable的透明度会改变 

Book book = ...;TextView listItem = ...;listItem.setText(book.getTitle());Drawable star = context.getResources().getDrawable(R.drawable.star);if (book.isFavorite()) {  star.setAlpha(255); // opaque} else {  star.setAlpha(70); // translucent}

android 1.5以后可使用 mutate()解决,如下代码:

Drawable star = context.getResources().getDrawable(R.drawable.star);if (book.isFavorite()) {  star.mutate().setAlpha(255); // opaque} else {  star. mutate().setAlpha(70); // translucent}

  

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/maneater/archive/2012/04/19/2457021.html

你可能感兴趣的文章
【ros】Create a ROS package:package dependencies报错
查看>>
通过容器编排和服务网格来改进Java微服务的可测性
查看>>
re:Invent解读:没想到你是这样的AWS
查看>>
PyTips 0x02 - Python 中的函数式编程
查看>>
使用《Deep Image Prior》来做图像复原
查看>>
Linux基础命令---rmdir
查看>>
Android图片添加水印图片并把图片保存到文件存储
查看>>
BigDecimal 舍入模式(Rounding mode)介绍
查看>>
开源 免费 java CMS - FreeCMS1.2-标签 infoSign
查看>>
Squid 反向代理服务器配置
查看>>
Java I/O操作
查看>>
Tomcat性能调优
查看>>
Android自学--一篇文章基本掌握所有的常用View组件
查看>>
灰度图像和彩色图像
查看>>
FreeMarker-Built-ins for strings
查看>>
argparse - 命令行选项与参数解析(转)
查看>>
修改上一篇文章的node.js代码,支持默认页及支持中文
查看>>
spring-boot支持websocket
查看>>
菜鸟笔记(一) - Java常见的乱码问题
查看>>
我理想中的前端工作流
查看>>