Java中Date類和Calendar類的經常使用實例小結。本站提示廣大學習愛好者:(Java中Date類和Calendar類的經常使用實例小結)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中Date類和Calendar類的經常使用實例小結正文
本文實例引見了Android完成粒子雨後果的完成進程,分享給年夜家供年夜家參考,詳細內容以下
先看看後果圖:
詳細完成辦法:
1.baseview重要是設定雨滴要完成的舉措,只是先設定,也就是籠統辦法,在子類中完成其辦法
2.Rainitems封裝雨滴類
3.Rainitems對雨滴聚集創立到面板中,顯示出來,詳細完成就是在這個類中
1、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(); } } } } }
2、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(); } } }
3、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); } } }
4、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軟件編程有所贊助。