栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android自定义View实现抖音飘动红心效果

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android自定义View实现抖音飘动红心效果

本文实例为大家分享了Android自定义View实现抖音飘动红心效果的具体代码,供大家参考,具体内容如下

自定义View——抖音飘动红心

效果展示

动画效果

使用自定义view完成红心飘动效果

View实现

动画:属性动画(位移+缩放+透明度+旋转)
+
随机数:(属性动画参数+颜色选取)

View


public class FlyHeartView extends RelativeLayout {

  private int defoutWidth = 200;//默认控件宽度
  private long mDuration = 3000;//默认动画时间
  //颜色集合 从中获取颜色
  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);
  }

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

  
  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));
    //执行动画
    animatorSet.start();
    //销毁view
    animatorSet.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
 super.onAnimationEnd(animation);
 removeView(heartIv);
      }
    });
  }

  
  private Animator createTranslationX(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0, (float) (defoutWidth * Math.random() / 4));
    animator.setDuration(mDuration);
    //CycleInterpolator cycles 正弦曲线数
    animator.setInterpolator(new CycleInterpolator((float) (3 * Math.random())));
    return animator;
  }


  
  private Animator createTranslationY(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, -1000);
    animator.setDuration(mDuration);
    animator.setInterpolator(new AccelerateInterpolator());
    return animator;
  }


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

  
  private Animator createAlpha(View view) {
    ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1, 0.1f);
    animator.setDuration(mDuration);
    animator.setInterpolator(new AccelerateInterpolator());
    return animator;
  }

  
  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中调用FlyHeartView 的startFly()方法就能实现点击飘心效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/154049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号