这篇文章给大家分享的是有关android如何实现直播点赞飘心动画效果的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
前段时间在写直播的时候,需要观众在看直播的时候点赞的效果,在此参照了腾讯大神写的点赞(飘心动画效果)。下面是效果图:
1.自定义飘心动画的属性
在attrs.xml 中增加自定义的属性
<!-- 飘心动画自定义的属性 --> <declare-styleable name="HeartLayout"> <attr name="initX" format="dimension"/> <attr name="initY" format="dimension"/> <attr name="xRand" format="dimension"/> <attr name="animLengthRand" format="dimension"/> <attr name="xPointFactor" format="dimension"/> <attr name="animLength" format="dimension"/> <attr name="heart_width" format="dimension"/> <attr name="heart_height" format="dimension"/> <attr name="bezierFactor" format="integer"/> <attr name="anim_duration" format="integer"/> </declare-styleable>2.定义飘心默认值
2.1 dimens.xml
<!-- 飘星 --> <dimen name="heart_anim_bezier_x_rand">50.0dp</dimen> <dimen name="heart_anim_init_x">50.0dp</dimen> <dimen name="heart_anim_init_y">25.0dp</dimen> <dimen name="heart_anim_length">400.0dp</dimen> <dimen name="heart_anim_length_rand">350.0dp</dimen> <dimen name="heart_anim_x_point_factor">30.0dp</dimen> <dimen name="heart_size_height">27.3dp</dimen> <dimen name="heart_size_width">32.5dp</dimen>2.2 integers.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <integer name="heart_anim_bezier_factor">6</integer> <integer name="anim_duration">3000</integer> </resources>3.定义飘心动画控制器
3.1 AbstractPathAnimator.java
public abstract class AbstractPathAnimator { private final Random mRandom; protected final Config mConfig; public AbstractPathAnimator(Config config) { mConfig = config; mRandom = new Random(); } public float randomRotation() { return mRandom.nextFloat() * 28.6F - 14.3F; } public Path createPath(AtomicInteger counter, View view, int factor) { Random r = mRandom; int x = r.nextInt(mConfig.xRand); int x2 = r.nextInt(mConfig.xRand); int y = view.getHeight() - mConfig.initY; int y2 = counter.intValue() * 15 + mConfig.animLength * factor + r.nextInt(mConfig.animLengthRand); factor = y2 / mConfig.bezierFactor; x = mConfig.xPointFactor + x; x2 = mConfig.xPointFactor + x2; int y3 = y - y2; y2 = y - y2 / 2; Path p = new Path(); p.moveTo(mConfig.initX, y); p.cubicTo(mConfig.initX, y - factor, x, y2 + factor, x, y2); p.moveTo(x, y2); p.cubicTo(x, y2 - factor, x2, y3 + factor, x2, y3); return p; } public abstract void start(View child, ViewGroup parent); public static class Config { public int initX; public int initY; public int xRand; public int animLengthRand; public int bezierFactor; public int xPointFactor; public int animLength; public int heartWidth; public int heartHeight; public int animDuration; static public Config fromTypeArray(TypedArray typedArray, float x, float y, int pointx, int heartWidth, int heartHeight) { Config config = new Config(); Resources res = typedArray.getResources(); config.initX = (int) typedArray.getDimension(R.styleable.HeartLayout_initX, x); config.initY = (int) typedArray.getDimension(R.styleable.HeartLayout_initY, y); config.xRand = (int) typedArray.getDimension(R.styleable.HeartLayout_xRand, res.getDimensionPixelOffset(R.dimen.heart_anim_bezier_x_rand)); config.animLength = (int) typedArray.getDimension(R.styleable.HeartLayout_animLength, res.getDimensionPixelOffset(R.dimen.heart_anim_length));//动画长度 config.animLengthRand = (int) typedArray.getDimension(R.styleable.HeartLayout_animLengthRand, res.getDimensionPixelOffset(R.dimen.heart_anim_length_rand)); config.bezierFactor = typedArray.getInteger(R.styleable.HeartLayout_bezierFactor, res.getInteger(R.integer.heart_anim_bezier_factor)); config.xPointFactor = pointx; // config.heartWidth = (int) typedArray.getDimension(R.styleable.HeartLayout_heart_width, // res.getDimensionPixelOffset(R.dimen.heart_size_width));//动画图片宽度 // config.heartHeight = (int) typedArray.getDimension(R.styleable.HeartLayout_heart_height, // res.getDimensionPixelOffset(R.dimen.heart_size_height));//动画图片高度 config.heartWidth = heartWidth; config.heartHeight = heartHeight; config.animDuration = typedArray.getInteger(R.styleable.HeartLayout_anim_duration, res.getInteger(R.integer.anim_duration));//持续期 return config; } } }郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。