Android custom Control (effect one) tap the screen to draw the ring based on the location you clicked

Source: Internet
Author: User

Before I saw the effect of a water wave on a Samsung phone after tapping the screen, I tried to write a similar result.


Implement a custom view and implement the constructor method

public class MyView extends View {

Public MyView (Context context) {
Super (context);
TODO auto-generated Constructor stub
}

Public MyView (context context, AttributeSet Attrs) {
Super (context, attrs);
TODO auto-generated Constructor stub
}
}

If you want to draw a circle, you need to set the radius of the ring, the stroke width (if filling is not required), the coordinates of x and Y, because you need to disappear after clicking on the screen, so add transparency

So we're going to create a new bean to implement these

public class Mybean {

int Alpha; Transparency
int X; X-coordinate
int Y; Y-coordinate
float width; Stroke width
float radius; Radius
Paint paint; Brush

}


Next is to get a click on the screen after the event, the idea is very clear, after tapping the screen, first set the radius of the drawn circle is 0, the transparency is the maximum value of 255, the stroke width is freely set by personal preference

@Override
public boolean ontouchevent (Motionevent event) {
Super.ontouchevent (event);

Switch (event.getaction ()) {
Case Motionevent.action_down:
After tapping the screen, set the radius to 0,alpha to 255.
Mybean bean = new Mybean ();
Bean.radius = 0; Click the rear radius to first set to 0
Bean.alpha = Maxalpha; Alpha set to Maximum value 255
Bean.width = BEAN.RADIUS/8; Stroke width This random
Bean. X = (int) event.getx (); The x-coordinate of the drawn circle
Bean. Y = (int) event.gety (); The y-coordinate of the drawn circle
Bean.paint = Initpaint (Bean.alpha, bean.width);

Break
}
return true;
}

After setting the properties, give handler to draw the interface, each after x milliseconds, draw the radius of the circle +n, transparency-m

Bean.radius + = 5;//radius +5 per time
Bean.alpha-= 10;//transparency minus 10 per time
if (Bean.alpha < 0) {
Transparency is 0 when it rains 0.
Bean.alpha = 0;
}
Bean.width = BEAN.RADIUS/8; Stroke width set to 1/4 of radius
Bean.paint.setAlpha (Bean.alpha);
Bean.paint.setStrokeWidth (Bean.width);

So far, all of our main code has been implemented

Attached demo Address: http://download.csdn.net/detail/qq_18612815/9511652






Android custom Control (effect one) tap the screen to draw the ring based on the location you clicked

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.