android自定义view粒子效果之雨-续
2015-05-14 16:11
162 查看
上次描述的主要是雨点的绘制和运动,因为绘制可以自己控制了,逻辑也可以自己进行改变,所以还有很多事情可以做。 首先可以改变雨点的颜色为自己想要的颜色,还可以让每个雨点的颜色都随机,更可以对画面进行剪裁而出现类似电视上一圆形从小到大显示里面的画面。 听起来好像很复杂,其实都是用固定的几步,也就是固定自定义View的模式,写过一次后,直接就可以套用了。 废话少说,上次主要是雨点的逻辑,那么雨点的数量怎么控制或设置,其实很简单的代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:effect="http://schemas.android.com/apk/res/com.lxf.particle" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff000000" android:orientation="vertical" > <com.lxf.particle.widget.RainAnimation android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" effect:itemNum="100" /> ...
设置数量只需要 effect:itemNum="100",当然effect这个是要在根视图中声明下:xmlns:effect="http://schemas.android.com/apk/res/com.lxf.particle",至于想要什么名字替换effect都可以,如改为particle如下: xmlns:particle="http://schemas.android.com/apk/res/com.lxf.particle" 然后设置数量:particle:itemNum="100" 那么现在有疑问了,声明这个particle,怎么就可以使用itemNum这个属性了呢,很简单,在res目录下的values下的attrs.xml中定义如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="EffectAnimation"> <attr name="itemNum" format="integer" /> <attr name="itemColor" format="color" /> <attr name="randColor" format="boolean"/> <attr name="clipScale" format="boolean"/> </declare-styleable> </resources>
可以看到,我在里面声明了4个属性,其实就相当于代码里面声明了一个变量,如<attr name="itemNum" format="integer" />就相当于int itemNum;只不过这个是在xml中声明的,在xml中使用。 现在为止,思路是在xml中声明了设置数量的变量,然后在布局文件中使用,但是,系统不会帮你做任何事情,数量怎么控制,还需要你在代码里面实现,那么看代码:
public EffectAnimation(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EffectAnimation); itemNum = a.getInt(R.styleable.EffectAnimation_itemNum, 0); itemColor = a.getColor(R.styleable.EffectAnimation_itemColor, 0xffffffff); randColor = a.getBoolean(R.styleable.EffectAnimation_randColor, false); clip_scale = a.getBoolean(R.styleable.EffectAnimation_clipScale, false); a.recycle(); setLayerType(View.LAYER_TYPE_SOFTWARE, null); }
可以看到itemNum是通过解析xml然后获取的
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EffectAnimation); itemNum = a.getInt(R.styleable.EffectAnimation_itemNum, 0);
其他自定义的xml属性也可以按这个步骤来解析(最后记得要a.recycle();) 如果看了上一篇粒子效果之雨的话,到这里就应该很清楚itemNum的作用了,因为一个雨点的逻辑和绘制有了,只需要设置列表的大小就可以了,那么这个itemNum就是这个列表的大小,或者遍历加入列表itemNum数量的RainPoint的对象就可以了,这样,通过xml来设置雨点的数量的过程就完成了。 回顾下过程, 1.首先在res下values下的xml文件中声明itemNum属性。 2.然后在布局中使用这个itemNum(在布局根部要定义如xmlns:particle="http://schemas.android.com/apk/res/com.lxf.particle"),然后以particle:itemNum="100"的方式使用。 3.在代码中解析itemNum。 4.在代码中使用itemNum加入到相应的逻辑当中。 根据上面的步骤itemNum,itemColor,randColor,clip_scale就可以解析到值了,如果xml中使用了,就不会使用默认值。 itemColor在RainPoint加入的列表的时候传入的构造方法中,这样,雨点的颜色就可以设置了,效果如下(设置属性effect:itemColor="#ff00aa00"): [itemColor](http://img.blog.csdn.net/20150601103319441.jpg) randColor也是在RainPoint加入的时候传入构造方法,如果设置为true则在生成雨点的时候颜色是随机的,效果如下(设置属性effect:randColor="true" ): [randColor](http://img.blog.csdn.net/20150601103417473.jpg) clip_scale这个属性的处理主要是在自定义View中进行处理的,如果设置为true,则会通过canvas.clipPath(path);来控制显示区域,效果如下(设置属性effect:clipScale="true"): [clipScale](http://img.blog.csdn.net/20150601103320446.jpg) xml中定义EffectAnimation主要是我写的粒子效果的自定义View都是继承它的,所以com.lxf.particle.widget.RainAnimation这个在布局里面使用的属性可以在EffectAnimation进行处理。 好了,自定义View的粒子之雨篇就告一段落,大家可以到gitHub上下载克隆代码:https://github.com/xianfeng99/Particle 对了,如果大家有什么好的想法关于粒子效果的,如果有思路可以一起讨论哦。
相关文章推荐
- android自定义view粒子效果之雨(not surfaceview)
- android通过自定义view实现粒子效果展示
- 最近较流行的效果 Android自定义View实现倾斜列表/图片
- Android 自定义RecyclerView 实现真正的Gallery效果
- Android自定义TextView闪烁文字的效果
- Android 自定义闪屏页广告倒计时view效果
- Android 自定义View之边缘凹凸的优惠券效果
- Android使用自定义View实现正弦波·波动效果
- Android 自定义View 实现QQ红点拖动删除效果
- Android自定义Viewpager指示器PagerIndicator-仿微博头条效果
- Android模仿打字机效果的自定义View实现
- Android 自定义view实现TopBar效果
- android自定义层级view,扑克牌堆叠效果,cascadeLayout
- android 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
- Android clipChildren与clipToPadding使用以及简单ViewPager自定义切换效果
- Android自定义View分享——仿网易云音乐留声机效果
- Android自定义View——实现屏幕触摸校验效果
- Android 自定义 HorizontalScrollView 实现ViewPager效果,打造再多图片(控件)也不怕 OOM
- android自定义ViewPager之——3D效果应用
- android自定义ViewPager水平滑动弹性效果