關於android中activity的動畫切換,是不少地方都會用到的,在這我粗略的說一下個人理解java
動畫大致上分四種:alpha、rotate、translate、scaleandroid
有兩種方法,app
1.在代碼中編寫動畫 (在遊戲中用得多)ide
2.在xml文件中編寫 (在應用中用得多)佈局
這裏主要講的xml文件的編寫。動畫
一。首先在工程的res目錄下新建一個anim文件夾this
1.alphaspa
進入xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500"
/>
</set> 遊戲
退出
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500"
/>
</set>
2.rotate
進入
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="500"
android:fromDegrees="-90"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="0%"
android:pivotY="0%"
android:toDegrees="0" />
</set>
退出
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:interpolator="@android:anim/linear_interpolator"
android:duration="500"
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="0%"
android:pivotY="0%"
/>
</set>
3.translate
進入
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:interpolator="@android:anim/linear_interpolator"
android:fromXDelta="480"
android:toXDelta="0"
android:fromYDelta="800"
android:toYDelta="0"
android:duration="500"
/>
</set>
退出
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:interpolator="@android:anim/linear_interpolator"
android:fromXDelta="0"
android:toXDelta="-480"
android:fromYDelta="0"
android:toYDelta="-800"
android:duration="500"
/>
</set>
4.scale
進入
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/linear_interpolator"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
/>
</set>
退出
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="@android:anim/linear_interpolator"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
/>
</set>
二。加載動畫的類Animation.java(方便在Activity中隨機選擇一組進出的動畫)
package com.youchuang.utils;
import com.youchuang.activity.R;
import android.app.Activity;
import android.content.Context;
public class Animation{
private Context context;
public Animation(Context context)
{
this.context = context;
}
public void change(int id) {
switch (id) {
case 1:
//淡入淡出
((Activity) context).overridePendingTransition(R.anim.enter_alpha,R.anim.out_alpha);
break;
case 2:
//旋入淡出
((Activity) context).overridePendingTransition(R.anim.enter_rotate, R.anim.out_alpha);
break;
case 3:
//旋入旋出
((Activity) context).overridePendingTransition(R.anim.enter_rotate, R.anim.out_rotate);
break;
case 4:
((Activity) context).overridePendingTransition(R.anim.enter_scale, R.anim.out_translate);
break;
}
}
}
三。Activity文件
1.InActivity.java
package com.youchuang.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.youchuang.utils.Animation;
public class InActivity extends Activity {
private Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in);
final Animation anim = new Animation(this);
btn = (Button)findViewById(R.id.button_in);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(InActivity.this, OutActivity.class);
startActivity(intent);
anim.change(4);
}
});
}
}
佈局文件:activity_in.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical" >
<Button
android:id="@+id/button_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/in" />
</LinearLayout>
2.OutActivity.java
package com.youchuang.activity;
import android.app.Activity;
import android.os.Bundle;
public class OutActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_out);
}
}
佈局文件:activity_out.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/register_bg"
android:orientation="vertical" >
<Button
android:id="@+id/button_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/out" />
</LinearLayout>