亲宝软件园·资讯

展开

Android粒子效果 Android自定义UI之粒子效果

qq_1113502097 人气:0
想了解Android自定义UI之粒子效果的相关内容吗,qq_1113502097在本文为您仔细讲解Android粒子效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,UI,粒子效果,下面大家一起来学习吧。

1.爆炸实体类

public class Ball {

  public int color;//颜色
  public float x;//圆心x坐标
  public float y;//圆心y坐标
  public float r;//粒子半径

  public float vX;//粒子水平方向速度
  public float vY;//粒子y方向速度
  public float aX;//粒子水平方向加速度
  public float ay;//粒子y方向加速度
}

2.自定义SplitView类

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class SplitView extends View {

  private Paint mPaint;
  private Bitmap mBitmap;
  private float d=3;//粒子直径
  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= BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background);
    for (int i=0;i<mBitmap.getWidth();i++){
      for (int j =0;j<mBitmap.getHeight();j++){
        Ball ball=new Ball();
        ball.color=mBitmap.getPixel(i,j);
        ball.x=i*d+d/2;
        ball.y=j*d+d/2;
        ball.r=d/2;

        //速度(-20,20)
        ball.vX=(float)(Math.pow(-1,Math.ceil(Math.random()*1000))*20*Math.random());
        ball.vY=rangInt(-15,35);

        ball.aX=0;
        ball.ay=0.98f;
        mBalls.add(ball);
      }
    }
    mAnimator=ValueAnimator.ofFloat(0,1);
    mAnimator.setRepeatCount(-1);//重复次数无限
    mAnimator.setDuration(2000);//重复时间
    mAnimator.setInterpolator(new LinearInterpolator());
    mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        upDateBall();
        invalidate();//重新调用onMeasure,ondraw方法。
      }
    });
  }

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

  private void upDateBall() {
    for (Ball ball:mBalls){
      ball.x=ball.x+ball.vX;
      ball.y=ball.y+ball.vY;
      ball.vX+=ball.aX;
      ball.vY+=ball.ay;

    }

  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.translate(500,500);
    for (Ball ball:mBalls){
      mPaint.setColor(ball.color);
      canvas.drawCircle(ball.x,ball.y,ball.r,mPaint);
    }
  }

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

     //触发动画
     mAnimator.start();
   }
    return super.onTouchEvent(event);
  }
}

加载全部内容

相关教程
猜你喜欢
用户评论