Android Lightning Effect (Electric screen, motorized)

Source: Internet
Author: User

This article introduces a very hot, fun app on Google Play, title

Implementation Ideas :

Android uses Surfaceview to draw lightning on a transparent activity, coupled with vibrations and sound effects.

Call the lightning algorithm in the touch event.

Surfaceview is a class that inherits from view, it is a very important drawing view that can get image data directly from hardware interface such as memory or DMA.

Surfaceview Features: You can draw views on threads outside the main thread without affecting the main thread, which is commonly used for game development.

surfaceviews steps to use :

Inheriting the Surfaceview class

Implement the Surfaceviewholder.callback interface and, if necessary, implement the Runnable interface (continuous view drawing in the Run method, which is a bit special, drawing the view in the touch event) and then drawing in the thread.

Rewrite:

@Override
public void surfacechanged (Surfaceholder arg0, int arg1, int arg2, int arg3) {//size changed

} @Override
public void surfacecreated (Surfaceholder arg0) {//Created
Mthread.start ();
}

@Override
public void surfacedestroyed (Surfaceholder arg0) {//Destroy when

}

Lightning algorithm:

The so-called lightning algorithm is to randomly find a lot of points between two points, and then connect these points together. So what is the basis for finding something? Let me start with my previous error algorithm: two points between the line of the near find, according to the slope of the connection between two points for vertical lines, in these perpendicular to seek random points, and then the random points sorted after the connection, I take a picture to explain (this is the error algorithm, Google Some of the similar apps on play are based on this error algorithm, why is it wrong? Because it is too fake, too unlike).

The lines are too stiff and the turn is sudden

So how is the correct lightning algorithm calculated?

Recursive

is in two point coordinate P1 (x1,y1), P2 (x2,y2) between the middle point x1,x2 the middle point x,y1,y2 the Middle point y, give X a random offset, also give Y an offset. The offset of x, Y includes the offset of the positive and negative directions, which are random values within a certain range, which is the first point of the P3 (x, y) that is calculated, and the position of the point is probably within a circle of the random radius of a bounded range in the middle of the P1,P2 connection. This is followed by recursion, repeating the calculation in P1 (x1,y1)-->p3 (x, y) and P3 (x, y)-->p2 (x2,y2) respectively. When do you get out of recursion? This is also random, we generate two random numbers to compare size, according to size to determine whether to jump. Execute Canvas.drawline (x1,y1,x2,y2) when jumping out and draw a line. In this way, the natural lightning path is generated.

Example code DrawLine (X1,y1,x2,y2,random,canvas), to define a brush first.

public void drawlightning (float x1, float y1, float x2, float y2,
int Paramint, Canvas paramcanvas) {
Random localrandom = new Random ();
if (Paramint < Localrandom.nextint (7)) {
Paramcanvas.drawline (x1, y1, x2, y2, mlighnitngcolorpaint);
Paramcanvas.drawline (x1, y1, x2, y2, mlighnitngcolorpaint);
Paramcanvas.drawline (x1, y1, x2, y2, mlighnitngglowpaintbold);
Return
}
Float x3 = 0, y3 = 0;
if (Localrandom.nextboolean ()) {
x3 = (float) ((x2 + x1)/2.0F + ((Localrandom.nextint (8)-0.5D) * paramint));
} else {
x3 = (float) ((x2 + x1)/2.0F-((Localrandom.nextint (8)-0.5D) * paramint));
}
if (Localrandom.nextboolean ()) {
Y3 = (float) ((y2 + y1)/2.0F + ((Localrandom.nextint (5)-0.5D) * paramint));
} else {
Y3 = (float) ((y2 + y1)/2.0F-((Localrandom.nextint (5)-0.5D) * paramint));
}
Drawlightning (x1, y1, X3, Y3, PARAMINT/2, Paramcanvas);
Drawlightning (x2, y2, x3, Y3, PARAMINT/2, Paramcanvas);
Return

}

To draw the path after the effect more realistic and luminous effect of the words to use setmaskfilter to draw, I draw a line, respectively, with three brushes on a track to draw three times

The first time with a thin line of paint

The second time with a wide, color heavy setmaskfilter to draw

The third time is wider than the second, but the color light setmaskfilter to draw

Three times the trajectory overlaps together, and there's the effect of the current shining in the night.

github:https://github.com/onehead/electric_screen2d

Android Lightning Effect (Electric screen, motorized)

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.