Android實(shí)現(xiàn)粒子雨效果
本文實(shí)例介紹了Android實(shí)現(xiàn)粒子雨效果的實(shí)現(xiàn)過(guò)程,分享給大家供大家參考,具體內(nèi)容如下
先看看效果圖:
具體實(shí)現(xiàn)方法:
1.baseview主要是設(shè)定雨滴要實(shí)現(xiàn)的動(dòng)作,只是先設(shè)定,也就是抽象方法,在子類中實(shí)現(xiàn)其方法
2.Rainitems封裝雨滴類
3.Rainitems對(duì)雨滴集合創(chuàng)建到面板中,顯示出來(lái),具體實(shí)現(xiàn)就是在這個(gè)類中
一、baseview封裝類,子類繼承后實(shí)現(xiàn)方法即可
public abstract class BaseView extends View { private control thread; public BaseView(Context context, AttributeSet attrs) { super(context, attrs); } public BaseView(Context context) { super(context); } //封裝,構(gòu)造畫(huà)面,子類繼承后需要重寫(xiě) protected abstract void drawsub(Canvas canvas); //封裝移動(dòng)方法,子類繼承后需要重寫(xiě) protected abstract void move(); //封裝的初始化方法 protected abstract void init(); @Override protected final void onDraw(Canvas canvas) { //啟動(dòng)線程 if (thread ==null) { thread = new control(); thread.start(); }else { drawsub(canvas); } } public class control extends Thread{ @Override public void run() { init(); while(true){ move(); //相當(dāng)于刷新畫(huà)布 postInvalidate(); try { sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
二、Rainitem雨點(diǎn)類
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對(duì)應(yīng)的分別是起止位置 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(); } /** * 繪畫(huà)雨滴 * @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); } /** * 雨滴的移動(dòng)行為 */ public void movestep() { //size*of這個(gè)是用來(lái)控制速度,所謂的速度就是線條增加的速度 startX += sizeX*of; stopX += sizeX*of; startY += sizeY*of; stopY += sizeY*of; //如果超出邊界則重新運(yùn)行 if (startY>height) { init(); } } }
三、Rainplay具體實(shí)現(xiàn)的類
public class Rainplay extends BaseView { List<RainItem> list = new ArrayList<RainItem>(); //控制雨滴的數(shù)量 private int num = 80; public Rainplay(Context context) { super(context); } public Rainplay(Context context, AttributeSet attrs) { super(context, attrs); //與xml鏈接起來(lái) 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(); } } /** * 因?yàn)楂@取長(zhǎng)寬是放在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>
希望本文所述對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
AndroidApk混淆編譯時(shí),報(bào)告java.io.IOException...錯(cuò)誤解決辦法
這篇文章主要介紹了 AndroidApk混淆編譯時(shí),報(bào)告Error:Execution failed for task ‘:gviews:transformClassesAndResourcesWithProguardForRelease’.錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-03-03編寫(xiě)簡(jiǎn)易Android天氣應(yīng)用的代碼示例
這篇文章主要介紹了編寫(xiě)簡(jiǎn)易Android天氣應(yīng)用的代碼示例,文中的例子主要是利用到了RxAndroid處理異步方法,需要的朋友可以參考下2016-02-02Android編程實(shí)現(xiàn)圖標(biāo)拖動(dòng)效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)圖標(biāo)拖動(dòng)效果的方法,涉及Android事件響應(yīng)及圖標(biāo)變換的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android開(kāi)發(fā)之開(kāi)門(mén)狗在程序鎖中的應(yīng)用實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之開(kāi)門(mén)狗在程序鎖中的應(yīng)用,以完整實(shí)例形式分析了程序鎖的使用技巧,需要的朋友可以參考下2016-02-02Android中l(wèi)istview和imageview實(shí)現(xiàn)條目單選效果
這篇文章主要為大家詳細(xì)介紹了Android中l(wèi)istview和imageview實(shí)現(xiàn)條目單選效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android小程序?qū)崿F(xiàn)音樂(lè)播放列表
這篇文章主要為大家詳細(xì)介紹了Android小程序?qū)崿F(xiàn)音樂(lè)播放列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android studio 運(yùn)行main 函數(shù)的方法
這篇文章主要介紹了Android studio 運(yùn)行main 函數(shù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Android 中RecyclerView通用適配器的實(shí)現(xiàn)
這篇文章主要介紹了Android 中RecyclerView通用適配器的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-03-03