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

Android仿QQ聊天撒花特效 很真实,第1张

概述先看看效果图吧实现这样的效果,你要知道贝塞尔曲线,何谓贝塞尔曲线?先在这里打个问号

先看看效果图吧

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

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;/** * 撒花 用到的知识点: 1、androID属性动画 2、Path路径绘制 3、贝塞尔曲线 */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 }; /** * 资源ID */ // 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); } } /** * 画曲线 * * @param path * @param points */ 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()); /** * 画路径 * * @param point * @return */ 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()); /** * @param canvas * @param fllowers */ 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",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",1f); mAnimator3.setDuration(time); mAnimator3.addUpdateListener(this); mAnimator3.start(); mAnimator3.setInterpolator(new AccelerateInterpolator(1f)); mAnimator3.setStartDelay(delay * 2); } /** * 跟新小球的位置 * * @param value * @param fllowers */ 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; /**  * x-axis distance  */ public float dx = 0f; /**  * y-axis distance  */ 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聊天撒花特效 很真实所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149269.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存