
具体实现方法:
1.baseview主要是设定雨滴要实现的动作,只是先设定,也就是抽象方法,在子类中实现其方法
2.Rainitems封装雨滴类
3.Rainitems对雨滴集合创建到面板中,显示出来,具体实现就是在这个类中
一、baseview封装类,子类继承后实现方法即可
public abstract class BaseView extends View {private control thread;public BaseView(Context context, AttributeSet attrs) {super(context, attrs);}public BaseView(Context context) {super(context);}//封装,构造画面,子类继承后需要重写protected abstract void drawsub(Canvas canvas);//封装移动方法,子类继承后需要重写protected abstract void move();//封装的初始化方法protected abstract void init();@Overrideprotected final void onDraw(Canvas canvas) {//启动线程if (thread ==null) {thread = new control();thread.start();}else {drawsub(canvas);}}public class control extends Thread{@Overridepublic void run() {init();while(true){move();//相当于刷新画布postInvalidate();try {sleep(30);} catch (InterruptedException e) {e.printStackTrace();}}}}}二、Rainitem雨点类public class RainItem {private int height;private int width;private float startX;private float startY;private float stopX;private float stopY;private float sizeX;private float sizeY;private float of = 0.5f;private Paint paint;private Random random = new Random();public RainItem(int height,int width) {this.height = height;this.width = width;init();}public void init() {//startx和y对应的分别是起止位置sizeX = 1 + random.nextInt(10);sizeY = 10 + random.nextInt(20);startX = random.nextInt(width);startY = random.nextInt(height);stopX = startX + sizeX;stopY = startY + sizeY;of = (float) (0.2 + random.nextFloat());paint = new Paint();}/** * 绘画雨滴 * @param canvas */public void draw(Canvas canvas) {paint.setARGB(255, random.nextInt(255), random.nextInt(255), random.nextInt(255));canvas.drawLine(startX, startY, stopX, stopY, paint);}/** * 雨滴的移动行为 */public void movestep() {//size*of这个是用来控制速度,所谓的速度就是线条增加的速度startX += sizeX*of;stopX += sizeX*of;startY += sizeY*of;stopY += sizeY*of;//如果超出边界则重新运行if (startY>height) {init();}}}三、Rainplay具体实现的类public class Rainplay extends BaseView {List<RainItem> list = new ArrayList<RainItem>();//控制雨滴的数量private int num = 80;public Rainplay(Context context) {super(context);}public Rainplay(Context context, AttributeSet attrs) {super(context, attrs);//与xml链接起来TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RainView);num = ta.getInteger(R.styleable.RainView_rainnum,80);ta.recycle();}@Overrideprotected void drawsub(Canvas canvas) {for (RainItem item : list) {item.draw(canvas);}}@Overrideprotected void move() {for (RainItem item : list) {item.movestep();}}/** * 因为获取长宽是放在layout之后才可以获取,所以需要 * 放在线程里面初始化 */@Overrideprotected void init() {for (int i = 0; i < num; i++) {RainItem item = new RainItem(getHeight(), getWidth());list.add(item);}}}四、value与xml文件<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name = "RainView"><attr name="rainnum" format="integer"/> </declare-styleable></resources>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:an="http://schemas.android.com/apk/res/com.niuli.Rain"android:layout_width="match_parent"android:layout_height="match_parent" ><com.niuli.Rain.Rainplay android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ff000000"an:rainnum = "100"/></FrameLayout>希望本文所述对大家学习Android软件编程有所帮助。