滑动到下一个片段

时间:2014-03-08 21:20:25

标签: android animation android-fragments ontouchevent

我的应用程序滑出已完成的游戏,该游戏保存在带动画的片段中。然后幻灯片放在一个新片段中的新游戏中。

问题:

1)我可以根据手指拖动让动画逐步完成动画。这样动画跟随手指?

2)目前如果我使用动画然后旋转到横向,layout_land使用动画。回到肖像也是如此。我可以阻止这个吗?

"slide_in_right.xml"
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_longAnimTime"
    android:fromXDelta="100%p"
    android:toXDelta="0"
    android:zAdjustment="top" />
</set>

"slide_out_left.xml"
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="0"
    android:toXDelta="-100%p"
    android:zAdjustment="top" />
</set>



import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends FragmentActivity {

    public static final String TAG = "FragTag";
    float x1,x2;
    float y1, y2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView background = (ImageView) this.findViewById(R.id.background);
        background.setImageResource(R.drawable.stars);

    if (savedInstanceState == null) {
        UIrFragment test = new UIrFragment();
        test.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().replace(android.R.id.content, test, TAG).commit();
    }
}


public boolean onTouchEvent(MotionEvent touch_event)
{
    switch (touch_event.getAction())
    {
        // when user first touches the screen we get x and y coordinate
        case MotionEvent.ACTION_DOWN:
        {
            x1 = touch_event.getX();
            y1 = touch_event.getY();
            break;
        }
        case MotionEvent.ACTION_UP: {
            x2 = touch_event.getX();
            y2 = touch_event.getY();

            if (x1 > x2) {

                UIrFragment transition = new UIrFragment();
                transition.setArguments(getIntent().getExtras());
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left);
                ft.replace(android.R.id.content, transition);
                ft.commit();

                }
                break;
            }
        }
        return false;
    }
}

0 个答案:

没有答案