博主聲明:
轉(zhuǎn)載請(qǐng)?jiān)陂_頭附加本文鏈接及作者信息,并標(biāo)記為轉(zhuǎn)載。本文由博主 威威喵 原創(chuàng),請(qǐng)多支持與指教。
本文首發(fā)于此 博主:威威喵 | 博客主頁(yè):https://blog.csdn.net/smile_running
上一次寫了一篇關(guān)于愛心點(diǎn)贊曲線動(dòng)畫效果的文章,查看請(qǐng)點(diǎn)擊:貝塞爾曲線(Bezier)之愛心點(diǎn)贊曲線動(dòng)畫效果。其實(shí)現(xiàn)的效果也太差強(qiáng)人意了,不過(guò)作為小試牛刀,我們算是對(duì)貝塞爾曲線有了一個(gè)認(rèn)識(shí),這前面一篇的鋪墊下,我們繼續(xù)對(duì)其展示的效果進(jìn)行加強(qiáng),所以就有了這樣的效果。
我們先來(lái)看看這次實(shí)現(xiàn)的效果圖:

其實(shí),這個(gè)效果也和上一篇那個(gè)差不了太多,但是實(shí)現(xiàn)的代碼就區(qū)別大了。上一次我使用的是純自定義 View 的方式,在執(zhí)行動(dòng)畫的時(shí)候自己開的子線程去改變它的坐標(biāo)值,雖然效果也還行,但是這種方式并不可取,而且我的貝塞爾曲線的路徑每設(shè)置隨機(jī)數(shù),它都往同一個(gè)方向走,效果不佳,與上面做一個(gè)對(duì)比。

來(lái)看一下這一次效果的實(shí)現(xiàn)思路,首先要繼承自父容器,我們將 ImageView 的位置設(shè)置到父容器的底部中間區(qū)域,代碼如下:
public void addLoveView() {
ImageView loveImageView = new ImageView(getContext());
Bitmap loveBitmap = mLoveBitmapList.get(mRandom.nextInt(mLoveBitmapList.size()));
loveImageView.setImageBitmap(loveBitmap);
//設(shè)置 ImageView 的位置
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(ALIGN_PARENT_BOTTOM);
params.addRule(CENTER_HORIZONTAL);
loveImageView.setLayoutParams(params);
//添加到 LoveBezierView 中,其位置是在容器底部
addView(loveImageView);
}
接著就是點(diǎn)擊屏幕就會(huì)添加一個(gè)愛心圖片,并且這個(gè)愛心圖片的動(dòng)畫效果是透明的和縮放的,代碼如下:
//愛心出現(xiàn)的動(dòng)畫
AnimatorSet startAnimator = new AnimatorSet();
ObjectAnimator alpha = ObjectAnimator.ofFloat(loveImageView, "alpha", 0.2f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(loveImageView, "scaleX", 0.2f, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(loveImageView, "scaleY", 0.2f, 1f);
startAnimator.playTogether(alpha, scaleX, scaleY);
startAnimator.setDuration(500);
這上面都沒什么難度,我就一筆帶過(guò)了。關(guān)鍵來(lái)看貝塞爾曲線路徑的計(jì)算方式。首先,來(lái)看看我畫的這張圖:
從上面的圖中我們看以看出,橙色的就是愛心的曲線路徑,在上面我標(biāo)注了 p1 p2 p3 p4 這四個(gè)點(diǎn),它是我們實(shí)現(xiàn)這個(gè)效果最關(guān)鍵的一步。從這四個(gè)點(diǎn)的位置看,很容易就可以計(jì)算出它的坐標(biāo)。
p4 點(diǎn) x 坐標(biāo)是控件的寬度,其值是隨機(jī)的,并且要相應(yīng)的減去圖片的寬度,y 坐標(biāo)就是 0
p3 點(diǎn) x 同上,y 坐標(biāo)是控件高度的一半,也是隨機(jī)值
p2 點(diǎn) x 同上,y 坐標(biāo)是控件高度的一半固定值,然后再加上高度的一半的隨機(jī)值
p1 點(diǎn) x 是固定的,控件寬度 /2 減去圖片的寬度 /2,y坐標(biāo)就是控件高度減去圖片高度
好了,已經(jīng)對(duì)這四個(gè)點(diǎn)的坐標(biāo)計(jì)算出來(lái)了,接下來(lái)就是將這四個(gè)點(diǎn)的坐標(biāo)套入三階貝塞爾曲線公式里面,公式如下:

這個(gè)應(yīng)該誰(shuí)都會(huì)吧,不過(guò)要在哪里使用這個(gè)公式,這才是問(wèn)題的關(guān)鍵。這里直接說(shuō)明,我們要實(shí)現(xiàn)一個(gè) TypeEvalutor 接口,它需要一個(gè)泛型,那個(gè)我們就傳入一個(gè) Point 類,通過(guò)套入貝塞爾曲線公式的計(jì)算,我們就可以得到曲線上的每一個(gè)點(diǎn),然后它會(huì)返回給我們,注意:這里傳入的是 p2 p3 控制點(diǎn),代碼如下:
public class LoveBezierTypeEvaluator implements TypeEvaluator<PointF> {
private PointF p2;
private PointF p3;
public LoveBezierTypeEvaluator(PointF p2, PointF p3) {
this.p2 = p2;
this.p3 = p3;
}
@Override
public PointF evaluate(float t, PointF p1, PointF p4) {
PointF point = new PointF();
point.x = (float) (p1.x * Math.pow(1 - t, 3) +
3 * p2.x * t * Math.pow(1 - t, 2) +
3 * p3.x * Math.pow(t, 2) * (1 - t) +
p4.x * Math.pow(t, 3));
point.y = (float) (p1.y * Math.pow(1 - t, 3) +
3 * p2.y * t * Math.pow(1 - t, 2) +
3 * p3.y * Math.pow(t, 2) * (1 - t) +
p4.y * Math.pow(t, 3));
return point;
}
}
最后通過(guò)屬性動(dòng)畫,我們?yōu)槠涮砑右粋€(gè)貝塞爾曲線的效果,那么傳入的值就是我們剛剛所計(jì)算出來(lái)的四個(gè)點(diǎn),代碼如下:
private void startAnimationSet(ImageView loveImageView) {
//存放所有動(dòng)畫,包括開始動(dòng)畫和 bezier 動(dòng)畫
AnimatorSet AllAnimatorSet = new AnimatorSet();
//愛心出現(xiàn)的動(dòng)畫
AnimatorSet startAnimator = new AnimatorSet();
ObjectAnimator alpha = ObjectAnimator.ofFloat(loveImageView, "alpha", 0.2f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(loveImageView, "scaleX", 0.2f, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(loveImageView, "scaleY", 0.2f, 1f);
startAnimator.playTogether(alpha, scaleX, scaleY);
startAnimator.setDuration(500);
//計(jì)算四個(gè)點(diǎn)的坐標(biāo)
point1 = new PointF(mWidth / 2 - mLoveWidth / 2, mHeight - mLoveHeight);
point2 = new PointF(mRandom.nextInt(mWidth) - mLoveWidth, mHeight / 2 + mRandom.nextInt(mHeight / 2));
point3 = new PointF(mRandom.nextInt(mWidth) - mLoveWidth, mRandom.nextInt(mHeight / 2));
point4 = new PointF(mRandom.nextInt(mWidth) - mLoveWidth, 0);
TypeEvaluator loveBezierTypeEvaluator = new LoveBezierTypeEvaluator(point2, point3);
ValueAnimator bezierAnimator = ObjectAnimator.ofObject(loveBezierTypeEvaluator, point1, point4);
bezierAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF point = (PointF) animation.getAnimatedValue();
loveImageView.setX(point.x);
loveImageView.setY(point.y);
}
});
bezierAnimator.setDuration(5000);
AllAnimatorSet.playSequentially(startAnimator, bezierAnimator);
AllAnimatorSet.start();
}
這樣就可以看到我們的曲線動(dòng)畫效果,上面的代碼是將兩個(gè)動(dòng)畫放在一起順序的執(zhí)行,效果如下圖:

那么至此,我們就簡(jiǎn)單的實(shí)現(xiàn)了這個(gè)貝塞爾曲線點(diǎn)贊的動(dòng)畫效果了,不過(guò)這樣的話,這個(gè)愛心都飄到了上面,我們應(yīng)該進(jìn)一步對(duì)其進(jìn)行優(yōu)化,讓愛心逐漸消失才行,那樣效果更佳。
做法就是監(jiān)聽動(dòng)畫事件,等到動(dòng)畫結(jié)束時(shí)候,直接移除 ImageView 即可,直接上代碼:
AllAnimatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
removeView(loveImageView);
}
});
那么這樣做的話,效果好多了,看看吧

到這里的話,好像每一個(gè)愛心的移動(dòng)速率都是一樣的,感覺有那么一丟丟死板,好吧,那我們就給它搞幾個(gè)插值器不就可以里,我這里添加了幾個(gè),然后隨機(jī)的設(shè)置一個(gè),代碼如下:
mInterpolator = new Interpolator[]{
new AccelerateInterpolator(), new AccelerateDecelerateInterpolator(), new DecelerateInterpolator(), new LinearInterpolator()
};
bezierAnimator.setInterpolator(mInterpolator[mRandom.nextInt(mInterpolator.length)]);
這下不會(huì)太死板了,有的快,有的慢,效果好多了吧

好吧,整個(gè)效果的完整代碼如下:
package nd.no.xww.qqmessagedragview;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
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.ArrayList;
import java.util.List;
import java.util.Random;
/**
* @author xww
* @desciption : 貝塞爾曲線的鮮花飄飄動(dòng)畫效果
* @date 2019/8/3
* @time 10:34
* 博主:威威喵
* 博客:https://blog.csdn.net/smile_Running
*/
public class LoveBezierView extends RelativeLayout {
private int mLoveWidth;
private int mLoveHeight;
private int mWidth;
private int mHeight;
private Random mRandom;
private Bitmap mLoveBitmap;
private int[] mLoveId = new int[]{
R.drawable.love1,
R.drawable.love2,
R.drawable.love3,
R.drawable.love4,
R.drawable.love5,
R.drawable.love6
};
private List<Bitmap> mLoveBitmapList;
private Interpolator[] mInterpolator;
private void init() {
mRandom = new Random();
mLoveBitmapList = new ArrayList<>();
setLoveSize(150, 150);
mInterpolator = new Interpolator[]{
new AccelerateInterpolator(), new AccelerateDecelerateInterpolator(), new DecelerateInterpolator(), new LinearInterpolator()
};
}
public LoveBezierView(Context context) {
this(context, null);
}
public LoveBezierView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LoveBezierView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = MeasureSpec.getSize(widthMeasureSpec);
mHeight = MeasureSpec.getSize(heightMeasureSpec);
}
/**
* 要設(shè)置圖片的大小,必須在 addLoveView 之前調(diào)用
*
* @param width 寬
* @param height 高
*/
public void setLoveSize(int width, int height) {
mLoveBitmapList.clear();
for (int i = 0; i < mLoveId.length; i++) {
mLoveBitmap = BitmapFactory.decodeResource(getResources(), mLoveId[i]);
mLoveBitmap = Bitmap.createScaledBitmap(mLoveBitmap, width, height, false);
mLoveBitmapList.add(mLoveBitmap);
}
//獲取圖片的寬度
mLoveWidth = mLoveBitmap.getWidth();
mLoveHeight = mLoveBitmap.getHeight();
}
public void addLoveView() {
ImageView loveImageView = new ImageView(getContext());
Bitmap loveBitmap = mLoveBitmapList.get(mRandom.nextInt(mLoveBitmapList.size()));
loveImageView.setImageBitmap(loveBitmap);
//設(shè)置 ImageView 的位置
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(ALIGN_PARENT_BOTTOM);
params.addRule(CENTER_HORIZONTAL);
loveImageView.setLayoutParams(params);
//添加到 LoveBezierView 中,其位置是在容器底部
addView(loveImageView);
//開始動(dòng)畫集合
startAnimationSet(loveImageView);
}
private PointF point1;
private PointF point2;
private PointF point3;
private PointF point4;
private void startAnimationSet(ImageView loveImageView) {
//存放所有動(dòng)畫,包括開始動(dòng)畫和 bezier 動(dòng)畫
AnimatorSet AllAnimatorSet = new AnimatorSet();
//愛心出現(xiàn)的動(dòng)畫
AnimatorSet startAnimator = new AnimatorSet();
ObjectAnimator alpha = ObjectAnimator.ofFloat(loveImageView, "alpha", 0.2f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(loveImageView, "scaleX", 0.2f, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(loveImageView, "scaleY", 0.2f, 1f);
startAnimator.playTogether(alpha, scaleX, scaleY);
startAnimator.setDuration(500);
//計(jì)算四個(gè)點(diǎn)的坐標(biāo)
point1 = new PointF(mWidth / 2 - mLoveWidth / 2, mHeight - mLoveHeight);
point2 = new PointF(mRandom.nextInt(mWidth) - mLoveWidth, mHeight / 2 + mRandom.nextInt(mHeight / 2));
point3 = new PointF(mRandom.nextInt(mWidth) - mLoveWidth, mRandom.nextInt(mHeight / 2));
point4 = new PointF(mRandom.nextInt(mWidth) - mLoveWidth, 0);
TypeEvaluator loveBezierTypeEvaluator = new LoveBezierTypeEvaluator(point2, point3);
ValueAnimator bezierAnimator = ObjectAnimator.ofObject(loveBezierTypeEvaluator, point1, point4);
bezierAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF point = (PointF) animation.getAnimatedValue();
loveImageView.setX(point.x);
loveImageView.setY(point.y);
}
});
bezierAnimator.setDuration(5000);
bezierAnimator.setInterpolator(mInterpolator[mRandom.nextInt(mInterpolator.length)]);
AllAnimatorSet.playSequentially(startAnimator, bezierAnimator);
AllAnimatorSet.start();
AllAnimatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
removeView(loveImageView);
}
});
}
public class LoveBezierTypeEvaluator implements TypeEvaluator<PointF> {
private PointF p2;
private PointF p3;
public LoveBezierTypeEvaluator(PointF p2, PointF p3) {
this.p2 = p2;
this.p3 = p3;
}
@Override
public PointF evaluate(float t, PointF p1, PointF p4) {
PointF point = new PointF();
point.x = (float) (p1.x * Math.pow(1 - t, 3) +
3 * p2.x * t * Math.pow(1 - t, 2) +
3 * p3.x * Math.pow(t, 2) * (1 - t) +
p4.x * Math.pow(t, 3));
point.y = (float) (p1.y * Math.pow(1 - t, 3) +
3 * p2.y * t * Math.pow(1 - t, 2) +
3 * p3.y * Math.pow(t, 2) * (1 - t) +
p4.y * Math.pow(t, 3));
return point;
}
}
}
使用方式就很簡(jiǎn)單了,如下:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
loveView.setLoveSize(120,120);
loveView.addLoveView();
break;
}
return super.onTouchEvent(event);
}
首先綁定控件的 id ,然后設(shè)置一下愛心圖片的大小,最后調(diào)用 addLoveView() 即可。你可以在主頁(yè)面設(shè)置一個(gè) Button,通過(guò)按鈕控制愛心的添加,這都不是事。