博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义图片,实现透明度动态变化
阅读量:5964 次
发布时间:2019-06-19

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

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).

123456 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, .

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.

123456 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
@Overridepublic 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.

转载地址:http://dovax.baihongyu.com/

你可能感兴趣的文章
Android 类库
查看>>
vue和iview应用中的一些问题(持续更新)
查看>>
基于 Workman 实现Web扫描登录
查看>>
Vagrant+PHPStorm+Google+XDebug断点调试
查看>>
karma如何与测试框架合作2之webpack
查看>>
关于VSCode更新对于emmet2.0支持的配置更改问题。
查看>>
二叉树的遍历
查看>>
三元组相加获得target
查看>>
Javascript设计模式之——代理模式
查看>>
天赋是牛人的基因?
查看>>
SaaS 公司如何应对 On-Call 挑战?
查看>>
HttpClient4.3.x的连接管理
查看>>
HTML中frame 框架详解
查看>>
Docker结合Jenkins的持续构建实践
查看>>
[分享] Flask 网络开发经典书籍: Flask Web Development
查看>>
Disruptor 分析
查看>>
three.js 坐标系、camera位置属性、点、线、面
查看>>
案例篇-HBase 实战之 MOB 使用指南
查看>>
今天,你用智能音箱了吗?
查看>>
苹果推出新款iPad Air和iPad mini,升级A12处理器
查看>>