Android creates snowflakes and raindrops for romantic effects _android

Source: Internet
Author: User
Tags random seed sin

This article on the basis of the implementation of snowflakes, according to the flying snowflakes, to achieve the effect of rainy days, using the Eclipse Android version, specific content as follows

Snowflake Effect Chart:

Specific code:

1, flying snowflakes in the sky main code
Snowview

<span style= "FONT-SIZE:14PX;" 
 
>package com.ex</span>ample.snowflake.view; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.util.AttributeSet; 
 
Import Android.view.View; /** * Snowflake View, delay time redraw, draw num_snowflakes snowflake/public class Snowview extends View {private static final int num_s Nowflakes = 150; Snowflake Quantity private static final int DELAY = 5; Delay private snowflake[] msnowflakes; 
 Snowflake public Snowview (context) {super (context); 
 Public Snowview (context, AttributeSet attrs) {Super (context, attrs); 
 Public Snowview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH) 
 ; 
 if (w!= oldw | | h!= OLDH) {Initsnow (W, h); }} private void Initsnow (int width, int height) {Paint Paint = new Paint (paint.anti_alias_flag);//Anti-aliasing Paint.setcolor (Color.White);//White Snowflake paint.se Tstyle (Paint.Style.FILL); 
 Fill 
 Msnowflakes = new Snowflake[num_snowflakes]; Msnowflakes all snowflakes are generated here for (int i = 0; i < num_snowflakes ++i) {Msnowflakes[i] = snowflake.create (width, hei 
 ght, paint); 
 } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
 For return snowflake for (Snowflake s:msnowflakes) {//Then draw S.draw (canvas); 
 ///GetHandler Once a time, animation effect (). postdelayed (runnable, DELAY); 
 //Redraw thread private Runnable Runnable = new Runnable () {@Override public void run () {//Automatically refresh invalidate (); 
} 
 };  }

Snowflake

Package Com.example.snowflake.view; 
 
Import Com.example.snowflake.RandomGenerator; 
Import Android.graphics.Canvas; 
Import Android.graphics.Paint; 
 
Import Android.graphics.Point; 
 /** * Snowflake class, move, move out of the screen will reset position. * * public class Snowflake {//Snowflake angle private static final float Ange_range = 0.1f;//angle range private static final Flo at half_angle_range = ange_range/2f; General angle private static final float Half_pi = (float) math.pi/2f; Half pi private static final float angle_seed = 25f; 
 Angle random seed private static final float angle_divisor = 10000f; 
 Snow's moving speed private static final float increment_lower = 2f; 
 
 Private static final float Increment_upper = 4f; 
 The size of the snowflake private static final float flake_size_lower = 7f; 
 
 Private static final float flake_size_upper = 20f; Private final Randomgenerator Mrandom; Random controller private final point mposition; Snowflake position private float mangle; Angle private final float mincrement; The speed of snowflakes private final float mflakesize; The size of snowflakes private final Paint mpaint; Brush private Snowflake (randomgenerator random, point position, float angle, float increment, float flakesize, Paint p 
 aint) {mrandom = random; 
 Mposition = position; 
 Mincrement = increment; 
 Mflakesize = flakesize; 
 Mpaint = paint; 
 mangle = angle; public static snowflake Create (int width, int height, Paint Paint) {randomgenerator random = new Randomgenerator ( 
 ); 
 int x = random.getrandom (width); 
 int y = random.getrandom (height); 
 Point position = new Point (x, y); 
 float angle = random.getrandom (angle_seed)/angle_seed * ange_range + half_pi-half_angle_range; 
 float increment = Random.getrandom (Increment_lower, Increment_upper); 
 float flakesize = Random.getrandom (Flake_size_lower, Flake_size_upper); 
 return new snowflake (random, position, angle, increment, flakesize, paint); 
 //Draw snowflake public void Draw (Canvas Canvas) {int width = canvas.getwidth (); 
 int height = canvas.getheight (); Move (width, HeigHT); 
 Canvas.drawcircle (mposition.x, MPOSITION.Y, Mflakesize, Mpaint); }//move snowflake private void Move (int width, int height) {//x horizontal direction, then need to shake, the main set this value can be, now cancel the shaking//if the mposition.x does not add the following value, 
 It won't rock. Double x = mposition.x + (mincrement * Math.Cos (mangle)); 
 
 Y is the vertical direction, the drop double y = mposition.y + (mincrement * Math.sin (mangle)); 
 Mangle + + mrandom.getrandom (-angle_seed, angle_seed)/angle_divisor; 
 
 This is to set the snowflake position, if refreshed in a very short time, is to connect the animation effect Mposition.set ((int) x, (int) y); 
 Remove screen, restart if (!isinside (width, height)) {//Reset snowflake Reset (width); 
 }//Judge whether the private Boolean isinside (int width, int height) {int x = mposition.x; 
 int y = MPOSITION.Y; return x > MFlakeSize-5 && x + mflakesize <= width && y >=-mflakesize-1 && y-mflake 
 Size < height; 
 //reset snowflake private void Reset (int width) {mposition.x = Mrandom.getrandom (width); MPOSITION.Y = (int) (-MFLAKESIZE-1); Top mangle = Mrandom.getrandom (anGle_seed)/angle_seed * ange_range + half_pi-half_angle_range; 
 } 
}

2, the implementation of rainy day effect code
Rainview

Package Com.example.raindrop.view; 
 
Import COM.EXAMPLE.RAINDROP.R; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Paint; 
Import Android.util.AttributeSet; 
 
Import Android.view.View; /** * Raindrop View, delay time redraw, draw Num_snowflakes raindrops/public class Rainview extends view {private static final int num_s Nowflakes = 150; Raindrop Quantity private static final int DELAY = 5; Delay private rainflake[] msnowflakes; 
 Raindrops public Rainview (context) {super (context); 
 Public Rainview (context, AttributeSet attrs) {Super (context, attrs); 
 Public Rainview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH) 
 ; 
 if (w!= oldw | | h!= OLDH) {Initsnow (W, h); } private void Initsnow (int width, int height) {Paint Paint = new Paint (Paint.Anti_alias_flag); Anti-aliasing Paint.setcolor (Getresources (). GetColor (R.color.colorwater)); The color of Raindrops Paint.setstyle (Paint.Style.FILL); 
 Fill 
 Msnowflakes = new Rainflake[num_snowflakes]; Msnowflakes all raindrops are generated and put in here for (int i = 0; i < num_snowflakes ++i) {Msnowflakes[i] = rainflake.create (width, hei 
 ght, paint); 
 } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
 For return snowflake for (Rainflake s:msnowflakes) {//Then draw S.draw (canvas); 
 ///GetHandler Once a time, animation effect (). postdelayed (runnable, DELAY); 
 //Redraw thread private Runnable Runnable = new Runnable () {@Override public void run () {//Automatically refresh invalidate (); 
} 
 }; 

 }

Rainflake

Package Com.example.raindrop.view; 
 
Import Com.example.raindrop.RandomGenerator; 
Import Android.graphics.Canvas; 
 
Import Android.graphics.Paint; 
 /** * Raindrops of the class, move, remove the screen will reset the position. 
 * * public class Rainflake {//Raindrops moving speed private static final float increment_lower = 6f; 
 
 Private static final float increment_upper = 8f; 
 The size of the raindrops private static final float flake_size_lower = 2f; 
 
 Private static final float flake_size_upper = 5f; Private final float mincrement; The speed of raindrops private final float mflakesize; The size of the raindrops private final Paint mpaint; Brush Private Line Mline; 
 
 Raindrops private Randomgenerator Mrandom; 
 Private Rainflake (Randomgenerator random,line line, float increment, float flakesize, Paint Paint) {mrandom = random; 
 Mline = line; 
 Mincrement = increment; 
 Mflakesize = flakesize; 
 Mpaint = paint; //Generate raindrops public static rainflake Create (int width, int height, Paint Paint) {randomgenerator random = new RANDOMG 
 Enerator (); int [] NliNe 
  
 nline = random.getline (width, height); 
 Line line = new Line (Nline[0], nline[1], nline[2], nline[3]); 
 float increment = Random.getrandom (Increment_lower, Increment_upper); 
 float flakesize = Random.getrandom (Flake_size_lower, Flake_size_upper); 
 return new Rainflake (Random,line, increment, flakesize, paint); 
 //Draw Raindrop public void Draw (Canvas Canvas) {int width = canvas.getwidth (); 
 int height = canvas.getheight (); 
 DrawLine (canvas, width, height); /** * Changed to line, similar to raindrop effect * @param canvas * @param width * @param height/private void drawLine (canvas canvas 
 , int width, int height) {//Set line width mpaint.setstrokewidth (mflakesize); 
 Y is the vertical direction, is the drop double y1 = mline.y1 + (mincrement * Math.sin (1.5)); 
 
 Double y2 = mline.y2 + (mincrement * Math.sin (1.5)); 
  
 This is set the Raindrop position, if in a very short time refresh, is the animation effect Mline.set (mline.x1, (int) y1,mline.x2, (int) y2); 
 if (!isinsideline (height)) {resetline (width,height); } canvas.drawline (Mline.x1, Mline.y1,MLINE.X2, Mline.y2, Mpaint); }//Determine if the private boolean isinsideline (int height) {return mline.y1 < height && Mline.y2 < Heigh 
 T 
 //Reset Raindrop private void resetline (int width, int height) {int [] nline; 
 nline = mrandom.getline (width, height); 
 mline.x1 = nline[0]; 
 Mline.y1 = nline[1]; 
 MLINE.X2 = nline[2]; 
 Mline.y2 = nline[3];  } 
 
}

Raindrop Effect Chart:

The above is the entire content of this article, to help everyone easily realize the romantic snow raindrop effect, we can apply the effect to their own projects, I hope you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.