Android:自定义滚动边缘(EdgeEffect)效果
Android可滚动控件(GridView、ListView、ScrollView等)当用户滚动到头的时候会有个边缘反馈效果,在4.0上默认为Holo蓝色效果。 如果您的App自定义了主题颜色,比如Google Play Music的橘黄色。 那么在滚动内容控件还是用默认的Holo蓝色边缘效果看起来可能不太协调。这个时候就需要自定义边缘效果了。【科学上网软件点击下载(能上youtube、facebook,享受google服务)】
边缘效果在Android系统中是通过EdgeEffect类来实现的,在该类的的构造函数中使用两个系统图片来绘制边缘效果:
1
2
3
|
final Resources res = context.getResources();
mEdge = res.getDrawable(R.drawable.overscroll_edge);
mGlow = res.getDrawable(R.drawable.overscroll_glow);
|
4.0默认的这两个图片如下(点击链接查看图片):
- overscroll_edge:overscroll_edge.png
- overscroll_glow:overscroll_glow.png
所以要实现自定义边缘效果,只需要hack系统在获取这两个图片的时候使用您App提供的图片即可。
Android系统的App是通过ContextWrapper类来获取Resources类,然后通过Resources类来获取各种资源。所以通过自定义这两个类并把自定义的类应用到这些滚动控件中即可。
首先自定义Resources类,在该类中如果判断需要获取上面这两个边缘效果图片,就返回自定义的图片(ResourcesEdgeEffect.java):
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
|
public class ResourcesEdgeEffect extends Resources {
private int overscroll_edge = getPlatformDrawableId( "overscroll_edge" );
private int overscroll_glow = getPlatformDrawableId( "overscroll_glow" );
public ResourcesEdgeEffect(AssetManager assets, DisplayMetrics metrics, Configuration config) {
super (assets, metrics, config);
}
private int getPlatformDrawableId(String name) {
try {
int i = ((Integer) Class.forName( "com.android.internal.R$drawable" ).getField(name).get( null )).intValue();
return i;
} catch (ClassNotFoundException e) {
Log.e( "[ContextWrapperEdgeEffect].getPlatformDrawableId()" , "Cannot find internal resource class" );
return 0 ;
} catch (NoSuchFieldException e1) {
Log.e( "[ContextWrapperEdgeEffect].getPlatformDrawableId()" , "Internal resource id does not exist: " + name);
return 0 ;
} catch (IllegalArgumentException e2) {
Log.e( "[ContextWrapperEdgeEffect].getPlatformDrawableId()" , "Cannot access internal resource id: " + name);
return 0 ;
} catch (IllegalAccessException e3) {
Log.e( "[ContextWrapperEdgeEffect].getPlatformDrawableId()" , "Cannot access internal resource id: " + name);
}
return 0 ;
}
public Drawable getDrawable( int resId) throws Resources.NotFoundException {
if (resId == this .overscroll_edge)
return ContextWrapperEdgeEffect. this .getBaseContext().getResources().getDrawable(R.drawable.overscroll_edge);
if (resId == this .overscroll_glow)
return ContextWrapperEdgeEffect. this .getBaseContext().getResources().getDrawable(R.drawable.overscroll_glow);
return super .getDrawable(resId);
}
}
|
然后自定义一个ContextWrapper类(ContextWrapperEdgeEffect.java):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class ContextWrapperEdgeEffect extends ContextWrapper {
private static ResourcesEdgeEffect RES_EDGE_EFFECT;
public ContextWrapperEdgeEffect(Context context) {
super (context);
Resources resources = context.getResources();
if (RES_EDGE_EFFECT == null )
RES_EDGE_EFFECT = new ResourcesEdgeEffect(resources.getAssets(), resources.getDisplayMetrics(), resources.getConfiguration());
}
//返回自定义的Resources
public Resources getResources() {
return RES_EDGE_EFFECT;
}
}
|
最后再自定义App中使用到的滚动控件,把Context对象替换为前面自定义的ContextWrapperEdgeEffect类即可(如下是GridView的示例):
1
2
3
4
5
6
7
8
9
10
11
|
public class GridView extends android.widget.GridView {
public GridView(Context context, AttributeSet attrs) {
super ( new ContextWrapperEdgeEffect(context), attrs);
}
public GridView(Context context, AttributeSet attrs, int defStyle) {
super ( new ContextWrapperEdgeEffect(context), attrs, defStyle);
}
}
|
然后让您的UE同学按照Holo蓝色边缘效果的两张图来提供自定义的两张图即可。
如果您感觉上面这些步骤比较繁琐的话,也可以下载EdgeEffectOverride这个项目,该项目已经实现了ListView、GridView、ScrollVeiw、ExpandableListView和ViewPager类, 下载该项目只需要替换两个图片即可。
下图是一个自定义红色效果的截图:
Read more:http://blog.chengyunfeng.com/?p=500#ixzz2bH9wX7vf
Android:自定义滚动边缘(EdgeEffect)效果相关推荐
- android中仿qq最新版抽屉,Android 自定义View实现抽屉效果
Android 自定义View实现抽屉效果 说明 这个自定义View,没有处理好多点触摸问题 View跟着手指移动,没有采用传统的scrollBy方法,而是通过不停地重新布局子View的方式,来使得子 ...
- android 循环弹幕,Android自定义View实现弹幕效果
原标题:Android自定义View实现弹幕效果 在很多视频直播中都有弹幕功能,而安卓上没有简单好用的弹幕控件,本文介绍一个自定义弹幕view的demo. 效果图: 思路: 自定义Textitem类表 ...
- Android自定义滚动选择器
Android自定义滚动选择器 实现图片的效果 代码如下 package com.linzihui.widget;import android.annotation.SuppressLint; imp ...
- Android 自定义 圆环,Android自定义view实现圆环效果实例代码
先上效果图,如果大家感觉不错,请参考实现代码. 重要的是如何实现自定义的view效果 (1)创建类,继承view,重写onDraw和onMesure方法 public class CirclePerc ...
- android波纹效果弹窗,Android自定义View实现波纹效果
Android自定义View实现波纹效果 时间:2017-05-27 来源:移动互联网学院 1.引言:随着Android智能手机的普及,Android应用得到了大力支持,而Android应用的 ...
- android下雨动画效果,Android 自定义View(二) 下雨效果
Rain.gif Android 自定义View(二) 下雨效果 一 实现思路, 雨点用线段表示,通过控制线段的大小和宽度来表示不同的线段. 一个雨点下雨的过程可以表示为一条直线,一次雨点在下雨的过程 ...
- android功能相同的view,Android自定义View实现扫描效果
本文实例为大家分享了Android自定义View实现扫描效果的具体代码,供大家参考,具体内容如下 演示效果如下: 实现内容: 1.控制动画是竖向或者横向 2.控制动画初始是从底部/左边开始,或者从上边 ...
- Android 自定义 HorizontalScrollView 横向滑动效果
自从Gallery被谷歌废弃以后,Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果.的确HorizontalScrollView可以实现Gal ...
- android 自定义加载动画效果,Android自定义View实现loading动画加载效果
项目开发中对Loading的处理是比较常见的,安卓系统提供的不太美观,引入第三发又太麻烦,这时候自己定义View来实现这个效果,并且进行封装抽取给项目提供统一的loading样式是最好的解决方式了. ...
最新文章
- MPB:上海交大肖湘组分享基于基因芯片的海洋微生物转录组学分析技术
- boost::safe_numerics::safe_signed_range相关的测试程序
- Android开发面试题之Android四大组件
- STM32 基于正电原子开发板,改换芯片为STM32F103R6,Proteus仿真的一些问题
- 详解.NET Core 依赖注入生命周期
- Box Shadow CSS教程–如何向任何HTML元素添加投影
- c++ 多重背包状态转移方程_Python|动态规划关于0-1背包问题
- 你知道海底捞在全国有多少家店吗?
- listen函数的第二个参数_signal(SIGPIPE,?SIG_IGN)listen函数中backlog参数分析
- matcaffe训练与测试
- Github多人协作
- python保存快捷键是什么_python中的快捷键
- mummer基因组共线性分析详解
- MySQL--备份还原常用命令
- android sqlite 随机,Android+Sqlite 实现内容的随机内容推送(一)
- 一份网站的商业计划书内容概要
- 3DMAX的vray自发光材质为什么渲出来是黑的?
- 中国“神威•太湖之光”蝉联世界超算冠军
- 利用Spring扩展点模拟MyBatis的注解编程「知识点多多」「扩展点实战系列」- 第448篇
- A002-181-2154