最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義View實現(xiàn)抖音飄動紅心效果

 更新時間:2020年05月29日 09:14:50   作者:汪星沒有熊  
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)抖音飄動紅心效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android自定義View實現(xiàn)抖音飄動紅心效果的具體代碼,供大家參考,具體內(nèi)容如下

自定義View——抖音飄動紅心

效果展示

動畫效果

使用自定義view完成紅心飄動效果

View實現(xiàn)

動畫:屬性動畫(位移+縮放+透明度+旋轉(zhuǎn))
+
隨機數(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)動畫時間
  //顏色集合 從中獲取顏色
  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)建一個心形的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()方法就能實現(xiàn)點擊飄心效果。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 圖文詳解Flutter單例的實現(xiàn)

    圖文詳解Flutter單例的實現(xiàn)

    一個類只允許創(chuàng)建一個實例,那這個類就是一個單例類,這種設(shè)計模式就叫作單例設(shè)計模式,簡稱單例模式,下面這篇文章主要給大家介紹了關(guān)于Flutter單例的實現(xiàn)方法,需要的朋友可以參考下
    2021-12-12
  • Android選擇與上傳圖片之Matisse教程

    Android選擇與上傳圖片之Matisse教程

    這篇文章主要介紹了在Android中對于圖片的選擇與上傳方法,本文介紹了Matisse的相關(guān)使用教程,學(xué)習(xí)Android的同學(xué)進來看看吧
    2021-08-08
  • Android 矢量室內(nèi)地圖開發(fā)實例

    Android 矢量室內(nèi)地圖開發(fā)實例

    這篇文章主要介紹了Android 矢量室內(nèi)地圖開發(fā)實例的相關(guān)資料,這里提供代碼實例,及實現(xiàn)效果圖,矢量室內(nèi)對圖簡單DEMO,需要的朋友可以參考下
    2016-11-11
  • Android藍牙服務(wù)查找附近設(shè)備分析探索

    Android藍牙服務(wù)查找附近設(shè)備分析探索

    這篇文章主要介紹了Android藍牙服務(wù)實現(xiàn)查找附近設(shè)備,了解內(nèi)部原理是為了幫助我們做擴展,同時也是驗證了一個人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會的
    2023-01-01
  • Kotlin?this關(guān)鍵字的使用實例詳解

    Kotlin?this關(guān)鍵字的使用實例詳解

    這篇文章主要介紹了Kotlin?this關(guān)鍵字的使用實例,在Kotlin中,this關(guān)鍵字允許我們引用一個類的實例,該類的函數(shù)恰好正在運行。此外,還有其他方式可以使this表達式派上用場
    2023-02-02
  • Android的異步任務(wù)AsyncTask詳解

    Android的異步任務(wù)AsyncTask詳解

    本文給大家介紹的是Android的異步任務(wù)AsyncTask,在Android中實現(xiàn)異步任務(wù)機制有兩種方式,Handler和AsyncTask。今天我們先來主要談下ASYNCTASK。
    2015-07-07
  • Android Studio設(shè)置主題與字體大小圖文教程

    Android Studio設(shè)置主題與字體大小圖文教程

    這篇文章通過圖文詳細(xì)的給大家介紹了Android Studio中如何設(shè)置主題與字體大小,文章介紹的非常詳細(xì),相信對大家學(xué)習(xí)使用Android Studio具有一定的參考借鑒價值,有需要的朋友們下面來一起看看吧。
    2016-10-10
  • Android編程開發(fā)之EditText實現(xiàn)輸入QQ表情圖像的方法

    Android編程開發(fā)之EditText實現(xiàn)輸入QQ表情圖像的方法

    這篇文章主要介紹了Android編程開發(fā)之EditText實現(xiàn)輸入QQ表情圖像的方法,涉及Android多媒體文件及EditText的相關(guān)操作技巧,需要的朋友可以參考下
    2015-12-12
  • android雜記:C++文件的添加log方法分享

    android雜記:C++文件的添加log方法分享

    這篇文章介紹了android雜記:C++文件的添加log方法,有需要的朋友可以參考一下
    2013-09-09
  • Android自定義view制作抽獎轉(zhuǎn)盤

    Android自定義view制作抽獎轉(zhuǎn)盤

    這篇文章主要為大家詳細(xì)介紹了Android自定義view制作抽獎轉(zhuǎn)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論

江都市| 元氏县| 北安市| 柘荣县| 九寨沟县| 平湖市| 华容县| 丹东市| 彰化县| 民县| 石家庄市| 莱芜市| 灵寿县| 当阳市| 明水县| 梧州市| 定边县| 琼结县| 安多县| 隆化县| 梁平县| 东莞市| 泸水县| 青铜峡市| 于田县| 大荔县| 凌源市| 万山特区| 广西| 岚皋县| 汝南县| 桦川县| 康马县| 凤冈县| 杭州市| 乌拉特前旗| 蕉岭县| 洪湖市| 翼城县| 清镇市| 林甸县|