Android自定義View實(shí)現(xiàn)抖音飄動紅心效果
本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)抖音飄動紅心效果的具體代碼,供大家參考,具體內(nèi)容如下
自定義View——抖音飄動紅心
效果展示
動畫效果
使用自定義view完成紅心飄動效果
View實(shí)現(xiàn)
動畫:屬性動畫(位移+縮放+透明度+旋轉(zhuǎn))
+
隨機(jī)數(shù):(屬性動畫參數(shù)+顏色選?。?/p>
View
/** * 飄心效果 * 1.創(chuàng)建ImageView * 2.ImageView執(zhí)行組合動畫 * 3.動畫執(zhí)行完成后銷毀View */ public class FlyHeartView extends RelativeLayout { private int defoutWidth = 200;//默認(rèn)控件寬度 private long mDuration = 3000;//默認(rèn)動畫時(shí)間 //顏色集合 從中獲取顏色 private int[] color = { 0xFFFF34B3, 0xFF9ACD32, 0xFF9400D3, 0xFFEE9A00, 0xFFFFB6C1, 0xFFDA70D6, 0xFF8B008B, 0xFF4B0082, 0xFF483D8B, 0xFF1E90FF, 0xFF00BFFF, 0xFF00FF7F }; public FlyHeartView(Context context) { super(context); initFrameLayout(); } public FlyHeartView(Context context, AttributeSet attrs) { super(context, attrs); initFrameLayout(); } private void initFrameLayout() { ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(defoutWidth, ViewGroup.LayoutParams.WRAP_CONTENT); setLayoutParams(params); } /** * 創(chuàng)建一個(gè)心形的view視圖 */ private ImageView createHeartView() { ImageView heartIv = new ImageView(getContext()); LayoutParams params = new LayoutParams(defoutWidth / 2, defoutWidth / 2); //控件位置 params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); params.addRule(RelativeLayout.CENTER_HORIZONTAL); heartIv.setLayoutParams(params); heartIv.setImageResource(R.mipmap.ic_heart); //改變顏色 heartIv.setImageTintList(ColorStateList.valueOf(color[(int) (color.length * Math.random())])); return heartIv; } /** * 執(zhí)行動畫 * 在展示調(diào)用該方法 */ public void startFly() { final ImageView heartIv = createHeartView(); addView(heartIv); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(createTranslationX(heartIv)) .with(createTranslationY(heartIv)) .with(createScale(heartIv)) .with(createRotation(heartIv)) .with(createAlpha(heartIv)); //執(zhí)行動畫 animatorSet.start(); //銷毀view animatorSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); removeView(heartIv); } }); } /** * 橫向正弦位移動畫 * * @return */ private Animator createTranslationX(View view) { ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0, (float) (defoutWidth * Math.random() / 4)); animator.setDuration(mDuration); //CycleInterpolator cycles 正弦曲線數(shù) animator.setInterpolator(new CycleInterpolator((float) (3 * Math.random()))); return animator; } /** * 縱向加速位移動畫 * * @return */ private Animator createTranslationY(View view) { ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, -1000); animator.setDuration(mDuration); animator.setInterpolator(new AccelerateInterpolator()); return animator; } /** * 加速放大動畫 * * @return */ private Animator createScale(View view) { ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", 1, 1.5f); ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", 1, 1.5f); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.setDuration(mDuration); animatorSet.setInterpolator(new AccelerateInterpolator()); animatorSet.play(animatorX).with(animatorY); return animatorSet; } /** * 透明度動畫 * * @return */ private Animator createAlpha(View view) { ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1, 0.1f); animator.setDuration(mDuration); animator.setInterpolator(new AccelerateInterpolator()); return animator; } /** * 旋轉(zhuǎn)動畫 * * @return */ private Animator createRotation(View view) { ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0, (float) (25f * Math.random())); animator.setDuration(mDuration); animator.setInterpolator(new CycleInterpolator((float) (6 * Math.random()))); return animator; } }
最后在MainActivity中調(diào)用FlyHeartView 的startFly()方法就能實(shí)現(xiàn)點(diǎn)擊飄心效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android OkHttp實(shí)現(xiàn)全局過期token自動刷新示例
本篇文章主要介紹了Android OkHttp實(shí)現(xiàn)全局過期token自動刷新示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03關(guān)于Kotlin委托你必須重視的幾個(gè)點(diǎn)
委托模式已經(jīng)被證明是實(shí)現(xiàn)繼承的一個(gè)很好的替代方式,下面這篇文章主要給大家介紹了關(guān)于Kotlin委托你必須重視的幾個(gè)點(diǎn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01Android使用ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除和拖拽
這篇文章主要為大家詳細(xì)介紹了Android使用ItemTouchHelper實(shí)現(xiàn)側(cè)滑刪除和拖拽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08詳解Android權(quán)限管理之Android 6.0運(yùn)行時(shí)權(quán)限及解決辦法
本篇文章主要介紹Android權(quán)限管理之Android 6.0運(yùn)行時(shí)權(quán)限及解決辦法,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-11-11Android tabLayout+recyclerView實(shí)現(xiàn)錨點(diǎn)定位的示例
這篇文章主要介紹了Android tabLayout+recyclerView實(shí)現(xiàn)錨點(diǎn)定位的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08詳解Flutter中StatefulBuilder組件的使用
StatefulBuilder小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時(shí)僅重建某些小區(qū)域而無需付出太多努力。本文將來詳細(xì)講講它的使用,需要的可以參考一下2022-05-05