Android移动view动画问题

</pre><span style="background-color: rgb(255, 255, 255);">Android写动画效果不是一般的麻烦,网上找了好久,终于解决了动画的问题,总结记录以共勉。</span></div><div style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px;"><span style="background-color: rgb(255, 255, 255);">仅以水平方向移动效果做说明,垂直方向类似。</span></div><div style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px;"><span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: rgb(255, 255, 255);">完整动画函数代码:</span></div><div style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"><span style="font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px; line-height:19px; background-color:rgb(40,85,126)"></span></div><pre name="code" class="java">public void slideview(final float p1, final float p2) {
    TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
    animation.setInterpolator(new OvershootInterpolator());
    animation.setDuration(durationMillis);
    animation.setStartOffset(delayMillis);
    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }
        
        @Override
        public void onAnimationRepeat(Animation animation) {
        }
        
        @Override
        public void onAnimationEnd(Animation animation) {
            int left = view.getLeft()+(int)(p2-p1);
            int top = view.getTop();
            int width = view.getWidth();
            int height = view.getHeight();
            view.clearAnimation();
            view.layout(left, top, left+width, top+height);
        }
    });
    view.startAnimation(animation);
}
调用示例: 
移动到目标位置
slideview(0, distance);
从目标位置移回原位

slideview(0, -distance); 

过程中遇到的问题:

 1、动画执行完成后,view回到原位

 TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
 animation.setInterpolator(new OvershootInterpolator());
 animation.setDuration(durationMillis);
 animation.setStartOffset(delayMillis);
 view.startAnimation(animation);

开始时动画效果只写了这么多,发现动画执行完,view会回到原位。

经过查资料尝试使用animation.setFillAfter(true); view不再返回原位,但又出现了第2个问题

2、点击按钮时,view在初始位置会先闪一下,再执行动画 

 经过查资料得知,animation.setFillAfter(true); 只是将view移动到了目标位置,但是view绑定的点击事件还在原来位置,导致点击时会先闪一下

又查资料找到解决办法:
不加setFillAfter, 通过设置view位置实现效果,增加如下代码
animation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }
    
    @Override
    public void onAnimationRepeat(Animation animation) {
    }
    
    @Override
    public void onAnimationEnd(Animation animation) {
        int left = view.getLeft()+(int)(p2-p1);
        int top = view.getTop();
        int width = view.getWidth();
        int height = view.getHeight();
        view.clearAnimation();
        view.layout(left, top, left+width, top+height);
    }
});

在动画执行完毕后(onAnimationEnd)设置view的位置,同时要clearAnimation()

注:clearAnimation() 必须在 layout(l,t,r,b) 前执行,否则会出错~
至此大功告成~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值