2014年1月5日 星期日

[Android] Android ActivityGroup和ViewFlipper實現多個Activity滑動切換

Reference:http://blog.csdn.net/wyzxk888/article/details/7167909

主顯示介面:

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class ActivityGroupDemoActivity extends ActivityGroup implements OnTouchListener, OnGestureListener {
private ViewFlipper vf;
private GestureDetector gd;
//identify four animation
private Animation leftIn; //Animation 左進動畫
private Animation leftOut; //Animation 左出動畫
private Animation rightIn; //Animation 右進動畫
private Animation rightOut; //Animation 右出動畫

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);  
        vf = (ViewFlipper)this.findViewById(R.id.viewFlipper1);
        gd = new GestureDetector(this);  
        vf.addView(getLocalActivityManager().startActivity("", new Intent(ActivityGroupDemoActivity.this, ActivityOne.class)).getDecorView(), 0); //註冊第一個activity
        vf.addView(getLocalActivityManager().startActivity("", new Intent(ActivityGroupDemoActivity.this, ActivityTwo.class)).getDecorView(), 1);  //註冊第二個activity
        vf.addView(getLocalActivityManager().startActivity("", new Intent(ActivityGroupDemoActivity.this, ActivityThree.class)).getDecorView(), 2); //註冊第三個activity
        vf.setDisplayedChild(0);
        vf.setOnTouchListener(this);
        vf.setLongClickable(true); //allow long click so that it can recognize gesture  
        leftIn = AnimationUtils.loadAnimation(this, R.anim.left_in);
        leftOut = AnimationUtils.loadAnimation(this, R.anim.left_out);
        rightIn = AnimationUtils.loadAnimation(this, R.anim.right_in);
        rightOut = AnimationUtils.loadAnimation(this, R.anim.right_out);
        Log.d("ActGroupDemo", "ActGroup onCreate()");
    }
 
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
//use GestureDetector object to handle motion event
return gd.onTouchEvent(arg1);
}

@Override
public boolean onDown(MotionEvent arg0) {
return false;
}

@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
//arg0 is the first motion event
//arg1 is the last motion event
//arg2 is the velocity of x orientation, px/s
//arg3 is the velocity of y orientation, px/s

int fling_min_distance = 100;
int fling_min_velocity = 200;

if (arg0.getX() - arg1.getX() > fling_min_distance && Math.abs(arg2) > fling_min_velocity) {
vf.setInAnimation(rightIn);
vf.setOutAnimation(leftOut);
vf.showNext();
}
else if (arg1.getX() - arg0.getX() > fling_min_distance && Math.abs(arg2) > fling_min_velocity) {
vf.setInAnimation(leftIn);
vf.setOutAnimation(rightOut);
vf.showNext();
}
return false;
}

@Override
public void onLongPress(MotionEvent e) {
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}

@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}

}
其他activity照常
最後res底下建立一個名為anim的資料夾 並新增四個xml檔
left_in.xml

android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="500"/>

    android:fromAlpha="1.0"
    android:toAlpha="1.0"
android:duration="500" />

left_out.xml

android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="500"/>

    android:fromAlpha="1.0"
    android:toAlpha="1.0"
android:duration="500" />

right_in.xml

android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="500"/>

    android:fromAlpha="1.0"
    android:toAlpha="1.0"
android:duration="500" />

right_out.xml

android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="500"/>

    android:fromAlpha="1.0"
    android:toAlpha="1.0"
android:duration="500" />


此外,主程式的layout:
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >  

沒有留言:

張貼留言