1. 程式人生 > >Android實戰簡易教程-第六十九槍(自定義控制元件實現雪花飄落效果)

Android實戰簡易教程-第六十九槍(自定義控制元件實現雪花飄落效果)

現在APP要求越來越高了,不只是要求實現功能,顏值的要求也越來越高,下面我們通過自定義控制元件來實現雪花飄落的效果,可以作為介面背景哦。

1.自定義控制元件:

package com.test.a;

import java.util.Random;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;


public class FlowerView extends View {

	Bitmap mFlowers = null;
	MyFlower flowers [] = new MyFlower[50];//50片雪花
	private Integer[] offsetX ;
	private Integer[] offsetY ;
	Random r = new Random();
	Matrix m = new Matrix();
	Paint p = new Paint();
	
	int mW = 480;
	int mH = 800;
	float de = 0f;
	
	public void setWH(int pW, int pH, float de){
		this.mW = pW;
		this.mH = pH;
		this.de = de;
		System.out.println("de ---->" + de);
		offsetX = new Integer[]{(int)(2*de), (int)(-2*de), (int)(-1*de), 0, (int)(1*de), (int)(2*de), (int)(1*de)};
		offsetY = new Integer[]{(int)(3*de), (int)(5*de), (int)(5*de), (int)(3*de), (int)(4*de)};
	}
	
	public FlowerView(Context context) {
		super(context);
	}

	public FlowerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public FlowerView(Context context, AttributeSet attrs) {
		super(context, attrs);

	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		for (int i = 0; i < flowers.length; i++) {
			MyFlower rect = flowers[i];
			int t = rect.t;
			t--;
			if (t <= 0) {
				rect.y += rect.g;
				canvas.save();
				m.reset();
				m.setScale(rect.s, rect.s);
				canvas.setMatrix(m);
				p.setAlpha(rect.a);
				canvas.drawBitmap(mFlowers, rect.x, rect.y, p);
				canvas.restore();
			}
			rect.t = t;
			if (rect.y >= mH) {
				rect.init();
			}
			if (rect.x >= mW || rect.x < - 20) {
				rect.init();
			}
			flowers[i] = rect;
		}
	}
	
	
	
	public void loadFlower(){
		Resources r = this.getContext().getResources();
		mFlowers = ((BitmapDrawable)r.getDrawable(R.drawable.snow)).getBitmap();
	}
	
	public void recly(){
		if (mFlowers != null && !mFlowers.isRecycled()) {
			mFlowers.recycle();
		}
	}
	
	public void addRect(){
		for (int i = 0; i < flowers.length; i++) {
			flowers[i] = new MyFlower();
		}
	}
	
	public void inva(){
		invalidate();
	}
	
	
	class MyFlower{
		int x;
		int y;
		float s;
		int a;
		int t;
		int g;
		
		public void init(){
			float aa = r.nextFloat();			
			this.x = r.nextInt(mW - 80) + 80;
			this.y = 0;
			if (aa >= 1) {
				this.s = 1.1f;
			}else if (aa <= 0.2) {
				this.s = 0.4f;
			}else{
				this.s = aa;
			}
			this.a = r.nextInt(155) + 100;
			this.t = r.nextInt(105) + 1;
			this.g = offsetY[r.nextInt(4)];
		}
		
		public MyFlower(){
			super();
			init();
		}		
		
	}
	
}

2.佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.test.a.FlowerView
        android:id="@+id/flowerview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.test.a.FlowerView>

</LinearLayout>
3.java:
package com.test.a;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	private FlowerView mFlowerView;
	// 螢幕寬度
	public static int screenWidth;
	// 螢幕高度
	public static int screenHeight;
	Timer myTimer = null;
	TimerTask mTask = null;
	private static final int SNOW_BLOCK = 1;
	private Handler mHandler = new Handler() {
		public void dispatchMessage(Message msg) {
			mFlowerView.inva();
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mFlowerView = (FlowerView) findViewById(R.id.flowerview);
		screenWidth = getWindow().getWindowManager().getDefaultDisplay()
				.getWidth();//獲取螢幕寬度		
		screenHeight = getWindow().getWindowManager().getDefaultDisplay()
				.getHeight();//獲取螢幕高度

		DisplayMetrics dis = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dis);
		float de = dis.density;
		mFlowerView.setWH(screenWidth, screenHeight, de);
		mFlowerView.loadFlower();
		mFlowerView.addRect();

		myTimer = new Timer();
		mTask = new TimerTask() {
			@Override
			public void run() {
				Message msg = new Message();
				msg.what = SNOW_BLOCK;
				mHandler.sendMessage(msg);
			}
		};
		myTimer.schedule(mTask, 3000, 10);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mFlowerView.recly();
	}
}

執行例項如下:

喜歡的朋友請關注我!

原始碼下載