Android 中 Tweened animation的實例詳解
Tweened animation有四種類型,下面主要介紹Scale類型。
運行效果如下:

Android SDK提供了2種方法:直接從XML資源中讀取Animation,使用Animation子類的構造函數來初始化Animation對象,第二種方法在看了Android SDK中各個類的說明就知道如何使用了,下面簡要說明從XML資源中讀取Animation。XML資源中的動畫文件animation.xml內容為:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.4" android:fromYScale="0.0" android:toYScale="1.4" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:startOffset="3000" android:duration="3000" android:repeatCount="4"/> </set>
主文件ScaleAnimation.java內容為:
package com.android.animation; import android.app.Activity; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; public class TestAnimation extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageView = (ImageView) findViewById(R.id.imageview); Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation); imageView.startAnimation(animation); } } 程序很容易看懂,主要為了說明伸縮動畫效果而沒有增加復雜性。值得說明的是,本人在測試階段,錯誤的認為不需要布局文件,把行setContentView(R.layout.main);去掉,導致程序運行一直出錯。其實動畫也需要首先把布局文件加載到Activity里面,然后對布局里面的控件增加動畫。
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答