The Use Case

In Fragment, there are a couple of places where we use horizontal scrollers as a selection view. This means that the center icon is the “selected” icon, and items should transition in and out of this state fluidly. For this we decided that a nice reveal transition would be great.

While this wasn’t entirely necessary, I felt that it was a effect that made the motion feel very fluid and added a touch of class to the app. I could have set up multiple image views and make parts of them individual, but this was the perfect place for a custom drawables.

Customizing Drawables

Drawables in Android are actually very similar to Views. They have similar methods for things like padding and bounds (layout), and have a draw method that can be overridden. In my case, I needed to be able to transition between two drawables, a selected drawable and an unselected drawable, based on a value.

In our case, we simply create a subclass of Drawable that contains other Drawables (and an orientation).

1
2
3
4
5
6 7 8 9 
public class RevealDrawable extends Drawable {  public RevealDrawable(Drawable unselected, Drawable selected, int orientation) {  this(null, null);   mUnselectedDrawable = unselected;  mSelectedDrawable = selected;  mOrientation = orientation;  } } 

Next we need to be able to set the value identifying where the drawable is in the selection process. Fortunately Drawable has a facility for this type of thing built in, setLevel(int).

A Drawable’s level is an integer between 0 and 10,000 which simply allows the Drawable to customize it’s view based on a value. In our case, we can simply define 5,000 as the selected state, 0 and entirely unselected to the left, and 10,000 as entirely unselected to the right.

All we need to do now is to override the draw(Canvas canvas) method to draw the appropriate drawable by clipping the canvas based on the current level.

1
2
3
4
5
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 
@Override
public void draw(Canvas canvas) {   // If level == 10000 || level == 0, just draw the unselected image  int level = getLevel();  if (level == 10000 || level == 0) {  mRevealState.mUnselectedDrawable.draw(canvas);  }   // If level == 5000 just draw the selected image  else if (level == 5000) {  mRevealState.mSelectedDrawable.draw(canvas);  }   // Else, draw the transitional version  else {  final Rect r = mTmpRect;  final Rect bounds = getBounds();   { // Draw the unselected portion  float value = (level / 5000f) - 1f;  int w = bounds.width();  if ((mRevealState.mOrientation & HORIZONTAL) != 0) {  w = (int) (w * Math.abs(value));  }  int h = bounds.height();  if ((mRevealState.mOrientation & VERTICAL) != 0) {  h = (int) (h * Math.abs(value));  }  int gravity = value < 0 ? Gravity.LEFT : Gravity.RIGHT;  Gravity.apply(gravity, w, h, bounds, r);   if (w > 0 && h > 0) {  canvas.save();  canvas.clipRect(r);  mRevealState.mUnselectedDrawable.draw(canvas); 

自定义图片,实现透明度动态变化相关推荐

  1. 【MATLAB教程案例44】通过matlab学习三维曲面的建模,颜色,透明度,动态变化等——以海浪曲面函数为例

    欢迎订阅<FPGA学习入门100例教程>.<MATLAB学习入门100例教程> 目录 1.软件版本 2.三维海浪曲面理论概述 3.通过MATLAB实现三维海浪曲面

  2. 自定义按钮动态变化_新闻价值的变化定义

    自定义按钮动态变化 I read Bari Weiss' resignation letter from the New York Times with some perplexity. In par ...

  3. MFC CMFCToolBar静态工具栏和动态工具栏,加载自定义图片(真彩透明)

    现在把我最新研究的对CMFCToolBar心得写在这边. 现在介绍两种加载工具栏的方式: 一种是静态加载工具栏,跟普通的加载工具栏一样,只是用了自定义图片. 一种是动态加载工具栏,这个也是需要静态工具 ...

  4. Map创建自定义图片图层,图片会随着地图缩放而缩放,uniapp、高德、腾讯、百度

    场景:在项目开发中会遇到在map上绘制图层,一般想到的方法会是通过接口获取图层边界的所有点,由点生成polygons多边形面.polygons 多边形示例 数据过多地图会存在卡顿的问题. 目的:创建自 ...

  5. 细品RibbonX(25):使用自定义图片和库

    细品RibbonX(25):使用自定义图片和库 资料整理来自于论坛 完整版下载地址:http://download.csdn.net/download/nodeman/10264659 Loading ...

  6. 【知识星球】有没有网络模型是动态变化的,每次用的时候都不一样?

    欢迎大家来到<知识星球>专栏,这里是网络结构1000变小专题,今天给大家介绍一类网络结构,它是动态变化的,每一次使用的时候都不一样. 作者&编辑 | 言有三 1 训练时变化的网络结 ...

  7. SignalR2结合ujtopo实现拓扑图动态变化

    上一篇文章基于jTopo的拓扑图设计工具库ujtopo,介绍了拓扑设计工具,这一篇我们使用SignalR2结合ujtopo实现拓扑图的动态变化. 仅仅作为演示,之前的文章SignalR2简易数据看板演 ...

  8. 国家粮食与物资储备局揭示中国稻谷产毒真菌分布及仓储动态变化

    中国水稻主产区产毒真菌的分布 Distribution of mycotoxin-producing fungi across major rice production areas of China ...

  9. ArcGIS里表示地理信息的动态变化(例三)

    发信人: WickerPark ([淮水安澜]我已回家), 信区: GIS. 本篇人气: 25 标  题: ArcGIS里表示地理信息的动态变化(例三) 发信站: 南京大学小百合站 (Sat Aug ...

最新文章

  1. Html画布w3c,HTML canvas 标签
  2. WPF学习12:基于MVVM Light 制作图形编辑工具(3)
  3. 读入的字节都写入字节数组中_使用Java将文件读入字节数组的7个示例
  4. 启航考研计算机课程,计算机考研专业课如何备考
  5. 【CVPR2019】 教程 Tutorials List
  6. pku2750 Potted Flower
  7. 线段树(SegmentTree)学习笔记
  8. SVN 回退到某一个版本
  9. vsr matlab仿真,电压型PWM整流器(VSR)及控制系统的matlab仿真..docx
  10. 软件工程--软件详细设计说明书(免费小说网站)
  11. 【每日一练:逻辑题】使用一个天平找8个球中其中一个重量不一致的球
  12. 计算机双硬盘怎么启动第二块硬盘,电脑装两个硬盘怎么设置主从盘_双硬盘设置主盘的方法...
  13. Kotlin和Swift语言在Redmonk榜上排名大幅提升
  14. 这么选家用投影仪,再也不交智商税
  15. alert意为:警告、警报。
  16. 2022年全球及中国工程软件 (CAD、CAM、 CAE、AECEDA)行业头部企业市场占有率及排名调研报告
  17. aws的eks平滑删除work节点实现降配
  18. Linux 文件系统类型 文件系统结构 与Windows文件系统的比较
  19. E576: viminfo: 缺少 ‘>‘ 位于行:
  20. 气象雷达地物杂波产生的原因

热门文章

  1. Java Signal实例
  2. UVA227-Puzzle
  3. UI复习练习_优酷布局
  4. 最牛营业部——国信泰然九路揭秘
  5. Linq to SQL只支持SQL Server(所选对象使用不支持的数据提供程序)
  6. Android开源项目整理:个性化空间View篇(看遍论坛千万篇,不看此篇也枉然)
  7. 得到src目录下的properties文件属性
  8. c语言里的宏(翻译)4
  9. scrapy爬取京东
  10. python入门学习:4.if语句