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

android 控件位置对调实现带动画

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

android 控件位置对调实现带动画

对调

实现上述功能代码很简单,如下:

private ObjectAnimator leftAnimator, rightAnimator;

private void changeText(View tvFrom, View tvTo) {
    float distance = tvTo.getX() - tvFrom.getX();
    float textWidth = tvFrom.getWidth() - tvTo.getWidth();
    if (distance < 0) {
        distance = Math.abs(distance);
        float otherDistance = distance - Math.abs(textWidth);
        if (textWidth > 0) {
            otherDistance = distance + textWidth;
        }
        leftAnimator = ObjectAnimator.ofFloat(tvFrom, "translationX", otherDistance, 0);
        rightAnimator = ObjectAnimator.ofFloat(tvTo, "translationX", -distance, 0);
    } else {
        float otherDistance = distance + Math.abs(textWidth);
        if (textWidth > 0) {
            otherDistance = distance - textWidth;
        }
        leftAnimator = ObjectAnimator.ofFloat(tvFrom, "translationX", 0, distance);
        rightAnimator = ObjectAnimator.ofFloat(tvTo, "translationX", 0, -otherDistance);
    }
    leftAnimator.setDuration(500);
    leftAnimator.start();
    rightAnimator.setDuration(500);
    rightAnimator.start();
    rightAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            startAnimal = false;

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
}

需要注意的是,如果是简单的展示就没什么问题了,但是如果是要对转换后进行操作的话还要想到这个转换其实是将整个控件也掉位置了,不仅仅只是显示效果而已。需要处理对应的逻辑。

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

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

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