天天看點

Android Api Demos登頂之路(二)

今天我們來實作在兩個activity之間實作動畫切換的Demo,要實作這個功能非常簡單,隻需要 在啟動第二個activity之後調用overridePendingTransition(enterAnim, exitAnim); 方法即可。

該方法的第一個參數進入的動畫效果,即你要為第二個activity設定進入的動畫效果 第二個數為退出時的動畫效果,即你為第一個activity退出舞台時所設定的動畫效果。

這兩個參數都為整型,對應你的xml檔案ID,你可以在該xml檔案中随意定義你想要實作的動畫效果。

我們在這裡所使用的動畫類型為補間動畫類型,tween-animation。共有四種基本動畫效果,alpha:透明度 漸變效果,rotate: 旋轉效果,scale:縮放效果,translate:移動效果。

下面我們來一步一步實作對這些動畫效果的定義

在res資源檔案夾下建立anim檔案夾,在該檔案夾下選擇建立android xml檔案,資源類型選擇tween animation

我們可以看到下面列出了四種動畫類型,還有一個set,set指的是一個用來存放各種動畫效果的容器,你可以 把各種動畫效果自由組合,爾後放到set容器中,實作你想要達到的效果。

我們先選擇alpha,來實作一個漸變的切換效果。

取名為fade.xml

android:interpolator=”@android:anim/accelerate_interpolator”設定插入器類型

為加速插入器

android:fromAlpha=”0.0”

android:toAlpha=”1.0”

設定透明度由完全透明到完全不透明變化

android:duration=”@android:integer/config_longAnimTime

設定動畫持續時間,這裡用了一個系統定義的時間

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="@android:integer/config_longAnimTime">
</alpha>
           

再建立一個名為hold.xml的動畫檔案,選擇了translate效果

其實我們可以看到這裡雖然定義了動畫,但它定義的橫向移動卻是從0到0

是以這個動畫是沒有動起來的

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="0"
    android:toXDelta="0"
    android:duration="@android:integer/config_longAnimTime">
</translate>
           

接着再建立兩個動畫檔案zoom_enter和zoom_exit,實作縮放的效果

<?xml version="1.0" encoding="utf-8"?>
<!-- android:pivotX="50%p",是指相對于父控件而言取中點,50%的寫法是相對于自身而言-->
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <scale 
        android:fromXScale="2.0" android:toXScale="1.0"
        android:fromYScale="2.0" android:toYScale="1.0"
        android:pivotX="50%p" android:pivotY="50%p"
        android:duration="@android:integer/config_longAnimTime"/>
</set>
           

zoom_exit

<?xml version="1.0" encoding="utf-8"?>
<!-- android:pivotX="50%p",是指相對于父控件而言取中點,50%的寫法是相對于自身而言
android:zAdjustment="top"是設定将該動畫層置于其他動畫層的上面-->
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:zAdjustment="top">
    <scale 
        android:fromXScale="2.0" android:toXScale="1.0"
        android:fromYScale="2.0" android:toYScale="1.0"
        android:pivotX="50%p" android:pivotY="50%p"
        android:duration="@android:integer/config_longAnimTime"/>
    <alpha 
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="@android:integer/config_longAnimTime"/>
</set>
           

建立Animation類,并繼承activity

public class Animation extends Activity {
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        button = (Button) findViewById(R.id.fadein);
        button.setOnClickListener(mFadeinListener);
        button = (Button) findViewById(R.id.zoomin);
        button.setOnClickListener(mZoominListener);

        // 判斷一下如果目前的編譯版本大于或等于jelly_bean(16、17、18;4.1,4.2,4.3)
        // 則顯示新功能的幾個按鈕
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            button = (Button) findViewById(R.id.modernfadein);
            button.setOnClickListener(mModernFadeinListener);
            button = (Button) findViewById(R.id.modernzoomin);
            button.setOnClickListener(mModernZoominListener);
            button = (Button) findViewById(R.id.scaleup);
            button.setOnClickListener(mModernScaleupListener);
            button = (Button) findViewById(R.id.thumbnailzoom);
            button.setOnClickListener(mModernThumbnailzoomListener);
        } else {
            findViewById(R.id.modernfadein).setEnabled(false);
            findViewById(R.id.modernzoomin).setEnabled(false);
            findViewById(R.id.scaleup).setEnabled(false);
            findViewById(R.id.thumbnailzoom).setEnabled(false);
        }
    }

    private OnClickListener mFadeinListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Animation.this, HelloWord.class);
            startActivity(intent);
            overridePendingTransition(R.anim.fade, R.anim.hold);
        }
    };
    private OnClickListener mZoominListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Animation.this, HelloWord.class);
            startActivity(intent);
            overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
        }
    };
    private OnClickListener mModernFadeinListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Animation.this, HelloWord.class);
            ActivityOptions options = ActivityOptions.makeCustomAnimation(
                    Animation.this, R.anim.fade, R.anim.hold);
            startActivity(intent, options.toBundle());
        }
    };
    private OnClickListener mModernZoominListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Android 4.1(API16)提供了一個新類ActivityOptions,用來實作Activity的切換動畫
            //該方法建立一個自定義的動畫
            Intent intent = new Intent(Animation.this, HelloWord.class);
            ActivityOptions options = ActivityOptions.makeCustomAnimation(
                    Animation.this, R.anim.zoom_enter, R.anim.zoom_exit);
            startActivity(intent, options.toBundle());
        }
    };
    private OnClickListener mModernScaleupListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Animation.this, HelloWord.class);
            //Android 4.1(API16)提供了一個新類ActivityOptions,用來實作Activity的切換動畫
            //該方法建立一個動畫,能夠從螢幕指定的位置和指定的大小拉伸一個活動視窗。
            //第一個參數是承載動畫的View,第二和第三個參數指定拉伸開始的位置,第四和第五個參數指定拉伸的大小
            //這裡的v就是名字為scaleup的button按鈕,起始位置就是這個按鈕的左上角
            ActivityOptions options = ActivityOptions.makeScaleUpAnimation(v,
                    , , v.getWidth(), v.getHeight());
            startActivity(intent, options.toBundle());
        }
    };
    private OnClickListener mModernThumbnailzoomListener=new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Animation.this, HelloWord.class);
            //将view轉換成bitmap
            v.setDrawingCacheEnabled(true);
            v.setPressed(false);
            v.refreshDrawableState();
            //轉換成的圖檔就是名為thnmbnailzoom的按鈕
            Bitmap bitmap = v.getDrawingCache();
            //Canvas c=new Canvas(bitmap);
            //建立一個動畫,能夠從螢幕指定的位置和提供的縮略圖拉伸一個活動視窗
            ActivityOptions options = ActivityOptions.makeThumbnailScaleUpAnimation(v, bitmap, , );
            startActivity(intent, options.toBundle());
            //記得将DrawingCacheEnabled還原為false
            v.setDrawingCacheEnabled(false);
        }
    };
}
           

在layout檔案夾下建立activity_animation布局檔案

<?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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:padding="5dp"
        android:text="Press a button to lauch an activity with a custom animation" />

    <Button
        android:id="@+id/fadein"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="14sp"
        android:text="Fade in" />

    <Button
        android:id="@+id/zoomin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="14sp"
        android:text="Zoom in" />

    <Button
        android:id="@+id/modernfadein"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="14sp"
        android:text="Modern fade in" />

    <Button
        android:id="@+id/modernzoomin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="14sp"
        android:text="Modern zoom in" />

    <Button
        android:id="@+id/scaleup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="14sp"
        android:text="Scale up" />

    <Button
        android:id="@+id/thumbnailzoom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="14sp"
        android:text="Thumbnail zoom" />

</LinearLayout>
           

配置檔案中配置activity

<activity 
            android:name="com.example.apidemos.Animation"
            android:label="@string/activity_animation" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="COM_FISHTOSKY_CODE"/>
            </intent-filter>
        </activity>
           

繼續閱讀