SoFunction
Updated on 2025-03-11

Android custom UI particle effect

This article shares the specific code of Android custom UI particle effect for your reference. The specific content is as follows

1. Explosive entity class

public class Ball {

  public int color;//color  public float x;//Center x coordinate  public float y;//Center y coordinate  public float r;//Particle Radius
  public float vX;//Horizontal velocity of particles  public float vY;//Particle y-direction velocity  public float aX;//The horizontal acceleration of the particle  public float ay;//The acceleration of the particle y direction}

2. Customize the SplitView class

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

import ;
import ;

public class SplitView extends View {

  private Paint mPaint;
  private Bitmap mBitmap;
  private float d=3;//Particle diameter  private ValueAnimator mAnimator;
  private List<Ball> mBalls=new ArrayList<>();
  public SplitView(Context context) {
    super(context);
  }

  public SplitView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public SplitView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    mPaint=new Paint();
    mBitmap= (getResources(),.ic_launcher_background);
    for (int i=0;i<();i++){
      for (int j =0;j<();j++){
        Ball ball=new Ball();
        =(i,j);
        =i*d+d/2;
        =j*d+d/2;
        =d/2;

        //Speed ​​(-20, 20)        =(float)((-1,(()*1000))*20*());
        =rangInt(-15,35);

        =0;
        =0.98f;
        (ball);
      }
    }
    mAnimator=(0,1);
    (-1);//Unlimited repetitions    (2000);//Repeat time    (new LinearInterpolator());
    (new () {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        upDateBall();
        invalidate();//Recall the onMeasure, ondraw method.      }
    });
  }

  private int rangInt(int x,int y){
    int max=(x,y);
    int min=(x,y);
    return (int)(min+(()*(max-min)));
  }

  private void upDateBall() {
    for (Ball ball:mBalls){
      =+;
      =+;
      +=;
      +=;

    }

  }

  @Override
  protected void onDraw(Canvas canvas) {
    (canvas);
    (500,500);
    for (Ball ball:mBalls){
      ();
      (,,,mPaint);
    }
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
   if (()==MotionEvent.ACTION_DOWN)
   {

     //Trigger animation     ();
   }
    return (event);
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.