说起APP呢,每个人都有那么几款喜欢的并且经常使用的应用,我呢喜欢的应用有这么几个:QQ、酷狗、今日头条、百度贴吧等等。不仅仅是因为我经常使用它们,更重要的是我作为一名移动开发者认为它们做的很好,里面的一些效果经常会吸引我,它们会经常创新,使用户体验更好~好了,废话不多说(广告也不打了,他们老总又不给我钱)。说到QQ,前不久更新了一版其中的登录界面的背景不再是单调的一张图片了而是一个有很多漂亮妹子的视频在播放,说实话我看到的时候还挺耳目一新的,因为之前没见到过这样的APP,最多也就加个动画,好吧又扯了一大堆,来看看效果图吧!

Paste_Image.png

既然都说了那么多了,不妨在多写一点,雷军曾经说过要做米粉心中最酷的公司,我本人呢也是一个忠实的米粉。虽然腾讯不是我心中最酷的公司,但是他们家的应用QQ我还是很喜欢的,因为功能强大优化很好,就拿刚才的登录页面来说我就觉得很漂亮,(注意:切入正题~)最后我就想如果再在界面上加一些满天飞的彩色气泡那不就美炸了,哈哈哈,于是就无聊写着完了,顺便巩固一下属性动画的知识,再来放个效果图(git图太大,所以截的比较短)~,这里加个小的友情提示:下载一个QQ的APK包,把扩展名改成.zip格式然后解压出来就能找到视频图片表情等这些个资源文件了哦,一般人我不告诉他~

Paste_Image.png

好吧,又扯了这么多,下面该上点真东西了~照例先放个GitHub传送门:BalloonRelativeLayout
看到这样的一个效果,该如何去实现呢?有人就要说了,这TM不就是类似直播间点赞效果的实现吗?yeah,You are right ! ! ! 我只不过把点赞换成了气泡(机智如我)

总体思路和原理

1.自定义ViewGroup继承自RelativeLayout;
2.在自定义ViewGroup里添加一个个的view(气泡);
3.使view(气泡)沿着贝塞尔曲线的轨迹移动;复制代码

好的,总体大致思路就是这么多吧,更细节的东西继续往下看:

1.自定义ViewGroup继承自RelativeLayout:

此次我们主要是实现功能,不考虑太多的扩展性,就不自定义属性啦~

public class BalloonRelativeLayout extends RelativeLayout {public BalloonRelativeLayout(Context context) {this(context, null);}public BalloonRelativeLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);}public BalloonRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mContext = context;init();}//重写测量方法,获取宽高@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);mWidth = getMeasuredWidth();mHeight = getMeasuredHeight();}复制代码

接下来把目光指向我们的气泡君~
[1].先来加工一下我们的气泡view:这里我选择了三张不同的图片获取Drawable对象然后放到drawables数组里面备用;

private Drawable[] drawables;//初始化显示的图片
drawables = new Drawable[3];
Drawable mBalloon = ContextCompat.getDrawable(mContext, R.mipmap.balloon_pink);
Drawable mBalloon2 = ContextCompat.getDrawable(mContext, R.mipmap.balloon_purple);
Drawable mBalloon3 = ContextCompat.getDrawable(mContext, R.mipmap.balloon_blue);
drawables[0] = mBalloon;
drawables[1] = mBalloon2;
drawables[2] = mBalloon3;复制代码

[2].既然有气泡了,那么就得对气泡进行一些处理

private int mViewHeight = dip2px(getContext(), 50);//默认50dp
private LayoutParams layoutParams;//设置view宽高相等,默认都是50dp
layoutParams = new LayoutParams(mViewHeight, mViewHeight);
layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);复制代码

[3].为了让气泡的动画显得更自然和随机性,那么我们还需要两个东西,属性动画中的插值器Interpolator和随机数Random;

private Interpolator[] interpolators;//插值器数组
private Interpolator linearInterpolator = new LinearInterpolator();// 以常量速率改变
private Interpolator accelerateInterpolator = new AccelerateInterpolator();//加速
private Interpolator decelerateInterpolator = new DecelerateInterpolator();//减速
private Interpolator accelerateDecelerateInterpolator = new AccelerateDecelerateInterpolator();//先加速后减速// 初始化插值器
interpolators = new Interpolator[4];
interpolators[0] = linearInterpolator;
interpolators[1] = accelerateInterpolator;
interpolators[2] = decelerateInterpolator;
interpolators[3] = accelerateDecelerateInterpolator;复制代码

2.在自定义ViewGroup里添加一个个的view(气泡);

OK,气泡已经做好了,接下来就是要把气泡塞到我们的ViewGroup里了。

final ImageView imageView = new ImageView(getContext());//随机选一个imageView.setImageDrawable(drawables[random.nextInt(3)]);imageView.setLayoutParams(layoutParams);addView(imageView);//放进ViewGroupAnimator animator = getAnimator(imageView);animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);//view动画结束后remove掉removeView(imageView);}});animator.start();复制代码

上面代码主要分为三点:1.初始化一个气泡view;2.添加到ViewGroup;3.获取一个属性动画对象执行动画,在这个动画结束后把气泡从ViewGroup里removeView掉。

3.使view(气泡)沿着贝塞尔曲线的轨迹移动;

OK,来到最后一个步骤,这是重点也是难点,其实说难不难说易不易,这特么又是一句废话~,先来思考一下我们想要的效果,气泡从左下角飘出,然后按照曲线的轨迹向屏幕的顶端飘去,有的很快,有的很慢,有的快快慢慢~~~既然如此,我们先来把曲线的路径的坐标获取到吧,我们使用三阶贝塞尔曲线公式:关于贝塞尔曲线呢,它很神秘也很神奇,关于它不仅要学很长时间还要理解很长时间,就不多说了,在这只放一个公式,然后推荐一篇博客[Android:贝塞尔曲线原理分析]

Paste_Image.png

好的,来写我们自定义的插值器吧~

/*** 自定义插值器*/
class BezierEvaluator implements TypeEvaluator<PointF> {//两个控制点private PointF pointF1;private PointF pointF2;public BezierEvaluator(PointF pointF1, PointF pointF2) {this.pointF1 = pointF1;this.pointF2 = pointF2;}@Overridepublic PointF evaluate(float time, PointF startValue,PointF endValue) {float timeOn = 1.0f - time;PointF point = new PointF();//这么复杂的公式让我计算真心头疼,但是计算机很easypoint.x = timeOn * timeOn * timeOn * (startValue.x)+ 3 * timeOn * timeOn * time * (pointF1.x)+ 3 * timeOn * time * time * (pointF2.x)+ time * time * time * (endValue.x);point.y = timeOn * timeOn * timeOn * (startValue.y)+ 3 * timeOn * timeOn * time * (pointF1.y)+ 3 * timeOn * time * time * (pointF2.y)+ time * time * time * (endValue.y);//这里返回的是曲线上每一个点的坐标值return point;}
}复制代码

下面就来使用这个插值器吧,开始写我们的属性动画~

//初始化一个自定义的贝塞尔曲线插值器,并且传入控制点
BezierEvaluator evaluator = new BezierEvaluator(getPointF(), getPointF());
//传入了曲线起点(左下角)和终点(顶部随机)
ValueAnimator animator = ValueAnimator.ofObject(evaluator, new PointF(0, getHeight()), new PointF(random.nextInt(getWidth()), -mViewHeight));
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {//获取到贝塞尔曲线轨迹上的x和y值 赋值给viewPointF pointF = (PointF) animation.getAnimatedValue();target.setX(pointF.x);target.setY(pointF.y);}
});
animator.setTarget(target);
animator.setDuration(5000);复制代码

这里面牵涉了一个控制点的获取,我们是采用随机性的原则来获取ViewGroup上的任何一点的坐标来作为曲线的控制点~

/*** 自定义曲线的两个控制点,随机在ViewGroup上的任何一个位置*/
private PointF getPointF() {PointF pointF = new PointF();pointF.x = random.nextInt(mWidth);pointF.y = random.nextInt(mHeight);return pointF;
}复制代码

最后再来初始化一个AnimatorSet把刚才写好的贝塞尔曲线的属性动画放进去即可,最后把这个AnimatorSet赋给最开始时候的animator就大功告成了。

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(bezierValueAnimator);
animatorSet.setInterpolator(interpolators[random.nextInt(4)]);
animatorSet.setTarget(target);复制代码

最后在Activity中获取该ViewGroup,随便写一个定时器源源不断的往ViewGroup中添加气泡即可。

按照惯例,把全家福放上来~

BalloonRelativeLayout.java

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;import java.util.Random;/*** Created by zhuyong on 2017/7/19.*/public class BalloonRelativeLayout extends RelativeLayout {private Context mContext;private Interpolator[] interpolators;//插值器数组private Interpolator linearInterpolator = new LinearInterpolator();// 以常量速率改变private Interpolator accelerateInterpolator = new AccelerateInterpolator();//加速private Interpolator decelerateInterpolator = new DecelerateInterpolator();//减速private Interpolator accelerateDecelerateInterpolator = new AccelerateDecelerateInterpolator();//先加速后减速private LayoutParams layoutParams;private int mHeight;private int mWidth;private Random random = new Random();//初始化随机数类private int mViewHeight = dip2px(getContext(), 50);//默认50dpprivate Drawable[] drawables;public BalloonRelativeLayout(Context context) {this(context, null);}public BalloonRelativeLayout(Context context, AttributeSet attrs) {this(context, attrs, 0);}public BalloonRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mContext = context;init();}private void init() {//初始化显示的图片drawables = new Drawable[3];Drawable mBalloon = ContextCompat.getDrawable(mContext, R.mipmap.balloon_pink);Drawable mBalloon2 = ContextCompat.getDrawable(mContext, R.mipmap.balloon_purple);Drawable mBalloon3 = ContextCompat.getDrawable(mContext, R.mipmap.balloon_blue);drawables[0] = mBalloon;drawables[1] = mBalloon2;drawables[2] = mBalloon3;//设置view宽高相等,默认都是50dplayoutParams = new LayoutParams(mViewHeight, mViewHeight);layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);// 初始化插值器interpolators = new Interpolator[4];interpolators[0] = linearInterpolator;interpolators[1] = accelerateInterpolator;interpolators[2] = decelerateInterpolator;interpolators[3] = accelerateDecelerateInterpolator;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);mWidth = getMeasuredWidth();mHeight = getMeasuredHeight();}public void addBalloon() {final ImageView imageView = new ImageView(getContext());//随机选一个imageView.setImageDrawable(drawables[random.nextInt(3)]);imageView.setLayoutParams(layoutParams);addView(imageView);Animator animator = getAnimator(imageView);animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);//view动画结束后remove掉removeView(imageView);}});animator.start();}private Animator getAnimator(View target) {ValueAnimator bezierValueAnimator = getBezierValueAnimator(target);AnimatorSet animatorSet = new AnimatorSet();animatorSet.playSequentially(bezierValueAnimator);animatorSet.setInterpolator(interpolators[random.nextInt(4)]);animatorSet.setTarget(target);return animatorSet;}private ValueAnimator getBezierValueAnimator(final View target) {//初始化一个自定义的贝塞尔曲线插值器,并且传入控制点BezierEvaluator evaluator = new BezierEvaluator(getPointF(), getPointF());//传入了曲线起点(左下角)和终点(顶部随机)ValueAnimator animator = ValueAnimator.ofObject(evaluator, new PointF(0, getHeight()), new PointF(random.nextInt(getWidth()), -mViewHeight));animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {//获取到贝塞尔曲线轨迹上的x和y值 赋值给viewPointF pointF = (PointF) animation.getAnimatedValue();target.setX(pointF.x);target.setY(pointF.y);}});animator.setTarget(target);animator.setDuration(5000);return animator;}/*** 自定义曲线的两个控制点,随机在ViewGroup上的任何一个位置*/private PointF getPointF() {PointF pointF = new PointF();pointF.x = random.nextInt(mWidth);pointF.y = random.nextInt(mHeight);return pointF;}/*** 自定义插值器*/class BezierEvaluator implements TypeEvaluator<PointF> {//途径的两个点private PointF pointF1;private PointF pointF2;public BezierEvaluator(PointF pointF1, PointF pointF2) {this.pointF1 = pointF1;this.pointF2 = pointF2;}@Overridepublic PointF evaluate(float time, PointF startValue,PointF endValue) {float timeOn = 1.0f - time;PointF point = new PointF();//这么复杂的公式让我计算真心头疼,但是计算机很easypoint.x = timeOn * timeOn * timeOn * (startValue.x)+ 3 * timeOn * timeOn * time * (pointF1.x)+ 3 * timeOn * time * time * (pointF2.x)+ time * time * time * (endValue.x);point.y = timeOn * timeOn * timeOn * (startValue.y)+ 3 * timeOn * timeOn * time * (pointF1.y)+ 3 * timeOn * time * time * (pointF2.y)+ time * time * time * (endValue.y);return point;}}/*** Dip into pixels*/public static int dip2px(Context context, float dpValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}
}复制代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.zhuyong.balloonrelativelayout.BalloonRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/balloonRelativeLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center"><!--播放视频--><com.zhuyong.balloonrelativelayout.CustomVideoViewandroid:id="@+id/videoView"android:layout_width="match_parent"android:layout_height="match_parent"android:clickable="false"android:focusable="false"android:focusableInTouchMode="false" /></com.zhuyong.balloonrelativelayout.BalloonRelativeLayout>复制代码

MainActivity.java

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.VideoView;public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {private BalloonRelativeLayout mBalloonRelativeLayout;private VideoView mVideoView;private int TIME = 100;//这里默认每隔100毫秒添加一个气泡Handler mHandler = new Handler();Runnable runnable = new Runnable() {@Overridepublic void run() {// handler自带方法实现定时器try {mHandler.postDelayed(this, TIME);mBalloonRelativeLayout.addBalloon();} catch (Exception e) {e.printStackTrace();}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//取消状态栏getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(R.layout.activity_main);mVideoView = (VideoView) findViewById(R.id.videoView);mBalloonRelativeLayout = (BalloonRelativeLayout) findViewById(R.id.balloonRelativeLayout);initVideoView();}private void initVideoView() {//设置屏幕常亮getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.mqr));//设置相关的监听mVideoView.setOnPreparedListener(this);mVideoView.setOnCompletionListener(this);}//播放准备@Overridepublic void onPrepared(MediaPlayer mp) {//开始播放mVideoView.start();mHandler.postDelayed(runnable, TIME);}//播放结束@Overridepublic void onCompletion(MediaPlayer mp) {//开始播放mVideoView.start();}
}复制代码

既然说了是全家福了,就把自定义VideoView也放进来把~主要是解决不能全屏显示的问题~

CustomVideoView.java

import android.content.Context;
import android.util.AttributeSet;
import android.widget.VideoView;/*** Created by zhuyong on 2017/7/20.* 自定义VideoView解决全屏问题*/
public class CustomVideoView extends VideoView {public CustomVideoView(Context context) {this(context, null);}public CustomVideoView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public CustomVideoView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = getDefaultSize(0, widthMeasureSpec);int height = getDefaultSize(0, heightMeasureSpec);setMeasuredDimension(width, height);}
}复制代码

demo地址:

github.com/SuperKotlin…

如果你觉得此文对您有所帮助,欢迎入群 QQ交流群 : 232203809
微信公众号:终端研发部

技术+职场

高仿QQ视频加点炫酷特效—你这样玩过吗相关推荐

  1. android 自定义特效,Android自定义View之高仿QQ健康

    我们都知道自定义View一般有三种直接继承View.继承原有的控件对控件的进行修改.重新拼装组合,最后一种主要针对于ViewGroup.具体的怎么做不是本文的所涉及的内容(本文是基于第一种方式实现的) ...

  2. 华为手机线刷工具_原来华为手机自带视频剪辑工具!简单操作几步,就能添加炫酷特效...

    短视频是两年很火的社交手段之一,不管是在微博还是微信朋友圈了,很多朋友都喜欢使用视频来表达自己的动态. 有些朋友为了让自己的视频更加好看,就给视频添加字幕.音乐.动画等元素,但这些操作都要使用第三方工 ...

  3. Android开发之高仿QQ消息侧拉删除

    Android开发之高仿QQ消息侧拉删除 QQ消息的侧滑删除效果之炫酷,想必大家都见过吧,本人作为一名安卓开发人员,遇到如此炫酷的效果,怎能不研究一番呢,现本人已实现其基本功能,现将代码贴出,望各位大 ...

  4. 【Android】史上最简单,一步集成侧滑(删除)菜单,高仿QQ、IOS。

    本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 转载请标明出处: http://blog.csdn.net/zxt0601/article/details/53157090 本文出 ...

  5. Flutter高仿微信-视频演示

    Flutter高仿微信系列共59篇,从Flutter客户端.Kotlin客户端.Web服务器.数据库表结构.Xmpp即时通讯服务器.视频通话服务器.腾讯云服务器全面讲解. 详情请查看 请看视频演示: ...

  6. 38.鼠标移动炫酷特效

    效果 (源码网盘地址在最后) 视频演示 「前端编程实战 38」HTML+CSS3 实现鼠标移动炫酷特效 视频演示地址一:https://www.bilibili.com/video/BV1Xz411v ...

  7. android+高仿视频录制,android高仿微信视频编辑页

    android高仿微信视频编辑页-视频多张图片提取 上一篇中介绍了有关视频提取图片的知识点,如果对这个不太了解 建议看下android提取视频多张图片和视频信息之前这篇. 这里实现的是仿微信的视频编辑 ...

  8. 高仿QQ即时聊天软件开发系列之三登录窗口用户选择下拉框

    上一篇高仿QQ即时聊天软件开发系列之二登录窗口界面写了一个大概的布局和原理 这一篇详细说下拉框的实现原理 先上最终效果图 一开始其实只是想给下拉框加一个placeholder效果,让下拉框在未选择未输 ...

  9. Android实现高仿QQ附近的人搜索展示

    本文主要实现了高仿QQ附近的人搜索展示,用到了自定义控件的方法 最终效果如下 1.下面展示列表我们可以使用ViewPager来实现(当然如果你不觉得麻烦,你也可以用HorizontalScrollVi ...

最新文章

  1. openGL第四讲——像素格式管理
  2. Spring Boot Admin 2 值得了解的新变化
  3. 读取记事本内容,自动发布到新浪微博
  4. 1359C. Mixing Water
  5. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 23丨学生们参加各科测试的次数【难度简单】​
  6. 【J2SE】java实现简单照片查看器
  7. 珍珠全面屏!华为畅享9S/9e正式发布:千元三摄加持
  8. centos7 mysql 数据库备份与还原
  9. 三星 9810 android 9,【极光ROM】-【三星NOTE9 N960X-9810】-【V22.0 Android-Q-TK1】
  10. 挖洞手记——软媒魔方可信任程序执行漏洞
  11. boundvalueops和opsforvalue区别
  12. 易福门电感式接近开关IGS200
  13. 实战三:手把手教你实现物体识别
  14. 极佳mysql修复数据库修复_极佳SQL数据库修复工具下载
  15. 程序员上了年纪可以做啥?
  16. Daemontools简介
  17. 如何免费压缩PDF文件,白嫖党必看
  18. 【嵌入式系统】独立看门狗原理+看门狗实验分析
  19. php中高级培训,php高级培训【货币问答】- php高级培训所有答案 - 联合货币
  20. 【华为机试题分析-C/C++】知识点分类总结

热门文章

  1. 全球最具收藏艺术家-唐忠球专题报道
  2. 【IIS】无法识别的属性“targetFramework”。请注意属性名称区分大小写。
  3. 设置charles代理后电脑浏览器与手机模拟器都无法连网
  4. 【数据分析实例】1,337,000 条深圳通刷卡记录数据分析
  5. BLE Mesh (10) —— Mesh Provisioning
  6. elementui 日期选择快捷设置
  7. 自然语言处理的21个基本概念
  8. 如何使用SpringBoot写一个属于自己的Starter
  9. {}如何利用邮件进行推广
  10. 【洁洁送书第一期】Python高并发与高性能编程: 原理与实践