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

  • 相关阅读:
    微信小程序开发 —— 一些小的注意点
    C# —— 继承
    JavaScript —— 判断一个对象是否为空
    SQLServer——事务
    创建存储过程
    (转)从开发小白到音视频专家
    (转载)从零开始学习音视频编程技术(一) 视频格式讲解
    (转载)如何正确的阅读FFmpeg官网提供的资料
    (转载)音频编解码基础(wav/aac/pcma/pcmu)
    (转载)语音编解码过程概述
  • 原文地址:https://www.cnblogs.com/Jabba93/p/Android-SpringAnimator-chu-tan.html
Copyright © 2011-2022 走看看