# Android特效專輯(八)——實現心型起泡飛舞的特效,讓你的APP瞬間暖心
* * *
> 馬上也要放年假了,家里估計會沒網,更完這篇的話,可能要到年后了,不過在此期間會把更新內容都保存在本地,這樣有網就可以發表了,也是極好的,今天說的這個特效,原本是Only上的一個小彩蛋的,我們來看看圖片

> 只要我點擊了Only這個字,下面就開始上升起起泡了,這個實現起來其實就是一個欲蓋彌彰的動畫而已,準備好三張顏色不一樣的心型圖片咯,這樣的話,我們就開始動手來寫一寫吧!?
> **首先新建一個工程——HeartFaom**?
> 準備工作就是準備圖片咯
## BezierEvaluator
~~~
單位轉換以及計算軌跡
~~~
~~~
package com.lgl.heartfaom;
import android.animation.TypeEvaluator;
import android.graphics.PointF;
public class BezierEvaluator implements TypeEvaluator<PointF> {
private PointF pointF1;
private PointF pointF2;
public BezierEvaluator(PointF pointF1, PointF pointF2) {
this.pointF1 = pointF1;
this.pointF2 = pointF2;
}
@Override
public PointF evaluate(float time, PointF startValue, PointF endValue) {
float timeLeft = 1.0f - time;
PointF point = new PointF();// 結果
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;
}
}
~~~
## PeriscopeLayout
~~~
貝塞爾曲線的計算以及氣泡的實現
~~~
~~~
package com.lgl.heartfaom;
import java.util.Random;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.os.Build;
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;
public class PeriscopeLayout extends RelativeLayout {
private Interpolator line = new LinearInterpolator();// 線性
private Interpolator acc = new AccelerateInterpolator();// 加速
private Interpolator dce = new DecelerateInterpolator();// 減速
private Interpolator accdec = new AccelerateDecelerateInterpolator();// 先加速后減速
private Interpolator[] interpolators;
private int mHeight;
private int mWidth;
private LayoutParams lp;
private Drawable[] drawables;
private Random random = new Random();
private int dHeight;
private int dWidth;
public PeriscopeLayout(Context context) {
super(context);
init();
}
public PeriscopeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PeriscopeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public PeriscopeLayout(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
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;
// 獲取圖的寬高 用于后面的計算
// 注意 我這里3張圖片的大小都是一樣的,所以我只取了一個
dHeight = red.getIntrinsicHeight();
dWidth = red.getIntrinsicWidth();
// 底部 并且 水平居中
lp = new LayoutParams(dWidth, dHeight);
lp.addRule(CENTER_HORIZONTAL, TRUE);// 這里的TRUE 要注意 不是true
lp.addRule(ALIGN_PARENT_BOTTOM, TRUE);
// 初始化插補器
interpolators = new Interpolator[4];
interpolators[0] = line;
interpolators[1] = acc;
interpolators[2] = dce;
interpolators[3] = accdec;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
}
public void addHeart() {
ImageView imageView = new ImageView(getContext());
// 隨機選一個
imageView.setImageDrawable(drawables[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;
}
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 ValueAnimator getBezierValueAnimator(View target) {
// 初始化一個貝塞爾計算器- - 傳入
BezierEvaluator evaluator = new BezierEvaluator(getPointF(2),
getPointF(1));
// 這里最好畫個圖 理解一下 傳入了起點 和 終點
ValueAnimator animator = ValueAnimator.ofObject(evaluator, new PointF(
(mWidth - dWidth) / 2, mHeight - dHeight),
new PointF(random.nextInt(getWidth()), 0));
animator.addUpdateListener(new BezierListenr(target));
animator.setTarget(target);
animator.setDuration(3000);
return animator;
}
/**
* 獲取中間的兩個 點
*
* @param scale
*/
private PointF getPointF(int scale) {
PointF pointF = new PointF();
pointF.x = random.nextInt((mWidth - 100));// 減去100 是為了控制 x軸活動范圍,看效果 隨意~~
// 再Y軸上 為了確保第二個點 在第一個點之上,我把Y分成了上下兩半 這樣動畫效果好一些 也可以用其他方法
pointF.y = random.nextInt((mHeight - 100)) / scale;
return pointF;
}
private class BezierListenr implements ValueAnimator.AnimatorUpdateListener {
private View target;
public BezierListenr(View target) {
this.target = target;
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 這里獲取到貝塞爾曲線計算出來的的x y值 賦值給view 這樣就能讓愛心隨著曲線走啦
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;
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// 因為不停的add 導致子view數量只增不減,所以在view動畫結束后remove掉
removeView((target));
}
}
}
~~~
## 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"
android:background="#000" >
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="飛舞吧!" />
<com.lgl.heartfaom.PeriscopeLayout
android:id="@+id/periscope"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.lgl.heartfaom.PeriscopeLayout>
</RelativeLayout>
~~~
## MainActivity
~~~
接著就是怎么去使用它了
~~~
~~~
package com.lgl.heartfaom;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn_start;
// 心型氣泡
private PeriscopeLayout periscopeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化
periscopeLayout = (PeriscopeLayout) findViewById(R.id.periscope);
btn_start = (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 調用添加泡泡的方法
periscopeLayout.addHeart();
}
});
}
}
~~~
> 好,我們接下來就可以運行一下試試實際上的效果了

> 覺得不錯的點個贊哦!
## Demo下載地址:[http://download.csdn.net/detail/qq_26787115/9422603](http://download.csdn.net/detail/qq_26787115/9422603)
- 前言
- (一)——水波紋過渡特效(首頁)
- (二)——ViewPager渲染背景顏色漸變(引導頁)
- (三)——自定義不一樣的Toast
- (四)——APP主頁框架TabHost綁定ViewPager的替換者TabLayout
- (五)——自定義圓形頭像和仿MIUI卸載動畫—粒子爆炸
- (六)——仿QQ聊天撒花特效,無形裝逼,最為致命
- (七)——飛機升空特效,一鍵清理緩存,靈活運用動畫會有不一樣的感受
- (八)——實現心型起泡飛舞的特效,讓你的APP瞬間暖心
- (九)——仿微信雷達搜索好友特效,邏輯清晰實現簡單
- (十)——點擊水波紋效果實現,邏輯清晰實現簡單
- (十一)——仿水波紋流量球進度條控制器,實現高端大氣的主流特效
- (十二)——仿支付寶咻一咻功能實現波紋擴散特效,精細小巧的View