Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)

现在APP要求越来越高了,不只是要求实现功能,颜值的要求也越来越高,下面我们通过自定义控件来实现雪花飘落的效果,可以作为界面背景哦。

1.自定义控件:

[java] view plain copy
  1. package com.test.a;  
  2.   
  3. import java.util.Random;  
  4.   
  5. import android.content.Context;  
  6. import android.content.res.Resources;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.Canvas;  
  9. import android.graphics.Matrix;  
  10. import android.graphics.Paint;  
  11. import android.graphics.drawable.BitmapDrawable;  
  12. import android.util.AttributeSet;  
  13. import android.view.View;  
  14.   
  15.   
  16. public class FlowerView extends View {  
  17.   
  18.     Bitmap mFlowers = null;  
  19.     MyFlower flowers [] = new MyFlower[50];//50片雪花  
  20.     private Integer[] offsetX ;  
  21.     private Integer[] offsetY ;  
  22.     Random r = new Random();  
  23.     Matrix m = new Matrix();  
  24.     Paint p = new Paint();  
  25.       
  26.     int mW = 480;  
  27.     int mH = 800;  
  28.     float de = 0f;  
  29.       
  30.     public void setWH(int pW, int pH, float de){  
  31.         this.mW = pW;  
  32.         this.mH = pH;  
  33.         this.de = de;  
  34.         System.out.println("de ---->" + de);  
  35.         offsetX = new Integer[]{(int)(2*de), (int)(-2*de), (int)(-1*de), 0, (int)(1*de), (int)(2*de), (int)(1*de)};  
  36.         offsetY = new Integer[]{(int)(3*de), (int)(5*de), (int)(5*de), (int)(3*de), (int)(4*de)};  
  37.     }  
  38.       
  39.     public FlowerView(Context context) {  
  40.         super(context);  
  41.     }  
  42.   
  43.     public FlowerView(Context context, AttributeSet attrs, int defStyle) {  
  44.         super(context, attrs, defStyle);  
  45.     }  
  46.   
  47.     public FlowerView(Context context, AttributeSet attrs) {  
  48.         super(context, attrs);  
  49.   
  50.     }  
  51.       
  52.     @Override  
  53.     protected void onDraw(Canvas canvas) {  
  54.         super.onDraw(canvas);  
  55.         for (int i = 0; i < flowers.length; i++) {  
  56.             MyFlower rect = flowers[i];  
  57.             int t = rect.t;  
  58.             t--;  
  59.             if (t <= 0) {  
  60.                 rect.y += rect.g;  
  61.                 canvas.save();  
  62.                 m.reset();  
  63.                 m.setScale(rect.s, rect.s);  
  64.                 canvas.setMatrix(m);  
  65.                 p.setAlpha(rect.a);  
  66.                 canvas.drawBitmap(mFlowers, rect.x, rect.y, p);  
  67.                 canvas.restore();  
  68.             }  
  69.             rect.t = t;  
  70.             if (rect.y >= mH) {  
  71.                 rect.init();  
  72.             }  
  73.             if (rect.x >= mW || rect.x < - 20) {  
  74.                 rect.init();  
  75.             }  
  76.             flowers[i] = rect;  
  77.         }  
  78.     }  
  79.       
  80.       
  81.       
  82.     public void loadFlower(){  
  83.         Resources r = this.getContext().getResources();  
  84.         mFlowers = ((BitmapDrawable)r.getDrawable(R.drawable.snow)).getBitmap();  
  85.     }  
  86.       
  87.     public void recly(){  
  88.         if (mFlowers != null && !mFlowers.isRecycled()) {  
  89.             mFlowers.recycle();  
  90.         }  
  91.     }  
  92.       
  93.     public void addRect(){  
  94.         for (int i = 0; i < flowers.length; i++) {  
  95.             flowers[i] = new MyFlower();  
  96.         }  
  97.     }  
  98.       
  99.     public void inva(){  
  100.         invalidate();  
  101.     }  
  102.       
  103.       
  104.     class MyFlower{  
  105.         int x;  
  106.         int y;  
  107.         float s;  
  108.         int a;  
  109.         int t;  
  110.         int g;  
  111.           
  112.         public void init(){  
  113.             float aa = r.nextFloat();             
  114.             this.x = r.nextInt(mW - 80) + 80;  
  115.             this.y = 0;  
  116.             if (aa >= 1) {  
  117.                 this.s = 1.1f;  
  118.             }else if (aa <= 0.2) {  
  119.                 this.s = 0.4f;  
  120.             }else{  
  121.                 this.s = aa;  
  122.             }  
  123.             this.a = r.nextInt(155) + 100;  
  124.             this.t = r.nextInt(105) + 1;  
  125.             this.g = offsetY[r.nextInt(4)];  
  126.         }  
  127.           
  128.         public MyFlower(){  
  129.             super();  
  130.             init();  
  131.         }         
  132.           
  133.     }  
  134.       
  135. }  

2.布局:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <com.test.a.FlowerView  
  8.         android:id="@+id/flowerview"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" >  
  11.     </com.test.a.FlowerView>  
  12.   
  13. </LinearLayout>  


3.java:

[java] view plain copy
  1. package com.test.a;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.util.DisplayMetrics;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     private FlowerView mFlowerView;  
  15.     // 屏幕宽度  
  16.     public static int screenWidth;  
  17.     // 屏幕高度  
  18.     public static int screenHeight;  
  19.     Timer myTimer = null;  
  20.     TimerTask mTask = null;  
  21.     private static final int SNOW_BLOCK = 1;  
  22.     private Handler mHandler = new Handler() {  
  23.         public void dispatchMessage(Message msg) {  
  24.             mFlowerView.inva();  
  25.         };  
  26.     };  
  27.   
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.   
  33.         mFlowerView = (FlowerView) findViewById(R.id.flowerview);  
  34.         screenWidth = getWindow().getWindowManager().getDefaultDisplay()  
  35.                 .getWidth();//获取屏幕宽度          
  36.         screenHeight = getWindow().getWindowManager().getDefaultDisplay()  
  37.                 .getHeight();//获取屏幕高度  
  38.   
  39.         DisplayMetrics dis = new DisplayMetrics();  
  40.         getWindowManager().getDefaultDisplay().getMetrics(dis);  
  41.         float de = dis.density;  
  42.         mFlowerView.setWH(screenWidth, screenHeight, de);  
  43.         mFlowerView.loadFlower();  
  44.         mFlowerView.addRect();  
  45.   
  46.         myTimer = new Timer();  
  47.         mTask = new TimerTask() {  
  48.             @Override  
  49.             public void run() {  
  50.                 Message msg = new Message();  
  51.                 msg.what = SNOW_BLOCK;  
  52.                 mHandler.sendMessage(msg);  
  53.             }  
  54.         };  
  55.         myTimer.schedule(mTask, 300010);  
  56.     }  
  57.       
  58.     @Override  
  59.     protected void onDestroy() {  
  60.         // TODO Auto-generated method stub  
  61.         super.onDestroy();  
  62.         mFlowerView.recly();  
  63.     }  
  64. }  

运行实例如下:

Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)

喜欢的朋友请关注我!

源码下载