Android in the implementation of the broken effect on the click Screen

Source: Internet
Author: User

yesterday on the Internet, with the brothers to discuss the effect of clicking the screen broken, today simply take out time to tidy up, very simple Austrian,Basic idea: 1.   Customize a view to show the broken effect 2. Add this view to the activity by Setcontentview (view);       3. Be careful to set the activity's theme to Android:theme= "@android: Style/theme.translucent.notitlebar" so that it will be more effective to play. First rewrite a view, let's call Customeview.  It's fine to define yourself.  The definition constructor is as follows: Public CustomView (Context context, AttributeSet Attrs) {super (context);  This.setkeepscreenon (TRUE);  This.setfocusable (TRUE);  This.setlongclickable (TRUE);  This.msoundpool = new Soundpool (5, Audiomanager.stream_system, 5);  This.mSoundMap.put (1, msoundpool.load (context, R.RAW.CFOKWOWBFV, 1));  This.mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.screen);  Mxpointlist = new arraylist<float> (); Mypointlist = new arraylist<float> (); }
Here a sound pool is declared (for tapping the screen, making a broken, lifelike effect), a bitmap that shows that the screen is broken in two lists, respectively.mxpointlistand themypointlist, which is used to save points of X and y when clicked.
Next, let's take a look at ontouchevent ()@OverridePublic Boolean ontouchevent (motionevent arg1) {//TODO auto-generated method stubswitch (arg1.getaction ()) {Case Motionevent.action_down:playSound ();//AudibleMxpointlist.add (Arg1.getx ());Mypointlist.add (Arg1.gety ());postinvalidate ();//Refresh Interfacemcount++;//The number of clicks, where,Mlength is the total numberif (MCount > Mlength) {mxpointlist.remove (0);mypointlist.remove (0);mlength++;   }Break ;Case MOTIONEVENT.ACTION_UP:Break ;Case Motionevent.action_move:Break ;Default:Break ;  }return super.ontouchevent (arg1); }
You can refer to the comments I wrote, and see that the last isOnDraw () method, this method is particularly important. @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas); for (int i = 0; i < mxpointlist.size (); ++i) {//points how many times, the broken picture is displayed how many times Canvas.drawbitmap (Mbitmap, Mxpointlist.get (i)-MB  Itmap.getwidth ()/2, Mypointlist.get (i)-mbitmap.getheight ()/2, NULL); } }
The following settings are then made in the OnCreate method in the activity to be referenced:
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        CustomView view = new CustomView (this, null); Setcontentview (view);
}

Also, don't forget to add <activity android:name= to the Mainfest file. Screencrashmainactivity "android:theme= "@android: Style/theme.translucent.notitlebar"Android:label= "@string/app_name" >
More realistic results. Hee Hee All right, finish the call!

Android in the implementation of the broken effect on the click Screen

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.