Android game development: How to Achieve explosive performance

Source: Internet
Author: User


During the development of magicbubble In the android game, when two buckets are connected, the bubble disappears in the case of bubbles. The idea of the author is as follows: add an imageview to framelayout and define an explosion of animation. imageview is hidden when it is not needed, move the imageview to the desired location and then startanimation to achieve the explosive effect. The following is the simplified code of the program. The effect of the program is as follows: Click anywhere on the screen to display the explosion effect.


The first is the definition of animation, which defines a frame animation and plays 5 animations in sequence. The duration of each animation is 50 milliseconds:

<Animation-list xmlns: Android = ""
Android: oneshot = "true">
<Item Android: drawable = "@ drawable/explode1" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode2" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode3" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode4" Android: Duration = "50"/>
<Item Android: drawable = "@ drawable/explode5" Android: Duration = "50"/>

The following is the main program code: Package com. Ray. Bubble;
Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. drawable. animationdrawable;
Import Android. OS. Bundle;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. View. View. ontouchlistener;
Import Android. widget. framelayout;
Import Android. widget. imageview;
Publicclass bubbleexplosion extends activity {
Private framelayout FL;
Private explosionview exv1;
Private animationdrawable exa1;
Publicvoid oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Set full screen
Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
Windowmanager. layoutparams. flag_fullscreen );
FL = new framelayout (this );
Fl. setbackgroundresource (R. drawable. bg );
Exv1 = new explosionview (this );
Exv1.setvisibility (view. Invisible );
Exv1.setbackgroundresource (R. anim. EXPLOSION );
Exa1 = (animationdrawable) exv1.getbackground ();
Fl. addview (exv1 );
Fl. setontouchlistener (New layoutlistener ());
Setcontentview (FL );
Class explosionview extends imageview {
Public explosionview (context ){
Super (context );
// Handle the explosion location
Publicvoid setlocation (INT top, int left ){
This. setframe (left, top, left + 40, top + 40 );
Class layoutlistener implements ontouchlistener {
Publicboolean ontouch (view V, motionevent event ){
// First, you must stop playing the animation. If the animation starts, you cannot repeat it!
Exv1.setvisibility (view. Invisible );
Exa1.stop ();
Float x = event. getx ();
Float y = event. Gety ();
Exv1.setlocation (INT) y-20, (INT) x-20 );
Exv1.setvisibility (view. Visible );
Exa1.start ();

With surfaceview of Android, animation can achieve a good transitional effect, and surfaceview is easy to use.

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: 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.