/** * Created by xiasuhuei321 on 2017/9/5. * author:luo * e-mail:xiasuhuei321@163.com * * desc: All shape's parent class.It describes a shape will have * what feature.It's draw flows are: * 1.Outside the class init some value such as the start and the * end point. * 2.Invoke draw(Canvas) method, in this method, there are still * two flows: * 1) Get random value to init paint, this will affect the shape * draw style. * 2) When the shape is not used, invoke init method, and when it * is not used invoke drawWhenInUse(Canvas) method. It should be * override by user and to implement draw itself. * */ abstractclassWeatherShape(val start: PointF, val end: PointF) { openvar TAG = "WeatherShape"
/** * 是否是正在被使用的状态 */ var isInUse = false
/** * 是否是随机刷新的Shape */ var isRandom = false
/** * 下落的速度,特指垂直方向,子类可以实现自己水平方向的速度 */ var speed = 0.05f
/** * Empty body, this will be invoke in initStyle * method.If current initStyle method can satisfy your need * but you still add something, by override this method * will be a good idea to solve the problem. */ openfunwtc(random:Random): Unit {
}
abstractfunrandomSpeed(random: Random): Float
/** * 获取一个随机的提前量,让shape在竖屏上有一个初始的偏移 */ openfunrandomPre(): Long { val random = Random() val pre = random.nextInt(1000).toLong() return pre } }
/** * Created by xiasuhuei321 on 2017/9/7. * author:luo * e-mail:xiasuhuei321@163.com */ classWeatherShapePool{ val constantRain = ArrayList<Rain>() val randomRain = ArrayList<Rain>()
val constantSnow = ArrayList<Snow>() val randomSnow = ArrayList<Snow>()
init { // 初始化 initData() initSnow() }
privatefuninitData() { val space = context.getScreenWidth() / 20 var currentSpace = 0f // 将其均匀的分布在屏幕x方向上 for (i in0..19) { val rain = Rain(PointF(currentSpace, 0f), PointF(currentSpace, 0f)) rain.originLength = 20f rain.originX = currentSpace constantRain.add(rain) currentSpace += space }
for (j in0..9) { val rain = Rain(PointF(0f, 0f), PointF(0f, 0f)) rain.isRandom = true rain.originLength = 20f randomRain.add(rain) } }
fundrawRain(canvas: Canvas) { for (r in constantRain) { r.draw(canvas) } for (r in randomRain) { r.draw(canvas) } }
privatefuninitSnow(){ val space = context.getScreenWidth() / 20 var currentSpace = 0f // 将其均匀的分布在屏幕x方向上 for (i in0..19) { val snow = Snow(PointF(currentSpace, 0f), PointF(currentSpace, 0f)) snow.originX = currentSpace snow.radius = 20f constantSnow.add(snow) currentSpace += space }
for (j in0..19) { val snow = Snow(PointF(0f, 0f), PointF(0f, 0f)) snow.isRandom = true snow.radius = 20f randomSnow.add(snow) } }
fundrawSnow(canvas: Canvas){ for(r in constantSnow){ r.draw(canvas) }