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

推荐订阅源

H
Help Net Security
腾讯CDC
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
量子位
F
Fortinet All Blogs
G
Google Developers Blog
T
The Blog of Author Tim Ferriss
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
D
Docker
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
M
MIT News - Artificial intelligence
博客园 - 【当耐特】

博客园 - 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 来管理开始和结束。