自定义图片,实现透明度动态变化

简介: The Use Case In Fragment, there are a couple of places where we use horizontal scrollers as a selection view.

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);  canvas.restore();  }  }   { // Draw the selected 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.RIGHT : Gravity.LEFT;  Gravity.apply(gravity, w, h, bounds, r);   if (w > 0 && h > 0) {  canvas.save();  canvas.clipRect(r);  mRevealState.mSelectedDrawable.draw(canvas);  canvas.restore();  }  }  } } 

With that, we can simply set the level of the icon based on scroll position and away we go.

1
2
3
4
5
6 
float offset = getOffestForPosition(recyclerView, position); if (Math.abs(offset) <= 1f) {  holder.image.setImageLevel((int) (offset * 5000) + 5000); } else {  holder.image.setImageLevel(0); } 

If you’d like to see the source for this custom drawable, you can find the Gist on Github here

源码: https://gist.github.com/rharter/34051da57f8a6a0991ff

博客地址:http://ryanharter.com/blog/2015/04/03/custom-drawables/

 

目录
相关文章
|
4月前
|
前端开发 算法 网络安全
图片滑块验证功能很难吗?做个可以自己抠形状的图片滑块验证组件
图片滑块验证功能很难吗?做个可以自己抠形状的图片滑块验证组件
123 0
4.为模型增加贴图
4.为模型增加贴图
317 0
|
2月前
|
容器
软件开发常见流程之物理像素导致图片变形问题如何解决,先把图缩放为原先的两倍,再缩放,利用Cutterman生成矢量图
软件开发常见流程之物理像素导致图片变形问题如何解决,先把图缩放为原先的两倍,再缩放,利用Cutterman生成矢量图
|
4月前
表格高度根据内容自适应的瀑布流
表格高度根据内容自适应的瀑布流
41 1
|
前端开发 容器
Bootstrap4(一)重点----网格系统,图像形状,轮播,多媒体对象,滚动监听
Bootstrap 提供了一套响应式、移动设备优先的流式网格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多 12 列。
177 0
Bootstrap4(一)重点----网格系统,图像形状,轮播,多媒体对象,滚动监听
SwiftUI—使用图像视图强大的色彩调整功能
SwiftUI—使用图像视图强大的色彩调整功能
688 0
SwiftUI—使用图像视图强大的色彩调整功能
UIImage如何修改模糊度和透明度
最近一位朋友问我如何根据alpha(透明度)和blur(模糊度)值任意修改图片效果,之后我查了一些资料,顺便写了一下Demo。
UIImage如何修改模糊度和透明度
图像边缘颜色更改
图像边缘颜色更改
157 0
图像边缘颜色更改
|
API 定位技术 UED
百度地图API二:根据标注点坐标范围计算显示缩放级别zoom自适应显示地图
原文:百度地图API二:根据标注点坐标范围计算显示缩放级别zoom自适应显示地图 版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/liusaint1992/article/details/50071613 上一文章讲了如何给地图添加点击事件做出不同的反应。
5144 0