动态增加view 的动画 AnimationSet.addAnimation


有时候,希望在动画启动前再去给AnimationSet 增加动画,特别是有时候需要设置一些位置,而大家都知道在Activity的onCreate当中是无法获取Layout的相关值得,获取width,height 的结果都是为0.因为压根还没有开始绘制嘛。所以想要在动画启动前给AnimationSet 增加动画。

但是看AnimationSet的源码,似乎找不到能够有动画启动前的监听。只有已经启动时的监听。setAnimationListener自然是用不上了。认真地看了下AnimationSet  和 Animation 以及View中有关Animation的源码。发现了一个函数setStartTime()感觉比较隐蔽来着。看到这个函数,谁会重载这个呢?而且这个是公有函数,如果被外部函数调用,那就。。。不管了。现在只找到这个函数了。。。

先看看View 和Animation 中的start 。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在XML布局文件中定义一个ImageView: ``` <ImageView android:id="@+id/iv_animation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_check_circle" android:visibility="invisible" android:layout_gravity="center"/> ``` 然后,在Java代码中实现动画效果: ``` // 获取ImageView ImageView ivAnimation = findViewById(R.id.iv_animation); // 定义动画 AnimationSet animationSet = new AnimationSet(true); ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 3f, 1f, 3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f); animationSet.addAnimation(scaleAnimation); animationSet.addAnimation(alphaAnimation); animationSet.setDuration(1000); // 设置动画监听器 animationSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // 动画开始时,显示ImageView ivAnimation.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animation animation) { // 动画结束时,隐藏ImageView,并跳转到下一页 ivAnimation.setVisibility(View.INVISIBLE); Intent intent = new Intent(MainActivity.this, SuccessActivity.class); startActivity(intent); } @Override public void onAnimationRepeat(Animation animation) { } }); // 开始动画 ivAnimation.startAnimation(animationSet); ``` 这个动画效果,是一个放大并逐渐消失的圆圈。通过设置动画监听器,在动画结束时跳转到下一页。具体的实现方式,可以根据自己的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值