Best Practices for Using Alpha
Alpha是图形界面开发中常用的特效,通常我们会使用以下代码来实现Alpha特效:
view.setAlpha(0.5f);
View.ALPHA.set(view, 0.5f);
ObjectAnimator.ofFloat(view, "alpha", 0.5f).start();
view.animate().alpha(0.5f).start();
view.setAnimation(new AlphaAnimation(1.0f, 0.5f));
其效果都等同于:
canvas.saveLayer(l, r, t, b, 127, Canvas.CLIP_TO_LAYER_SAVE_FLAG);
所以常见的alpha特效是通过将图像绘制到offscreen buffer中然后显示出来,这样的操作是非常消耗资源的,甚至可能导致性能问题,在开发过程中我们可以通过其他方式避免创建offsreen buffer。
TextView
对于TextView我们通常需要文字透明效果,而不是View本身透明,所以,直接设置带有alpha值的TextColor是比较高效的方式。
// Not this
textView.setAlpha(alpha);
// 以下方式可以避免创建 offscreen buffer
int newTextColor = (int) (0xFF * alpha) << 24 | baseTextColor & 0xFFFFFF;
textView.setTextColor(newTextColor);
ImageView
同样的对于只具有src image的ImageView,直接调用setImageAlpha()方法更为合理。
// Not this, setAlpha方法由View继承而来,性能不佳
imageView.setAlpha(0.5f);
// 使用以下方式时,ImageView会在绘制图片时单独为图片指定Alpha
// 可以避免创建 offScreenBuffer
imageView.setImageAlpha((int) alpha * 255);
CustomView
类似的,自定义控件时,应该直接去设置paint的alpha。
// Not this
customView.setAlpha(alpha);
// But this
paint.setAlpha((int) alpha * 255);
canvas.draw*(..., paint);
同时Android提供了hasOverlappingRendering()接口,通过重写该接口可以告知系统当前View是否存在内容重叠的情况,帮助系统优化绘制流程,原理是这样的:对于有重叠内容的View,系统简单粗暴的使用 offscreen buffer来协助处理。当告知系统该View无重叠内容时,系统会分别使用合适的alpha值绘制每一层。
/**
* Returns whether this View has content which overlaps. This function, intended to be
* overridden by specific View types, is an optimization when alpha is set on a view. If
* rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer
* and then composited it into place, which can be expensive. If the view has no overlapping
* rendering, the view can draw each primitive with the appropriate alpha value directly.
* An example of overlapping rendering is a TextView with a background image, such as a
* Button. An example of non-overlapping rendering is a TextView with no background, or
* an ImageView with only the foreground image. The default implementation returns true;
* subclasses should override if they have cases which can be optimized.
*
* @return true if the content in this view might overlap, false otherwise.
*/
public boolean hasOverlappingRendering() {
return true;
}
最后引用Chet Haase的一句话作为总结
“You know what your view is doing, so do the right thing for your situation.”
Via Android Tips: Best Practices for Using Alpha
Best Practices for Using Alpha的更多相关文章
- 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting
一.序言 陆陆续续的如果累计起来,我估计至少有二十来位左右的朋友加我QQ,向我咨询有关抠图方面的算法,可惜的是,我对这方面之前一直是没有研究过的.除了利用和Photoshop中的魔棒一样的技术或者 ...
- 第六次团队作业——Alpha冲刺之事后诸葛亮
Deadline:2016-11-24 22:00pm Alpha冲刺,很多同学经历了"Learning by doing"的学一门新的编程语言.学Git.学做一个完整的项目.但是 ...
- Alpha阶段总结
Alpha阶段的验收已经完成,8个小组都展现了他们经过连夜奋战后的成果.相比过往几届,这是第一次8个小组全部顺利演示操作完成,没有个别小组因为任务未完成而延宕演示的情况发生.Alpha演示,各组都实现 ...
- 第五次团队作业——第一次项目冲刺——Alpha版本
Deadline:2016-11-19 8:00am 本次团队作业将持续三周时间,完成项目Alpha版本,在2016.11.19 的实践课上进行演示操作. 阅读或再次阅读<构建之法> ...
- 2016福州大学软件工程第五、六次团队作业-Alpha阶段成绩汇总
1.本次作业成绩统计结果: 本次Alpha阶段团队作业公布如下: 表格说明: PE:贡献百分比 YS:演示评分(满分15分) BK:博客评分(满分15分) SH:事后诸葛亮环节(满分5分) P:个人分 ...
- Ubuntu 16.04 安装 Kodi v17 “Krypton” Alpha 2
Ubuntu 16.04 安装 Kodi v17 “Krypton” Alpha 2:sudo add-apt-repository ppa:team-xbmc/xbmc-nightlysudo ap ...
- Alpha版总结会议
昨天上课的时候,我们学习了项目总结这一部分的内容,并根据老师提供的项目Postmortem模板对我们的项目进行了总结. 项目Postmortem模板主要分为设想和目标.计划.资源.变更管理.设计和实现 ...
- 博客整理——Alpha版冲刺
Alpha冲刺 助教链接:2016福州大学软件工程第五.六次团队作业-Alpha阶段成绩汇总 1.Transcend Daily Scrum Meeting --FirstDay Daily Scru ...
- Some practices to write better C#/.NET code(译)
C#(.NET)中有关编码的一些建议,原文地址:http://www.codeproject.com/Articles/539179/Some-practices-to-write-better-Cs ...
随机推荐
- 重命名计算机名称导致TFS版本管理下的工作区问题的修复
1.问题 若在本地已有工作区之后,此时修改计算机名称重启生效后,打开本地的项目解决方案,输出窗口会提示如下图: 2.解决 输入命令:tf workspaces /collection:http://1 ...
- java基础语法笔记
这段时间看了一些java,急了一些笔记,记下一遍以后复习用! 2016-07-24 15:12:40 java很多语法都跟C#类似,下面列举一些不同的地方******注意***** java中的系统方 ...
- 多个线程访问url
多个线程访问url package com.aig.ecompassios.ecard; import java.io.BufferedReader; import java.io.InputStre ...
- sql中truncate 、delete与drop区别
SQL truncate .delete与drop区别 相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL ...
- scrapy shell 中文网站输出报错.记录.
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 381-382: illegal multibyte sequence 上 ...
- 04_过滤器Filter_04_Filter生命周期
[Filter生命周期] [init(FilterConfig filterConfig) throws ServletException] *和Servlet程序类似,Filter的创建和销毁由we ...
- 15_AOP入门准备_静态代理模式
[工程截图] [PersonDao.java] package com.HigginCui.daoProxy; public interface PersonDao { public void sav ...
- struts2初印象
第一次写这么正式的文章,如果写的不好的地方,请指出. 今天玩了一下struts2,不过貌似是我被他玩了.简要笔记如下: 一.配置struts2(在eclipse Helios版本下) (1)先创建一个 ...
- 设计模式之开篇(C#语法)
很长时间没有写文章了,前段时间写的C#语法糖分享得到有很多朋友支持,这个也使得我有信心继续分享下去,在这里非常感谢大家!这次开始给大家分享一下设计模式,我个人觉得设计模式也对于我们提高个人技术水平来说 ...
- ubuntu 12.04 mysql转移目录后 无法 启动
http://www.boyunjian.com/do/article/snapshot.do?uid=com.iteye.xgbjmxn%2Fblog%2F1208086(转,) 我是用ap ...