日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App->Activity->Animation Activity跳转动画 R.anim.×× overridePendingTransition ActivityOptions类

發布時間:2024/8/24 Android 28 如意码农

App->Activity->Animation示例用于演示不同Activity切換時動態效果。

android 5.0例子中定義了6種動畫效果:

漸變Fade In

縮放Zoom In

Modern fade In

Modern zoom in

Scale up

Thumbnail zoom

Android 中 Animation 資源可以分為兩種:

  • Tween Animation 對單個圖像進行各種變換(縮放,平移,旋轉等)來實現動畫。
  • Frame Animation 由一組圖像順序顯示顯示動畫。

Animation 中使用的是Tween Animation, 使用的資源為R.anim.fade, R.anim.hold,R.anim.zoom_enter, R.anim.zoom_exit。

其中R.anim.fade, R.anim.zoom_enter分別為Fade In 和 Zoom動畫資源。其定義為

fade.xml

<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" />

zoom_center.xml

<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_mediumAnimTime" />
</set>

tween animation 資源定義的格式如下:

<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”
android:interpolator=”@[package:]anim/interpolator_resource”
android:shareInterpolator=[ ” true ” false “>
<alpha
android:fromAlpha=”float”
android:toAlpha=”float” />
<scale
android:fromXScale=”float”
android:toXScale=”float”
android:fromYScale=”float”
android:toYScale=”float”
android:pivotX=”float”
android:pivotY=”float” />
<translate
android:fromXDelta=”float”
android:toXDelta=”float”
android:fromYDelta=”float”
android:toYDelta=”float” />
<rotate
android:fromDegrees=”float”
android:toDegrees=”float”
android:pivotX=”float”
android:pivotY=”float” />
<set> …
</set>
</set>

<set> 為其它animation類型<alpha>,<scale>,<translate>和<rotate>或其它<set>的容器。

android:interpolator 為Interpolator資源ID,Interpolator定義了動畫的變化速率,動畫的各幀的顯示可以加速,減速,重復顯示。

android:shareInterpolator 如果想為<set>中的各個子動畫定義共享interpolator,shareInterpolator 則設為true.

<alpha> 定義Fade in ,Fade out 動畫,其對應的Android類AlphaAnimation,參數由fromAlpha,toAlpha定義。

<scale>定義縮放動畫,其對應的Android類為ScaleAnimation,參數由 fromXScale,toXScale,fromYScale,toYScale,pivotX,pivotY定義,pivotX,pivotY定義了 縮放時的中心。

<translate>定義平移動畫,其對應的Android類為TranslateAnimation,參數由fromXDelta,toXDelta,fromYDelta,toYDelta定義。

<rotate>定義選擇動畫,其對應的Android類RotateAnimation,參數由fromDegrees,toDegrees,pivotX,pivotY, pivotX,pivotY定義選擇中心。

Animation中的Fade In和Zoom In按鈕的事件處理代碼:

    private OnClickListener mFadeListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, AlertDialogSamples.class));
// Supply a custom animation. This one will just fade the new
// activity on top. Note that we need to also supply an animation
// (here just doing nothing for the same amount of time) for the
// old activity to prevent it from going away too soon.
overridePendingTransition(R.anim.fade, R.anim.hold);
}
}; private OnClickListener mZoomListener = new OnClickListener() {
public void onClick(View v) {
// Request the next activity transition (here starting a new one).
startActivity(new Intent(Animation.this, AlertDialogSamples.class));
// This is a more complicated animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
}
};

從代碼可以看到Activity Animation到其它Activity Controls1 切換的動畫使用overridePendingTransition 來定義,函數overridePendingTransition(int enterAnim, int exitAnim) 必須定義在StartActivity(Intent)或是 Activity.finish()之后來定義兩個Activity切換時的動畫,enterAnim 為新Activity出現時動畫效果,exitAnim則定義了當前Activity退出時動畫效果。

剩下的Modern fade In, Modern zoom in, Scale up ,Thumbnail zoom使用ActivityOptions實現Activity跳轉的動畫效果:

    private OnClickListener mModernFadeListener = new OnClickListener() {
public void onClick(View v) {
// Create the desired custom animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this,
R.anim.fade, R.anim.hold);
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
}
}; private OnClickListener mModernZoomListener = new OnClickListener() {
public void onClick(View v) {
// Create a more complicated animation, involving transformations
// on both this (exit) and the new (enter) activity. Note how for
// the duration of the animation we force the exiting activity
// to be Z-ordered on top (even though it really isn't) to achieve
// the effect we want.
ActivityOptions opts = ActivityOptions.makeCustomAnimation(Animation.this,
R.anim.zoom_enter, R.anim.zoom_enter);
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
}
}; private OnClickListener mScaleUpListener = new OnClickListener() {
public void onClick(View v) {
// Create a scale-up animation that originates at the button
// being pressed.
ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(
v, 0, 0, v.getWidth(), v.getHeight());
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
}
}; private OnClickListener mZoomThumbnailListener = new OnClickListener() {
public void onClick(View v) {
// Create a thumbnail animation. We are going to build our thumbnail
// just from the view that was pressed. We make sure the view is
// not selected, because by the time the animation starts we will
// have finished with the selection of the tap.
v.setDrawingCacheEnabled(true);
v.setPressed(false);
v.refreshDrawableState();
Bitmap bm = v.getDrawingCache();
Canvas c = new Canvas(bm);
//c.drawARGB(255, 255, 0, 0);
ActivityOptions opts = ActivityOptions.makeThumbnailScaleUpAnimation(
v, bm, 0, 0);
// Request the activity be started, using the custom animation options.
startActivity(new Intent(Animation.this, AlertDialogSamples.class), opts.toBundle());
v.setDrawingCacheEnabled(false);
}
};

Activity跳轉動畫是指一個Activity跳轉到另外一個Activity的動畫效果,我們可以通過Acitivity的overridePendingTransition()方法實現跳轉動畫。

這個函數接受兩個參數:

  • 第一個參數為第一個Activity退出時的動畫
  • 第二個參數為第二個Activity進入時的動畫

這個方法必須在startActivity方法或者finish方法之后調用。

示例:

Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);

Android 4.1(API16)提供了一個新類ActivityOptions,用來實現Activity的切換動畫。

ActivityOptions類提供了三個方法

  • makeCustomAnimation() 創建一個動畫,由你自己的資源所定義:一個用來定義活動開啟的動畫,一個用來定義活動被關閉的動畫。
  • makeScaleUpAnimation() 創建一個動畫,能夠從屏幕指定的位置和指定的大小拉伸一個活動窗口。例如,當打開一個應用時,Android 4.1的主屏幕使用了這個方法。
  • makeThumbnailScaleUpAnimation() 創建一個動畫,能夠從屏幕指定的位置和提供的縮略圖拉伸一個活動窗口。例如,在Android 4.1的最近使用程序窗口中,當往回一個應用程序時使用了這個動畫。

通過這三個方法中的一個就可以創建一個ActivityOptions示例,然后調用toBundle()方法獲取一個Bundle對象,傳遞給startActivity方法。

示例:

Intent intent = new Intent(MainActivity.this,OtherActivity.class);
ActivityOptions opts = ActivityOptions.makeCustomAnimation(MainActivity.this, R.anim.fade, R.anim.hold);
startActivity(intent, opts.toBundle());

總結

以上是生活随笔為你收集整理的【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App-&gt;Activity-&gt;Animation Activity跳转动画 R.anim.&#215;&#215; overridePendingTransition ActivityOptions类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。