實(shí)現(xiàn)愛(ài)心滿天飛的效果

最近需要完成這樣的一個(gè)效果:

sdasd.gif

看上去就是動(dòng)畫(huà)跟貝塞爾曲線的組合驰徊,但是因?yàn)閯?dòng)畫(huà)不熟悉侥袜,所以比較難以實(shí)現(xiàn),這里的代碼是摘取至 hcc大神的分享

LoveLikeLayout:

public class LoveLikeLayout extends RelativeLayout {

    //線性插值器
    private Interpolator line = new LinearInterpolator();

    //加速插值器
    private Interpolator acc = new AccelerateInterpolator();

    //減速插值器
    private Interpolator dce = new DecelerateInterpolator();

    //先加速后減速插值器
    private Interpolator accdec = new AccelerateDecelerateInterpolator();

    //插值器數(shù)組
    private Interpolator[] interpolators;

    //隨機(jī)數(shù)
    private Random mRandom = new Random();

    //愛(ài)心圖片數(shù)組
    private Drawable[] drawables;

    //愛(ài)心圖片的高度
    private int drawableHeight;

    //愛(ài)心圖片的寬度
    private int drawableWidth;

    //參數(shù)值
    private LayoutParams layoutParams;

    //layout高度
    private int measuredHeight;

    //layout寬度
    private int measuredWidth;

    public LoveLikeLayout(Context context) {
        this(context, null);
    }

    public LoveLikeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoveLikeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        //初始化愛(ài)心圖片
        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;

        //獲取愛(ài)心的寬高
        drawableHeight = red.getIntrinsicHeight();
        drawableWidth = red.getIntrinsicWidth();

        //設(shè)置愛(ài)心的顯示位置
        layoutParams = new LayoutParams(drawableWidth, drawableHeight);
        layoutParams.addRule(CENTER_HORIZONTAL, TRUE);
        layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);

        //初始化動(dòng)畫(huà)插值器
        interpolators = new Interpolator[4];
        interpolators[0] = line;
        interpolators[1] = acc;
        interpolators[2] = dce;
        interpolators[3] = accdec;
    }

    /**
     * 測(cè)量layout的寬高
     * 用于計(jì)算愛(ài)心的顯示位置
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measuredHeight = getMeasuredHeight();
        measuredWidth = getMeasuredWidth();
    }

    /**
     * 顯示愛(ài)心
     */
    public void addLove() {
        final ImageView imageView = new ImageView(getContext());
        //隨機(jī)生成愛(ài)心顏色
        imageView.setImageDrawable(drawables[mRandom.nextInt(3)]);
        imageView.setLayoutParams(layoutParams);

        addView(imageView);
        Animator animtor = getAnimtor(imageView);
        animtor.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                removeView(imageView);
                super.onAnimationEnd(animation);
            }
        });
        animtor.start();
    }


    /**
     * 愛(ài)心的顯示和運(yùn)行軌跡動(dòng)畫(huà)組合實(shí)現(xiàn)
     *
     * @param target
     * @return
     */
    private Animator getAnimtor(View target) {
        AnimatorSet enterAnimtorSet = getEnterAnimtorSet(target);
        ValueAnimator bezierAnimtor = getBezierAnimtor(target);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playSequentially(enterAnimtorSet);
        animatorSet.playSequentially(enterAnimtorSet, bezierAnimtor);
        animatorSet.setInterpolator(interpolators[mRandom.nextInt(4)]);
        animatorSet.setTarget(target);

        return animatorSet;
    }


    /**
     * 愛(ài)心的顯示動(dòng)畫(huà)實(shí)現(xiàn)
     *
     * @param target
     * @return
     */
    private AnimatorSet getEnterAnimtorSet(View target) {
        //愛(ài)心的3中動(dòng)畫(huà)組合 透明度 x,y軸的縮放
        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);

        //組合動(dòng)畫(huà)
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        set.setInterpolator(new LinearInterpolator());
        set.playTogether(alpha, scaleX, scaleY);
        set.setTarget(target);

        return set;
    }


    /**
     * 愛(ài)心運(yùn)動(dòng)軌跡的動(dòng)畫(huà)實(shí)現(xiàn)
     *
     * @param target
     * @return
     */
    private ValueAnimator getBezierAnimtor(final View target) {
        BezierEvaluator evaluator = new BezierEvaluator(getPoint(2), getPoint(1));

        ValueAnimator animator = ValueAnimator.ofObject(evaluator,
                new PointF((measuredWidth - drawableWidth) / 2, measuredHeight - drawableHeight),
                new PointF(mRandom.nextInt(getWidth()), 0));


        animator.setDuration(3000);
        animator.setTarget(target);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //獲取貝塞爾曲線的運(yùn)動(dòng)軌跡 讓愛(ài)心跟隨著移動(dòng)
                PointF animatedValue = (PointF) valueAnimator.getAnimatedValue();
                target.setX(animatedValue.x);
                target.setY(animatedValue.y);
                //增加透明度的變化
                target.setAlpha(1 - valueAnimator.getAnimatedFraction());
            }
        });

        return animator;
    }

    /**
     * 獲取中間的兩個(gè)點(diǎn)
     *
     * @return
     */
    private PointF getPoint(int scale) {
        PointF pointF = new PointF();
        pointF.x = mRandom.nextInt((measuredWidth - 100));
        pointF.y = mRandom.nextInt((measuredHeight - 100)) / scale;
        return pointF;
    }
}

BezierEvaluator:

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();//結(jié)果

        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;
    }
}

用法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#80000000"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.stay4it.widgets.ui.LoveLikeLayout
        android:id="@+id/id_LoveLikeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="點(diǎn)擊" />
</LinearLayout>


public class LoveActivity extends AppCompatActivity {
    private LoveLikeLayout mLoveLikeLayout;
    private Button btn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love);
        mLoveLikeLayout = (LoveLikeLayout) findViewById(R.id.id_LoveLikeLayout);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLoveLikeLayout.addLove();
            }
        });
    }

先把代碼摘錄下來(lái)祥诽,后面再研究。瓮恭。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末雄坪,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌维哈,老刑警劉巖绳姨,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異阔挠,居然都是意外死亡飘庄,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)购撼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)跪削,“玉大人,你說(shuō)我怎么就攤上這事迂求∧胙危” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵揩局,是天一觀的道長(zhǎng)毫玖。 經(jīng)常有香客問(wèn)我,道長(zhǎng)凌盯,這世上最難降的妖魔是什么付枫? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮驰怎,結(jié)果婚禮上阐滩,老公的妹妹穿的比我還像新娘。我一直安慰自己砸西,他們只是感情好叶眉,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著芹枷,像睡著了一般衅疙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上鸳慈,一...
    開(kāi)封第一講書(shū)人閱讀 52,262評(píng)論 1 308
  • 那天饱溢,我揣著相機(jī)與錄音,去河邊找鬼走芋。 笑死绩郎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的翁逞。 我是一名探鬼主播肋杖,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼挖函!你這毒婦竟也來(lái)了状植?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎津畸,沒(méi)想到半個(gè)月后振定,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡肉拓,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年后频,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片暖途。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡卑惜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出丧肴,到底是詐尸還是另有隱情残揉,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布芋浮,位于F島的核電站,受9級(jí)特大地震影響壳快,放射性物質(zhì)發(fā)生泄漏纸巷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一眶痰、第九天 我趴在偏房一處隱蔽的房頂上張望瘤旨。 院中可真熱鬧,春花似錦竖伯、人聲如沸存哲。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)祟偷。三九已至,卻和暖如春打厘,著一層夾襖步出監(jiān)牢的瞬間修肠,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工户盯, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留嵌施,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓莽鸭,卻偏偏與公主長(zhǎng)得像吗伤,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子硫眨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容