Skip to content

Instantly share code, notes, and snippets.

@fdoyle
Last active December 25, 2015 19:18
Show Gist options
  • Save fdoyle/a98f95621d529301ef86 to your computer and use it in GitHub Desktop.
Save fdoyle/a98f95621d529301ef86 to your computer and use it in GitHub Desktop.
package com.lol.android.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;
import com.lol.android.R;
import java.util.Date;
import butterknife.InjectView;
import butterknife.Views;
/**
* Created by fdoyle on 10/16/13.
*/
public class CoolGestureActivity extends Activity {
@InjectView(R.id.container)
android.widget.LinearLayout container;
@Override
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.animtestlayout);
Views.inject(this);
for (int i = 0; i != container.getChildCount(); i++) {
container.getChildAt(i).setOnTouchListener(new CustomTouchListener());
container.getChildAt(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(CoolGestureActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
public class CustomTouchListener implements View.OnTouchListener {
//public long start;
public int waitTime = 300;
float rotMax = 10;
float startX;
float startY;
@Override
public boolean onTouch(final View view, MotionEvent motionEvent) {
float touchPositionX = motionEvent.getX();
float touchPositionY = motionEvent.getY();
float touchNormalX = touchPositionX / view.getWidth() - .5f;
float touchNormalY = touchPositionY / view.getHeight() - .5f;
touchNormalX = Math.max(Math.min(touchNormalX, .5f), -.5f);
touchNormalY = Math.max(Math.min(touchNormalY, .5f), -.5f);
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = touchPositionX;
startY = touchPositionY;
view.animate().scaleX(.9f).scaleY(.9f).setDuration(300).rotationY(touchNormalX * rotMax).rotationX(-touchNormalY * rotMax).start();
view.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
if (motionEvent.getDownTime() + waitTime < motionEvent.getEventTime()) {
view.animate().rotationY(touchNormalX * rotMax).rotationX(-touchNormalY * rotMax).setDuration(0).start();
} else if (Math.abs(startX - touchPositionX) < Math.abs(startY - touchPositionY)){
view.getParent().requestDisallowInterceptTouchEvent(false);
view.animate().scaleX(1).scaleY(1).setDuration(300).rotationX(0).rotationY(0).start();
}
return true;
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
view.animate().scaleX(1).scaleY(1).setDuration(300).rotationX(0).rotationY(0).start();
break;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment