Activity切り替え---ジェスチャー+アニメーション
4525 ワード
上下、左右のスライド切り替えactivity+アニメーション効果を記録します.頭が悪いのではないかと心配しています.
右進アニメーション
下り上り
public class StartActivity extends Activity implements OnTouchListener,OnGestureListener{
private GestureDetector mGestureDetector;
private int verticalMinDistance = 20;
private int minVelocity = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_start);
mGestureDetector = new GestureDetector((OnGestureListener)this);
//
LinearLayout viewSnsLayout = (LinearLayout)findViewById(R.id.linear);
viewSnsLayout.setOnTouchListener(this);
viewSnsLayout.setLongClickable(true);
}
public void intentMainActivity(){
new Thread(new Runnable() {
@Override
public void run() {
try {
// 3
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//
Intent intent = new Intent(StartActivity.this,MainActivity.class);
startActivity(intent);
// Activiry
finish();
}
}).start();
}
/*
*
*/
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
/*
*
* e1 The first down motion event that started the fling.
* e2 The move motion event that triggered the current onFling.
* velocityX The velocity of this fling measured in pixels per second along the x axis. x
* velocityY The velocity of this fling measured in pixels per second along the y axis. y
*/
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
Intent intent = new Intent(StartActivity.this,MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
//
}
if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
// Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
}
if (Math.abs(e2.getY()-e1.getY())>50) {
Intent intent = new Intent(StartActivity.this,MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_down, R.anim.out_to_up);
}
if (e2.getY()-e1.getY()>50) {
// Toast.makeText(getApplicationContext(), " ", Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public boolean onDown(MotionEvent arg0) {return false;}
@Override
public void onLongPress(MotionEvent arg0) {}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,float arg3) {return false;}
@Override
public void onShowPress(MotionEvent arg0) {}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {return false;}
}
右進アニメーション
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="100%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="0%p" />
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="-100%p" />
下り上り
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0%p"
android:interpolator="@android:anim/accelerate_interpolator"
/>
<?xml version="1.0" encoding="utf-8"?>
<!-- -->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromYDelta="0%p"
android:toYDelta="-100%p"
android:duration="1000">
</translate>