Simple tutorial for Android-69th guns (custom controls for Snowflake falling effect)
At present, the APP requirements are getting higher and higher, not just for functions, but for face filter values. Below we use custom controls to achieve the effect of falling snow, which can be used as the interface background.
1. Custom Controls:
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 pieces of snowflake 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);} @ Overrideprotected void onDraw (Canvas canv As) {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. Layout:
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; // screen width public static int screenWidth; // screen height 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 () ;};}; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mFlowerView = (FlowerView) findViewById (R. id. flowerview); screenWidth = getWindow (). getWindowManager (). getdefadisplay display (). getWidth (); // obtain the screen width screenHeight = getWindow (). getWindowManager (). getdefadisplay display (). getHeight (); // obtain the screen height DisplayMetrics dis = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dis); float de = dis. density; mFlowerView. setWH (screenWidth, screenHeight, de); mFlowerView. loadFlower (); mFlowerView. addRect (); myTimer = new Timer (); mTask = new TimerTask () {@ Overridepublic void run () {Message msg = new Message (); msg. what = SNOW_BLOCK; mHandler. sendMessage (msg) ;}}; myTimer. schedule (mTask, 3000, 10) ;}@ Overrideprotected void onDestroy () {// TODO Auto-generated method stubsuper. onDestroy (); mFlowerView. recly ();}}
The running instance is as follows: