2020-02-12
首先,来一个心跳动画的工具类,然后调用时传入要动画的View即可;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.OvershootInterpolator;
public class XinTiaoAnimatorUtil {
private static AnimatorSet animatorSet;
public static void start(View target) {
//先取消原来的动画
if (animatorSet != null) animatorSet.cancel();
animatorSet = new AnimatorSet();
// ObjectAnimator animator1 = ObjectAnimator.ofFloat(target, "ScaleX", 1, 1.08f);
// ObjectAnimator animator2 = ObjectAnimator.ofFloat(target, "ScaleY", 1, 1.08f);
// animator1.setRepeatCount(ValueAnimator.INFINITE); //重复次数:无限
// animator1.setRepeatMode(ValueAnimator.REVERSE); //重复模式:返回
// animator2.setRepeatCount(ValueAnimator.INFINITE);
// animator2.setRepeatMode(ValueAnimator.REVERSE);
// //开始时超出位置插值器
// animatorSet.setInterpolator(new AnticipateInterpolator());
// animatorSet.setDuration(500);
//or
ObjectAnimator animator1 = ObjectAnimator.ofFloat(target, "ScaleX", 1, 1.08f, 1);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(target, "ScaleY", 1, 1.08f, 1);
animator1.setRepeatCount(ValueAnimator.INFINITE); //重复次数:无限
animator2.setRepeatCount(ValueAnimator.INFINITE);
//开始时结束时都超出位置插值器
animatorSet.setInterpolator(new AnticipateOvershootInterpolator());
animatorSet.setDuration(1000);
//设置为两个动画一起执行
animatorSet.playTogether(animator1, animator2);
//开始动画
animatorSet.start();
}
}
好了,心跳动画功能写完了,就这一个类,核心动画功能就几行代码。
当然,这个是单个视图的心跳动画,如果要多个,就去掉static,还得根据不同的使用场景进行优化处理。
如何使用:
使用时直接: XinTiaoAnimatorUtil.start(view)); //传入要心跳动画的view
要是在列表里要怎么用呢?
以选中条目视图心跳动画为例:
我用的是/第三方recyclerView适配器/
api 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
XinTiaoAnimatorUtil.start(view.findViewById(R.id.xinTiaoView));
}
});
这样,点击哪个条目,哪个条目就执行心跳动画,但是刚进页面时,则没有动画,
如果要一开始就让第一个条目执行心跳动画要怎么做呢:
在列表、适配器以及数据都初始完成后, 用下面的代码 执行第一个条目的点击操作:
1、如果有条目才执行;
2、延迟执行:因为刚数据初始完成后,界面没出来,直接调用获取不到条目视图;
if (dataList.size() > 0) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//别忘记在条目布局的根布局加上id item_view
adapter.getViewByPosition(recyclerView, 0, R.id.item_view).performClick();
}
}, 300);
}
完事儿