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

先看看效果图吧

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

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", 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);
 }

 /**
 * 跟新小球的位置
 *
 * @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!

(0)

相关推荐

  • android上传图片到PHP的过程详解

    今天在做上传头像的时候,总是提交连接超时错误,报错信息如下:XXXXXXSokcetTimeOutXXXXXXXX 然后自己设置HTTP的超时时间: 复制代码 代码如下: [java] view plaincopyprint? //设置超时时间  httpclient.setTimeout(20000); 再building,runing,还是不行....这就怪了,明明好好的,怎么会突然就变成连接超时了呢!又折腾了一阵子后,也跟后台那边的朋友沟通过,他也测试了上传接口,发现没什么问题,就让我自己

  • Android应用中拍照后获取照片路径并上传的实例分享

    Activity 中的代码,我只贴出重要的事件部分代码 public void doPhoto(View view) { destoryBimap(); String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); s

  • Android仿QQ空间主页面的实现

    今天模仿安卓QQ空间,效果如下:    打开程序的启动画面和导航页面我就不做了,大家可以模仿微信的那个做一下,很简单.这次主要做一下主页面的实现,下面是主页面的布局: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l

  • Android ScrollView滑动实现仿QQ空间标题栏渐变

    今天来研究的是ScrollView-滚动视图,滚动视图又分横向滚动视图(HorizontalScrollView)和纵向滚动视图(ScrollView),今天主要研究纵向的.相信大家在开发中经常用到,ScrollView的功能已经很强大了,但是仍然满足不了我们脑洞大开的UI设计师们,所以我们要自定义-本篇文章主要讲监听ScrollView的滑动实现仿QQ空间标题栏渐变,先看一下效果图: 好了我们切入主题. 有可能你不知道的那些ScrollView属性  •android:scrollbars 设

  • Android编程实现仿QQ发表说说,上传照片及弹出框效果【附demo源码下载】

    本文实例讲述了Android编程实现仿QQ发表说说,上传照片及弹出框效果.分享给大家供大家参考,具体如下: 代码很简单,主要就是几个动画而已,图标什么的就随便找了几个,效果图:   动画说明: 1.点击右上角按钮,菜单从顶部下拉弹出,同时背景变暗; 2.再次点击右上角按钮,点击返回键,或者点击空白区域(也就是变暗的部分),菜单向上收回; 3.点击菜单上的按钮响应事件,同时菜单收回(效果同2) 重要说明:动画结束后必须clearAnimation,否则隐藏状态的view依然能响应点击事件 主体代码

  • Android中自定义PopupWindow实现弹出框并带有动画效果

    使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { public Lost lost; public void onLost(Lost lost){ this.lost = lost; } private View conentView; public View getConentView() { return conentView; } public L

  • Android仿QQ空间底部菜单示例代码

    之前曾经在网上看到Android仿QQ空间底部菜单的Demo,发现这个Demo有很多Bug,布局用了很多神秘数字.于是研究了一下QQ空间底部菜单的实现,自己写了一个,供大家参考.效果如下图所示:   1.实现原理很简单,底部菜单是一个水平分布的LinearLayout,里面又是五个LinearLayout,它们的layout_weight都为1,意味着底部菜单的子控件将屏幕宽度平均分为5部分.五个LinearLayout除了中间那个,其余都在里面放置ImageView和TextView(中间先空

  • Android实现本地上传图片并设置为圆形头像

    先从本地把图片上传到服务器,然后根据URL把头像处理成圆形头像. 因为上传图片用到bmob的平台,所以要到bmob(http://www.bmob.cn)申请密钥. 效果图: 核心代码: 复制代码 代码如下: public class MainActivity extends Activity {         private ImageView iv;         private String appKey="";                //填写你的Applicatio

  • Android使用post方式上传图片到服务器的方法

    本文实例讲述了Android使用post方式上传图片到服务器的方法.分享给大家供大家参考,具体如下: /** * 上传文件到服务器类 * * @author tom */ public class UploadUtil { private static final String TAG = "uploadFile"; private static final int TIME_OUT = 10 * 1000; // 超时时间 private static final String CH

  • android文件上传示例分享(android图片上传)

    主要思路是调用系统文件管理器或者其他媒体采集资源来获取要上传的文件,然后将文件的上传进度实时展示到进度条中. 主Activity 复制代码 代码如下: package com.guotop.elearn.activity.app.yunpan.activity; import java.io.File;import java.io.FileNotFoundException;import java.io.IOException; import android.app.Activity;impor

  • Android仿QQ好友列表分组实现增删改及持久化

    Android自带的控件ExpandableListView实现了分组列表功能,本案例在此基础上进行优化,为此控件添加增删改分组及子项的功能,以及列表数据的持久化. Demo实现效果: GroupListDemo具体实现: ①demo中将列表页面设计为Fragment页面,方便后期调用:在主界面MainActivity中动态添加GroupListFragment页面: MainActivity.java package com.eric.grouplistdemo; import android

  • Android仿QQ滑动弹出菜单标记已读、未读消息

    在上一篇<Android仿微信滑动弹出编辑.删除菜单效果.增加下拉刷新功能>里,已经带着大家学习如何使用SwipeMenuListView这一开源库实现滑动列表弹出菜单,接下来,将进一步学习,如何为不同的list item呈现不同的菜单,此处我们做一个实例:Android 高仿QQ滑动弹出菜单标记已读.未读消息,看下效果图: 1. 创建项目,并导入SwipeMenuListView类库 2. 创建消息实体bean: public class Msg { public int id; publi

  • Android仿QQ登陆窗口实现原理

    今天根据腾讯qq,我们做一个练习,来学习如何制作一个漂亮的布局.首先看一下官方图片 还是一个启动画面,之后进入登录页面,导航页面就不介绍了,大家可以参考微信的导航页面.首先程序进入SplashActivity,就是启动页面,Activity代码如下: 复制代码 代码如下: package com.example.imitateqq; import android.app.Activity; import android.content.Intent; import android.os.Bund

随机推荐