android---粒子雨效果的實現

來源:互聯網
上載者:User

標籤:多線程   繼承   設計   

剛學習了自訂view,就按照極客學院的教程做了粒子雨效果,主要用到繪畫線條和多線程,其中的抽象類別設計方法值得學習,

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();    @Override    protected final void onDraw(Canvas canvas) {        //啟動線程        if (thread ==null) {            thread = new control();            thread.start();        }else {            drawsub(canvas);        }    }    public class control extends Thread{        @Override        public 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();    }    @Override    protected void drawsub(Canvas canvas) {        for (RainItem item : list) {            item.draw(canvas);        }    }    @Override    protected void move() {        for (RainItem item : list) {            item.movestep();        }    }    /**     * 因為擷取長寬是放在layout之後才可以擷取,所以需要     * 放線上程裡面初始化     */    @Override    protected 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---粒子雨效果的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.