Android 给一个View设置一个从上往下缓慢向下移动触底弹跳两下的动画
时间: 2024-03-05 15:50:11 浏览: 26
好的,可以使用属性动画实现这个动画效果。以下是示例代码:
```java
// 获取需要执行动画的View
View view = findViewById(R.id.view);
// 计算移动距离
int translationY = getResources().getDisplayMetrics().heightPixels - view.getBottom();
// 创建属性动画
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, translationY);
// 设置动画持续时间
animator.setDuration(2000);
// 设置插值器,实现缓慢向下移动效果
animator.setInterpolator(new AccelerateInterpolator());
// 设置动画监听器,实现触底弹跳效果
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// 创建属性动画,实现触底弹跳效果
ObjectAnimator bounceAnimator = ObjectAnimator.ofFloat(view, "translationY", 0, -100, 0);
bounceAnimator.setDuration(500);
bounceAnimator.setInterpolator(new BounceInterpolator());
bounceAnimator.start();
}
});
// 执行动画
animator.start();
```
这段代码会让View从上向下缓慢移动到底部,触底后会弹跳两下。需要注意的是,这段代码中使用了两个插值器,分别为AccelerateInterpolator和BounceInterpolator,可以通过修改这些插值器来改变动画效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)