本文主要是介绍安卓直播送礼物的爱心动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
贝塞尔曲线
import android.animation.TypeEvaluator;
import android.graphics.PointF;/*** Created by peng on 2017/11/13.*/public class BezierEvaluator2 implements TypeEvaluator<PointF> {private PointF pointF1;private PointF pointF2;public BezierEvaluator2(PointF pointF1, PointF pointF2) {this.pointF1 = pointF1;this.pointF2 = pointF2;}@Overridepublic PointF evaluate(float time, PointF startValue, PointF endValue) {//创建一个PointF对象PointF point = new PointF();float timeLeft = 1 - time;point.x = timeLeft * timeLeft * timeLeft * (startValue.x)+ 3 * timeLeft * timeLeft * time * (pointF1.x)+ 3 * timeLeft * time * time * (pointF2.x)+ time * time * time * (endValue.x);point.y = timeLeft * timeLeft * timeLeft * (startValue.y)+ 3 * timeLeft * timeLeft * time * (pointF1.y)+ 3 * timeLeft * time * time * (pointF2.y)+ time * time * time * (endValue.y);return point;}
}
//动画爱心类
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
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 com.sxs.huoshanvideo.R;import java.util.Random;/*** Created by peng on 2017/11/12.compile 'me.yifeiyuan.periscopelayout:library:1.0.0'*/public class FavorLayout extends RelativeLayout {private int mHeight;//FavorLayout的高度private int mWidth;//FavorLayout的宽度private Drawable[] drawables;private int dHeight;private int dWidth;private Interpolator line = new LinearInterpolator();private Interpolator acc = new AccelerateInterpolator();private Interpolator dce = new DecelerateInterpolator();private Interpolator accdec = new AccelerateDecelerateInterpolator();private Interpolator interpolators[];private LayoutParams lp;private Random random = new Random();public FavorLayout(Context context) {this(context, null);}public FavorLayout(Context context, AttributeSet attrs) {super(context, attrs);init();}private void init() {drawables = new Drawable[3];Drawable red = getResources().getDrawable(R.drawable.pl_red);Drawable yellow = getResources().getDrawable(R.drawable.pl_yellow);Drawable blue = getResources().getDrawable(R.drawable.pl_blue);drawables[0] = red;drawables[1] = yellow;drawables[2] = blue;dHeight = red.getIntrinsicHeight();dWidth = red.getIntrinsicWidth();lp = new LayoutParams(dWidth, dHeight);lp.addRule(CENTER_HORIZONTAL, TRUE);lp.addRule(ALIGN_PARENT_BOTTOM, TRUE);//初始化插补器interpolators = new Interpolator[4];interpolators[0] = line;interpolators[1] = acc;interpolators[2] = dce;interpolators[3] = accdec;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);mWidth = getMeasuredHeight();mHeight = getMeasuredWidth();}public void addHeart() {ImageView imageView = new ImageView(getContext());imageView.setImageDrawable(drawables[new Random().nextInt(3)]);imageView.setLayoutParams(lp);addView(imageView);Animator set = getAnimator(imageView);set.addListener(new AnimEndListener(imageView));set.start();}private Animator getAnimator(View target) {AnimatorSet set = getEnterAnimtor(target);ValueAnimator bezierValueAnimator = getBezierValueAnimator(target);AnimatorSet finalSet = new AnimatorSet();finalSet.playSequentially(set);finalSet.playSequentially(set, bezierValueAnimator);finalSet.setInterpolator(interpolators[random.nextInt(4)]);finalSet.setTarget(target);return finalSet;}/*** 让心形走贝塞尔曲线的动画* @param target* @return*/private ValueAnimator getBezierValueAnimator(View target) {//贝塞尔曲线BezierEvaluator2 bezierEvaluator = new BezierEvaluator2(getPointF(2), getPointF(1));ValueAnimator valueAnimator = ValueAnimator.ofObject(bezierEvaluator, new PointF((mWidth - dWidth) / 2,mHeight - dHeight), new PointF(random.nextInt(getWidth()), 0));valueAnimator.addUpdateListener(new BezierListenr(target));valueAnimator.setTarget(target);valueAnimator.setDuration(3000);return valueAnimator;}/*** 给布局添加心形的动画* @param target* @return*/private AnimatorSet getEnterAnimtor(final View target) {ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 0.2f, 1f);ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, 0.2f, 1f);ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, 0.2f, 1f);AnimatorSet enter = new AnimatorSet();enter.setDuration(500);enter.setInterpolator(new LinearInterpolator());enter.playTogether(alpha, scaleX, scaleY);enter.setTarget(target);return enter;}private PointF getPointF(int scale) {PointF pointF = new PointF();pointF.x = random.nextInt((mWidth));//减去100 是为了控制 x轴活动范围,看效果 随意~~//再Y轴上 为了确保第二个点 在第一个点之上,我把Y分成了上下两半 这样动画效果好一些 也可以用其他方法pointF.y = random.nextInt((mHeight));return pointF;}private class BezierListenr implements ValueAnimator.AnimatorUpdateListener {private View target;public BezierListenr(View target) {this.target = target;}@Overridepublic void onAnimationUpdate(ValueAnimator animation) {PointF pointF = (PointF) animation.getAnimatedValue();target.setX(pointF.x);target.setY(pointF.y);// 这里顺便做一个alpha动画target.setAlpha(1 - animation.getAnimatedFraction());}}private class AnimEndListener extends AnimatorListenerAdapter {private View target;public AnimEndListener(View target) {this.target = target;}@Overridepublic void onAnimationEnd(Animator animation) {super.onAnimationEnd(animation);//因为不停的add 导致子view数量只增不减,所以在view动画结束后remove掉removeView((target));}}
}
在布局中应用:
<com.sxs.huoshanvideo.widgt.widget.FavorLayoutandroid:id="@+id/fly"android:layout_marginTop="550px"android:layout_marginLeft="350px"android:layout_width="400px"android:layout_height="500px"></com.sxs.huoshanvideo.widgt.widget.FavorLayout>
在Activity中:
final FavorLayout fl = (FavorLayout) findViewById(R.id.fl);fl.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {fl.addHeart();}});
这篇关于安卓直播送礼物的爱心动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!