標籤:android style blog http io color ar java sp
我們不得不飲食、睡眠、遊玩、戀愛,也就是說,我們不得不接觸生活中最甜蜜的事情,不過我們必須不屈服於這些事物。—— 居裡夫人
本講內容:補間動畫 Tween Animation
前面我們只學習了實現單個動畫效果,本講將同時實現多個動畫效果:
我們通過一個例子感受一下,代碼的講解都寫在注釋裡了
下面是res/layout/activity_main.xml 布局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.text.MainActivity$PlaceholderFragment" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_gravity="center" android:src="@drawable/c4" /> <Button android:id="@+id/b" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="測試動畫效果" /></LinearLayout>
下面是建立的res/anim/alpha_rotate.xml檔案
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:shareInterpolator="true"><alpha android:fromAlpha="1.0"android:toAlpha="0.0"android:startOffset="500"android:duration="2000" /><rotate android:fromDegrees="0"android:toDegrees="360"android:pivotX="50%"android:pivotY="50%"android:duration="2000" /></set>
android:shareInterpolator="true" 表示裡面的一系列動畫都應用 <span style="font-family: Arial;">interpolator</span>
下面是MainActivity.java主介面檔案:
public class MainActivity extends Activity {private Button b;private ImageView image;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);b = (Button) findViewById(R.id.b);image = (ImageView) findViewById(R.id.image);AnimationButtonListener listener=new AnimationButtonListener();b.setOnClickListener(listener);}private class AnimationButtonListener implements OnClickListener {@Overridepublic void onClick(View v) {Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha_rotate);image.startAnimation(animation);}}}
下面是運行結果:可以看出一邊旋轉一邊漸層透明度
本講到這裡,謝謝大家!
第二十八講:Android之Animation(三)