zoukankan      html  css  js  c++  java
  • Android SpringAnimator初探

    介绍

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

  • 相关阅读:
    POJ 3253 Fence Repair
    POJ 2431 Expedition
    NYOJ 269 VF
    NYOJ 456 邮票分你一半
    划分数问题 DP
    HDU 1253 胜利大逃亡
    NYOJ 294 Bot Trust
    NYOJ 36 最长公共子序列
    HDU 1555 How many days?
    01背包 (大数据)
  • 原文地址:https://www.cnblogs.com/Jabba93/p/Android-SpringAnimator-chu-tan.html
Copyright © 2011-2022 走看看