Skip to content

Instantly share code, notes, and snippets.

@vivekgidmare
Forked from nickbutcher/MainActivity.java
Created March 15, 2017 18:04
Show Gist options
  • Save vivekgidmare/b50328318a5cf54c3c4ed7b4ca237433 to your computer and use it in GitHub Desktop.
Save vivekgidmare/b50328318a5cf54c3c4ed7b4ca237433 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment