📌  相关文章
📜  如何在 Android 应用程序中添加 ParticleSmasher?

📅  最后修改于: 2022-05-13 01:55:28.413000             🧑  作者: Mango

如何在 Android 应用程序中添加 ParticleSmasher?

ParticleSmasher 是一个 Android 库,它允许我们轻松地在我们的 android 应用程序中为我们的视图添加粒子效果。我们可以在许多应用程序中使用此功能,例如我们在完成任务后销毁特定 UI 或删除特定 UI 时的应用程序文件。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

在 Android 应用示例 GIF 中添加 ParticleSmasher

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。

第 2 步:在进入编码部分之前先做一些预任务



转到app -> res -> values -> colors.xml文件并设置应用程序的颜色。

XML


  
    #0F9D58
    #0F9D58
    #05af9b
    


XML


  
    
    
          
        
        


Java
// creating  ParticleSmasher object
ParticleSmasher smasher = new ParticleSmasher(MainActivity.this);
  
Button btn1=(Button) findViewById(R.id.button1);
Button btn2=(Button) findViewById(R.id.button2);
Button btn3=(Button) findViewById(R.id.button3);
Button btn4=(Button) findViewById(R.id.button4);
Button btn5=(Button) findViewById(R.id.button5);
Button btn6=(Button) findViewById(R.id.button6);


Java
// add the code iside all buttons onclick 
smasher.with(view)
                  .setStyle(SmashAnimator.STYLE_DROP)    // Set animation style
                  .setDuration(1500)                     // Set animation time
                  .setStartDelay(300)                    // Set the delay before the animation
                  .setHorizontalMultiple(2)              // Set the amplitude of lateral movement, the default is 3
                  .setVerticalMultiple(2)                // Set the vertical movement amplitude, the default is 4
                  .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                   @Override
                   public void onAnimatorStart() {
                       super.onAnimatorStart();
                       // Callback, the animation starts
                       Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                    }
  
                   @Override
                   public void onAnimatorEnd() {
                       super.onAnimatorEnd();
                       // Callback, the animation ends
                       Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                        }
                   }).start();


Java
// click listener for  btn1
btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_DROP)    // Set animation style
                        .setDuration(1500)                     // Set animation time
                        .setStartDelay(300)                    // Set the delay before the animation
                        .setHorizontalMultiple(2)              // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn2
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_EXPLOSION)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn3
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_BOTTOM)    // Set animation style
                        .setDuration(1500)                             // Set animation time
                        .setStartDelay(300)                            // Set the delay before the animation
                        .setHorizontalMultiple(2)                      // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                        // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn4
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_LEFT)    // Set animation style
                        .setDuration(1500)                           // Set animation time
                        .setStartDelay(300)                          // Set the delay before the animation
                        .setHorizontalMultiple(2)                    // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                      // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
  
        // click listener for  btn5
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_RIGHT)    // Set animation style
                        .setDuration(1500)                            // Set animation time
                        .setStartDelay(300)                           // Set the delay before the animation
                        .setHorizontalMultiple(2)                     // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                       // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
  
        // click listener for  btn6
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_TOP)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
          }
});


Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.fadai.particlesmasher.ParticleSmasher;
import com.fadai.particlesmasher.SmashAnimator;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // creating  ParticleSmasher object
        ParticleSmasher smasher = new ParticleSmasher(MainActivity.this);
  
        Button btn1=(Button) findViewById(R.id.button1);
        Button btn2=(Button) findViewById(R.id.button2);
        Button btn3=(Button) findViewById(R.id.button3);
        Button btn4=(Button) findViewById(R.id.button4);
        Button btn5=(Button) findViewById(R.id.button5);
        Button btn6=(Button) findViewById(R.id.button6);
  
        // click listener for  btn1
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_DROP)    // Set animation style
                        .setDuration(1500)                     // Set animation time
                        .setStartDelay(300)                    // Set the delay before the animation
                        .setHorizontalMultiple(2)              // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn2
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_EXPLOSION)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn3
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_BOTTOM)    // Set animation style
                        .setDuration(1500)                             // Set animation time
                        .setStartDelay(300)                            // Set the delay before the animation
                        .setHorizontalMultiple(2)                      // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                        // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn4
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_LEFT)    // Set animation style
                        .setDuration(1500)                           // Set animation time
                            .setStartDelay(300)                      // Set the delay before the animation
                        .setHorizontalMultiple(2)                    // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                      // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn5
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_RIGHT)    // Set animation style
                        .setDuration(1500)                            // Set animation time
                        .setStartDelay(300)                           // Set the delay before the animation
                        .setHorizontalMultiple(2)                     // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                       // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn6
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_TOP)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
    }
}


转到 Gradle Scripts -> build.gradle (Module: app)部分并导入以下依赖项,然后在上面的弹出窗口中单击“立即同步”。

第 3 步:设计 UI

activity_main.xml 中删除默认的Text View 并将布局更改为Relative 布局,并在其中添加一个垂直的LinearLayout 并在LinearLayout 内添加6 个具有不同id、文本和颜色的按钮。下面是activity_main.xml文件的代码。

XML



  
    
    
          
        
        

第 4 步:编码部分



打开主活动。 Java文件和onCreate()方法内部创建和初始化 ParticleSmasher 对象并对 6 个按钮执行相同的操作,如下所示。

Java

// creating  ParticleSmasher object
ParticleSmasher smasher = new ParticleSmasher(MainActivity.this);
  
Button btn1=(Button) findViewById(R.id.button1);
Button btn2=(Button) findViewById(R.id.button2);
Button btn3=(Button) findViewById(R.id.button3);
Button btn4=(Button) findViewById(R.id.button4);
Button btn5=(Button) findViewById(R.id.button5);
Button btn6=(Button) findViewById(R.id.button6);

下面是用于粉碎视图的代码(动画开始结束时它也有OnAnimatorListener ,我们只是在方法内部显示 toast 消息),我们将在不同按钮的 onClick Listeners 中调用此视图

Java

// add the code iside all buttons onclick 
smasher.with(view)
                  .setStyle(SmashAnimator.STYLE_DROP)    // Set animation style
                  .setDuration(1500)                     // Set animation time
                  .setStartDelay(300)                    // Set the delay before the animation
                  .setHorizontalMultiple(2)              // Set the amplitude of lateral movement, the default is 3
                  .setVerticalMultiple(2)                // Set the vertical movement amplitude, the default is 4
                  .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                   @Override
                   public void onAnimatorStart() {
                       super.onAnimatorStart();
                       // Callback, the animation starts
                       Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                    }
  
                   @Override
                   public void onAnimatorEnd() {
                       super.onAnimatorEnd();
                       // Callback, the animation ends
                       Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                        }
                   }).start();

接下来,为所有 6 个按钮创建一个单独的 OnClickListener,并在 onClick(视图视图)方法中添加具有不同效果的上述代码。

Java

// click listener for  btn1
btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_DROP)    // Set animation style
                        .setDuration(1500)                     // Set animation time
                        .setStartDelay(300)                    // Set the delay before the animation
                        .setHorizontalMultiple(2)              // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn2
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_EXPLOSION)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn3
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_BOTTOM)    // Set animation style
                        .setDuration(1500)                             // Set animation time
                        .setStartDelay(300)                            // Set the delay before the animation
                        .setHorizontalMultiple(2)                      // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                        // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn4
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_LEFT)    // Set animation style
                        .setDuration(1500)                           // Set animation time
                        .setStartDelay(300)                          // Set the delay before the animation
                        .setHorizontalMultiple(2)                    // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                      // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
  
        // click listener for  btn5
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_RIGHT)    // Set animation style
                        .setDuration(1500)                            // Set animation time
                        .setStartDelay(300)                           // Set the delay before the animation
                        .setHorizontalMultiple(2)                     // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                       // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
  
        // click listener for  btn6
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_TOP)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
          }
});

下面是MainActivity的完整代码。 Java文件。代码内部添加注释,以便更详细地理解代码

Java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.fadai.particlesmasher.ParticleSmasher;
import com.fadai.particlesmasher.SmashAnimator;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // creating  ParticleSmasher object
        ParticleSmasher smasher = new ParticleSmasher(MainActivity.this);
  
        Button btn1=(Button) findViewById(R.id.button1);
        Button btn2=(Button) findViewById(R.id.button2);
        Button btn3=(Button) findViewById(R.id.button3);
        Button btn4=(Button) findViewById(R.id.button4);
        Button btn5=(Button) findViewById(R.id.button5);
        Button btn6=(Button) findViewById(R.id.button6);
  
        // click listener for  btn1
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_DROP)    // Set animation style
                        .setDuration(1500)                     // Set animation time
                        .setStartDelay(300)                    // Set the delay before the animation
                        .setHorizontalMultiple(2)              // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn2
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_EXPLOSION)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn3
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_BOTTOM)    // Set animation style
                        .setDuration(1500)                             // Set animation time
                        .setStartDelay(300)                            // Set the delay before the animation
                        .setHorizontalMultiple(2)                      // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                        // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn4
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_LEFT)    // Set animation style
                        .setDuration(1500)                           // Set animation time
                            .setStartDelay(300)                      // Set the delay before the animation
                        .setHorizontalMultiple(2)                    // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                      // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn5
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_RIGHT)    // Set animation style
                        .setDuration(1500)                            // Set animation time
                        .setStartDelay(300)                           // Set the delay before the animation
                        .setHorizontalMultiple(2)                     // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                       // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
  
        // click listener for  btn6
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                smasher.with(view)
                        .setStyle(SmashAnimator.STYLE_FLOAT_TOP)    // Set animation style
                        .setDuration(1500)                          // Set animation time
                        .setStartDelay(300)                         // Set the delay before the animation
                        .setHorizontalMultiple(2)                   // Set the amplitude of lateral movement, the default is 3
                        .setVerticalMultiple(2)                     // Set the vertical movement amplitude, the default is 4
                        .addAnimatorListener(new SmashAnimator.OnAnimatorListener() {
                            @Override
                            public void onAnimatorStart() {
                                super.onAnimatorStart();
                                // Callback, the animation starts
                                Toast.makeText(MainActivity.this, "onAnimatorStart "+view, Toast.LENGTH_SHORT).show();
                            }
  
                            @Override
                            public void onAnimatorEnd() {
                                super.onAnimatorEnd();
                                // Callback, the animation ends
                                Toast.makeText(MainActivity.this, "onAnimatorEnd "+view, Toast.LENGTH_SHORT).show();
                            }
                        }).start();
            }
        });
    }
}

输出: