如何制作这个动画?

时间:2011-07-08 17:23:05

标签: android

我想在两个活动之间切换时做这个动画:

enter image description here

-

enter image description here

-

enter image description here

任何帮助?

2 个答案:

答案 0 :(得分:1)

这两种方法对于快速解决问题非常有用:

Animation slide_out_left = AnimationUtils.makeOutAnimation(getActivity(),false);
Animation slide_in_right = AnimationUtils.makeInAnimation(getActivity(), true);

Animation slide_out_right = AnimationUtils.makeOutAnimation(getActivity(),true);
Animation slide_in_left = AnimationUtils.makeInAnimation(getActivity(), false);

here是方法的文档,因为你会看到第二个参数是选择边的标志。

结果类似于Rodrigo之前编写的内容,但它需要2行代码,并且不需要定义自定义动画。

答案 1 :(得分:0)

答案如下:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

                ViewFlipper flipper = findViewById(R.id.flipper);
                flipper.setOnTouchListener(this);
}

private void myOnTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_MOVE:
            mDragged = true;
            break;
        case MotionEvent.ACTION_DOWN:
            mDragged = false;
            mXbefore = event.getX();
            break;
        case MotionEvent.ACTION_UP:

            if(mDragged){
                vf = (ViewFlipper) v;
                mXafter = event.getX();

                if(mXafter > mXbefore){
                    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_to_left_in));
                    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_to_left_out));
                    vf.showNext();
                }else{
                    vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right_in));
                    vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right_out));
                    vf.showPrevious();
                }
            }

            mDragged = false;
            break;
        default:
            break;
        }
    }

main.xml中:

<ViewFLipper id="@+id/flipper">
         <YourLayout />
          <YourLayout />
</ViewFlipper>

right_to_left_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="-100%p" 
        android:toXDelta="0%p" 
        android:duration="500"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set> 

right_to_left_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="100%p" 
        android:toXDelta="200%p" 
        android:duration="500"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set>

left_to_right_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="100%p" 
        android:toXDelta="0" 
        android:duration="500"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
</set>

left_to_right_out_xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0%p" 
        android:toXDelta="-100%p" 
        android:duration="500"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
</set>