文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android仿QQ聊天撒花特效 很真实

2022-06-06 08:31

关注

先看看效果图吧

实现这样的效果,你要知道贝塞尔曲线,何谓贝塞尔曲线?先在这里打个问号
下面就直接写了

1.activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
 //撒花的区域
 <RelativeLayout
 android:id="@+id/rlt_animation_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent" >
 </RelativeLayout>
 <Button
 android:id="@+id/btn_start"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="23dp"
 android:text="开始撒花" />
</RelativeLayout>

2.Fllower

传参类


package com.lgl.test;
import android.graphics.Bitmap;
import android.graphics.Path;
import java.io.Serializable;
public class Fllower implements Serializable {
 private static final long serialVersionUID = 1L;
 private Bitmap image;
 private float x;
 private float y;
 private Path path;
 private float value;
 public Bitmap getResId() {
 return image;
 }
 public void setResId(Bitmap img) {
 this.image = img;
 }
 public float getX() {
 return x;
 }
 public void setX(float x) {
 this.x = x;
 }
 public float getY() {
 return y;
 }
 public void setY(float y) {
 this.y = y;
 }
 public Path getPath() {
 return path;
 }
 public void setPath(Path path) {
 this.path = path;
 }
 public float getValue() {
 return value;
 }
 public void setValue(float value) {
 this.value = value;
 }
 @Override
 public String toString() {
 return "Fllower [ x=" + x + ", y=" + y + ", path=" + path + ", value="
  + value + "]";
 }
}

3.FllowerAnimation

动画类


package com.lgl.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;

public class FllowerAnimation extends View implements AnimatorUpdateListener {
 
 private float phase1 = 0f;
 private float phase2 = 0f;
 private float phase3 = 0f;
 
 private List<Fllower> fllowers1 = new ArrayList<Fllower>();
 private List<Fllower> fllowers2 = new ArrayList<Fllower>();
 private List<Fllower> fllowers3 = new ArrayList<Fllower>();
 
 private int time = 4000;
 
 private int delay = 400;
 int[] ylocations = { -100, -50, -25, 0 };
 
 // private int resId = R.drawable.fllower_love;
 public FllowerAnimation(Context context) {
 super(context);
 init(context);
 // this.resId = resId;
 }
 @SuppressWarnings("deprecation")
 private void init(Context context) {
 WindowManager wm = (WindowManager) context
  .getSystemService(Context.WINDOW_SERVICE);
 width = wm.getDefaultDisplay().getWidth();
 height = (int) (wm.getDefaultDisplay().getHeight() * 3 / 2f);
 mPaint = new Paint();
 mPaint.setAntiAlias(true);
 // mPaint.setStrokeWidth(2);
 // mPaint.setColor(Color.BLUE);
 // mPaint.setStyle(Style.STROKE);
 pathMeasure = new PathMeasure();
 builderFollower(fllowerCount, fllowers1);
 builderFollower(fllowerCount, fllowers2);
 builderFollower(fllowerCount, fllowers3);
 }
 
 private int width = 0;
 
 private int height = 0;
 
 private int quadCount = 10;
 
 private float intensity = 0.2f;
 
 private int fllowerCount = 4;
 
 private void builderFollower(int count, List<Fllower> fllowers) {
 int max = (int) (width * 3 / 4f);
 int min = (int) (width / 4f);
 Random random = new Random();
 for (int i = 0; i < count; i++) {
  int s = random.nextInt(max) % (max - min + 1) + min;
  Path path = new Path();
  CPoint CPoint = new CPoint(s, ylocations[random.nextInt(3)]);
  List<CPoint> points = builderPath(CPoint);
  drawFllowerPath(path, points);
  Fllower fllower = new Fllower();
  fllower.setPath(path);
  Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
   R.drawable.lift_flower);
  fllower.setResId(bitmap);
  fllowers.add(fllower);
 }
 }
 
 private void drawFllowerPath(Path path, List<CPoint> points) {
 if (points.size() > 1) {
  for (int j = 0; j < points.size(); j++) {
  CPoint point = points.get(j);
  if (j == 0) {
   CPoint next = points.get(j + 1);
   point.dx = ((next.x - point.x) * intensity);
   point.dy = ((next.y - point.y) * intensity);
  } else if (j == points.size() - 1) {
   CPoint prev = points.get(j - 1);
   point.dx = ((point.x - prev.x) * intensity);
   point.dy = ((point.y - prev.y) * intensity);
  } else {
   CPoint next = points.get(j + 1);
   CPoint prev = points.get(j - 1);
   point.dx = ((next.x - prev.x) * intensity);
   point.dy = ((next.y - prev.y) * intensity);
  }
  // create the cubic-spline path
  if (j == 0) {
   path.moveTo(point.x, point.y);
  } else {
   CPoint prev = points.get(j - 1);
   path.cubicTo(prev.x + prev.dx, (prev.y + prev.dy), point.x
    - point.dx, (point.y - point.dy), point.x, point.y);
  }
  }
 }
 }
 
 private int range = (int) TypedValue
  .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, getResources()
   .getDisplayMetrics());
 
 private List<CPoint> builderPath(CPoint point) {
 List<CPoint> points = new ArrayList<CPoint>();
 Random random = new Random();
 for (int i = 0; i < quadCount; i++) {
  if (i == 0) {
  points.add(point);
  } else {
  CPoint tmp = new CPoint(0, 0);
  if (random.nextInt(100) % 2 == 0) {
   tmp.x = point.x + random.nextInt(range);
  } else {
   tmp.x = point.x - random.nextInt(range);
  }
  tmp.y = (int) (height / (float) quadCount * i);
  points.add(tmp);
  }
 }
 return points;
 }
 
 private Paint mPaint;
 
 private PathMeasure pathMeasure = null;
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 drawFllower(canvas, fllowers1);
 drawFllower(canvas, fllowers2);
 drawFllower(canvas, fllowers3);
 }
 
 private float dy = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
  40, getResources().getDisplayMetrics());
 
 private void drawFllower(Canvas canvas, List<Fllower> fllowers) {
 for (Fllower fllower : fllowers) {
  float[] pos = new float[2];
  // canvas.drawPath(fllower.getPath(),mPaint);
  pathMeasure.setPath(fllower.getPath(), false);
  pathMeasure.getPosTan(height * fllower.getValue(), pos, null);
  // canvas.drawCircle(pos[0], pos[1], 10, mPaint);
  canvas.drawBitmap(fllower.getResId(), pos[0], pos[1] - dy, null);
 }
 }
 ObjectAnimator mAnimator1;
 ObjectAnimator mAnimator2;
 ObjectAnimator mAnimator3;
 public void startAnimation() {
 if (mAnimator1 != null && mAnimator1.isRunning()) {
  mAnimator1.cancel();
 }
 mAnimator1 = ObjectAnimator.ofFloat(this, "phase1", 0f, 1f);
 mAnimator1.setDuration(time);
 mAnimator1.addUpdateListener(this);
 mAnimator1.start();
 mAnimator1.setInterpolator(new AccelerateInterpolator(1f));
 if (mAnimator2 != null && mAnimator2.isRunning()) {
  mAnimator2.cancel();
 }
 mAnimator2 = ObjectAnimator.ofFloat(this, "phase2", 0f, 1f);
 mAnimator2.setDuration(time);
 mAnimator2.addUpdateListener(this);
 mAnimator2.start();
 mAnimator2.setInterpolator(new AccelerateInterpolator(1f));
 mAnimator2.setStartDelay(delay);
 if (mAnimator3 != null && mAnimator3.isRunning()) {
  mAnimator3.cancel();
 }
 mAnimator3 = ObjectAnimator.ofFloat(this, "phase3", 0f, 1f);
 mAnimator3.setDuration(time);
 mAnimator3.addUpdateListener(this);
 mAnimator3.start();
 mAnimator3.setInterpolator(new AccelerateInterpolator(1f));
 mAnimator3.setStartDelay(delay * 2);
 }
 
 private void updateValue(float value, List<Fllower> fllowers) {
 for (Fllower fllower : fllowers) {
  fllower.setValue(value);
 }
 }
 
 @Override
 public void onAnimationUpdate(ValueAnimator arg0) {
 updateValue(getPhase1(), fllowers1);
 updateValue(getPhase2(), fllowers2);
 updateValue(getPhase3(), fllowers3);
 Log.i(tag, getPhase1() + "");
 invalidate();
 }
 public float getPhase1() {
 return phase1;
 }
 public void setPhase1(float phase1) {
 this.phase1 = phase1;
 }
 public float getPhase2() {
 return phase2;
 }
 public void setPhase2(float phase2) {
 this.phase2 = phase2;
 }
 public float getPhase3() {
 return phase3;
 }
 public void setPhase3(float phase3) {
 this.phase3 = phase3;
 }
 private String tag = this.getClass().getSimpleName();
 private class CPoint {
 public float x = 0f;
 public float y = 0f;
 
 public float dx = 0f;
 
 public float dy = 0f;
 public CPoint(float x, float y) {
  this.x = x;
  this.y = y;
 }
 }
}

4.MainActivity

接着就看我们使用


package com.lgl.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
 private Button btn_start;
 // 撒花特效
 private RelativeLayout rlt_animation_layout;
 private FllowerAnimation fllowerAnimation;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 // 撒花初始化
 rlt_animation_layout = (RelativeLayout) findViewById(R.id.rlt_animation_layout);
 rlt_animation_layout.setVisibility(View.VISIBLE);
 fllowerAnimation = new FllowerAnimation(this);
 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
  RelativeLayout.LayoutParams.MATCH_PARENT,
  RelativeLayout.LayoutParams.MATCH_PARENT);
 fllowerAnimation.setLayoutParams(params);
 rlt_animation_layout.addView(fllowerAnimation);
 btn_start = (Button) findViewById(R.id.btn_start);
 btn_start.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
  // 开始撒花
  fllowerAnimation.startAnimation();
  }
 });
 }
}

好,我们现在来看看效果

源码下砸:Android仿QQ聊天撒花特效

好的,你也赶快去试一下吧!大家可以制作类似的雪花飘落效果等,Try!

您可能感兴趣的文章:Android仿QQ登陆窗口实现原理Android仿QQ空间底部菜单示例代码Android仿QQ空间主页面的实现Android ScrollView滑动实现仿QQ空间栏渐变Android仿QQ好友列表分组实现增删改及持久化Android仿QQ滑动弹出菜单标记已读、未读消息Android应用中拍照后获取照片路径并上传的实例分享android文件上传示例分享(android图片上传)Android实现本地上传图片并设置为圆形头像Android使用post方式上传图片到服务器的方法android上传图片到PHP的过程详解Android中自定义PopupWindow实现弹出框并带有动画效果Android编程实现仿QQ发表说说,上传照片及弹出框效果【附demo源码下载】


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯