惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
博客园 - 司徒正美
宝玉的分享
宝玉的分享
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
月光博客
月光博客
博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
雷峰网
雷峰网
博客园 - 叶小钗
量子位
IT之家
IT之家

博客园 - hangox

Claude Code 自定义状态栏配置指南 如何给 Claude 中的网页做截图 AGP 8.0+ Publish idea 插件兼容 K2 模式 IDEA 不提示 gitlab-ci 字段问题 不要随便使用 npm 源加速站点 Xcode Format Swift 代码 idea Convert File To Kotlin File 多 Commit 问题 ApkList 源问题修复 BCM94360CS2 黑苹果 macOS Sonoma Wifi 不能开启修复 NginxProxyManager 数据库迁移记录 android ConcurrentModificationException 错误 Android Studio 插件分享——Gradle Utilities Android Studio 插件分享——Jadx Class Decompiler templete Android Studio 3.0 新特性 关于Android菜单上的记录 升级到 classpath 'com.android.tools.build:gradle:1.0.0-rc1 OnScrollListenerPro
Android SpringAnimator初探
hangox · 2017-07-13 · via 博客园 - hangox

介绍

Android 官方发布了SpringAnimator ,这个动画效果其实和Facebook的rebound是一样的,但是API的设计不一样,总体来说功能更多,设计也友好。推荐使用

实现的效果

实现一个很简单的拖拽放开回弹的效果

使用

添加依赖

新的动画库是在26.0.0 这个版本的API包上的,所以需要target 和 compileVersion 都为26 的才能使用,下面是我的项目依赖配置

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.hangox.springanimatortest"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding{
        enabled true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.0.0-beta2'
    compile "com.android.support:support-dynamic-animation:26.0.0-beta2"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.hangox:xLog:1.0'
    testCompile 'junit:junit:4.12'
}

主要代码实现

 mainBinding.icon.setOnTouchListener(new View.OnTouchListener() {

            PointF mPoint = new PointF();
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                XLog.v(motionEvent);
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        mPoint.set(motionEvent.getRawX(),motionEvent.getRawY());
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        view.setTranslationX(motionEvent.getRawX() - mPoint.x+ view.getTranslationX());
                        view.setTranslationY(motionEvent.getRawY() - mPoint.y + view.getTranslationY());
                        mPoint.set(motionEvent.getRawX(),motionEvent.getRawY());
                        return true;

                    case MotionEvent.ACTION_UP:
                        SpringAnimation springAnimationY  = new SpringAnimation(view, DynamicAnimation.TRANSLATION_Y,0);
                        SpringAnimation springAnimationX = new SpringAnimation(view,DynamicAnimation.TRANSLATION_X,0);
                        springAnimationX.start();
                        springAnimationY.start();
                        return true;

                }
                return false;
            }
        });

总结

SpringAnimator的API设计要rebound的要简洁的多,很容易写成反弹的动画。但是也和rebound 有一样的问题,代码中我是先后start两个动画。并不能像普通的Animator 那样提供一个AnimatorSet 来管理开始和结束。