activity切换动画与页面切换动画

思路:先敲好两个Activity之间的跳转,调试成功,然后敲这个跳转的动画,调试成功,最后敲页面切换

在main.xml中


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"   
     >

    <ViewFlipper
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/viewFlipper">
        
        <!-- 第一页 -->
        <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
           >
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/first"
               />
           <Button
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="@string/open"
               android:onClick="openActivity"
               />
            
        </LinearLayout>
        
        <!-- 第二页 -->
        <LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
                android:background="#339900"
            >
           <TextView
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:text="@string/second"
               />
            
        </LinearLayout>
    </ViewFlipper>"

</LinearLayout>

在string.xml中

    <string name="first">Hello first!</string>
    <string name="open">open</string>
    <string name="second">Hello second!</string>
    <string name="newone">I am new !</string>



在MainActivity.java中

    private ViewFlipper viewFlipper;
    private float startX;
    private Animation in_lefttoright;
    private Animation out_lefttoright;
    private Animation in_righttoleft;
    private Animation out_righttoleft;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
        out_lefttoright = AnimationUtils.loadAnimation(this,R.anim.out_lefttoright);
        
        in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
        out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
        
        viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
        
    }
    
    @Override
    public  boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN) {
            startX = event.getX();
        }else if(event.getAction()==MotionEvent.ACTION_UP) {
            float endX = event.getX();
            if(endX > startX) {
                viewFlipper.setInAnimation(in_lefttoright);
                viewFlipper.setOutAnimation(out_lefttoright);
                viewFlipper.showNext();
            }else if(endX < startX) {
                viewFlipper.setInAnimation(in_righttoleft);
                viewFlipper.setOutAnimation(out_righttoleft);
                viewFlipper.showPrevious();
            }
            return true;
        }
        
        return super.onTouchEvent(event);
    }

    public void openActivity(View v) {
        Intent intent = new Intent(MainActivity.this,OtherActivity.class);
        startActivity(intent);
        this.overridePendingTransition(R.anim.enteralpha, R.anim.outalpha);
    }



在AndroidManifest.xml中

        </activity>
        <activity android:name=".OtherActivity" android:label="新窗口"/>





在other.xml中


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#6600FF">
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/newone"/>
</LinearLayout>


在OtherActivity.java中

        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);



在anim文件中


 enter_lefttoright.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:duration="500"
        />
</set>


 enter_righttoleft.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="500"
        />
</set>


enteralpha.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="5000"
        />
</set>


out_lefttoright.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="500"
        />
</set>


out_righttoleft.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        android:duration="500"
        />
</set>


outalpha.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    
    <alpha
        android:fromAlpha="1"
        android:toAlpha="0"
        android:duration="5000"
        />

</set>


遇到的问题:    

     
    忘了注册   <activity android:name=".OtherActivity" android:label="新窗口"/>


详解参考:http://blog.csdn.net/lidew521/article/details/8946392
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值