Skip to content

Instantly share code, notes, and snippets.

@nickbutcher
Last active August 20, 2021 16:15
Show Gist options
  • Save nickbutcher/7fdce476aaa589680cdd626d78e3149d to your computer and use it in GitHub Desktop.
Save nickbutcher/7fdce476aaa589680cdd626d78e3149d to your computer and use it in GitHub Desktop.
A quick sample of the new physics-based animation library added in Support Library 25.3.0 docs: https://developer.android.com/reference/android/support/animation/package-summary.html output: https://twitter.com/crafty/status/842055117323026432
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.boing"
minSdkVersion 25
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-dynamic-animation:25.3.0'
compile 'com.android.support:design:25.3.0'
}
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.boing;
import android.app.Activity;
import android.os.Bundle;
import android.support.animation.SpringAnimation;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.SeekBar;
public class MainActivity extends Activity {
private float downX, downY;
private SeekBar damping, stiffness;
private VelocityTracker velocityTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(android.R.id.content).setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
stiffness = (SeekBar) findViewById(R.id.stiffness);
damping = (SeekBar) findViewById(R.id.damping);
velocityTracker = VelocityTracker.obtain();
final View box = findViewById(R.id.box);
findViewById(R.id.root).setOnTouchListener(new View.OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
velocityTracker.addMovement(event);
return true;
case MotionEvent.ACTION_MOVE:
box.setTranslationX(event.getX() - downX);
box.setTranslationY(event.getY() - downY);
velocityTracker.addMovement(event);
return true;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
velocityTracker.computeCurrentVelocity(1000);
if (box.getTranslationX() != 0) {
SpringAnimation animX = new SpringAnimation(box, SpringAnimation.TRANSLATION_X, 0);
animX.getSpring().setStiffness(getStiffness());
animX.getSpring().setDampingRatio(getDamping());
animX.setStartVelocity(velocityTracker.getXVelocity());
animX.start();
}
if (box.getTranslationY() != 0) {
SpringAnimation animY = new SpringAnimation(box, SpringAnimation.TRANSLATION_Y, 0);
animY.getSpring().setStiffness(getStiffness());
animY.getSpring().setDampingRatio(getDamping());
animY.setStartVelocity(velocityTracker.getYVelocity());
animY.start();
}
velocityTracker.clear();
return true;
}
return false;
}
});
}
private float getStiffness() {
return Math.max(stiffness.getProgress(), 1f);
}
private float getDamping() {
return damping.getProgress() / 100f;
}
}
@songzhw
Copy link

songzhw commented Mar 18, 2017

activity_main.xml is like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <SeekBar
        android:id="@+id/stiffness"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" />

    <SeekBar
        android:id="@+id/damping"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp" />

    <View
        android:id="@+id/box"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_margin="140dp"
        android:background="@color/colorPrimary" />

</FrameLayout>

@ukevgen
Copy link

ukevgen commented Apr 20, 2017

Can you show how to implement this example Figure 2: Animation built by using physics-based APIs fromhttps://developer.android.com/guide/topics/graphics/physics-based-animation.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment