上文中在代码中设置了补间动画,在本文中将使用XML来定义补间动画,代码如下:
动画XML:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="3000" android:fromAlpha="1" android:repeatCount="-1" android:repeatMode="reverse" android:toAlpha="0.3" /> <rotate android:duration="3000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="-1" android:repeatMode="reverse" android:toDegrees="360" /> <scale android:duration="3000" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:repeatCount="-1" android:repeatMode="reverse" android:toXScale="0" android:toYScale="0" /> <translate android:duration="3000" android:fillAfter="true" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="300" android:toYDelta="300" /> </set>
Activity:
package com.lovo.testtween; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class TestTweenActivity extends Activity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.main_iv); Button startBtn = (Button) findViewById(R.id.main_btn_start); startBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 加载动画资源 Animation animation = AnimationUtils.loadAnimation( TestTweenActivity.this,R.anim.test_tween_anim); // 启动动画 imageView.startAnimation(animation); } }); Button clearBtn = (Button) findViewById(R.id.main_btn_clear); clearBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 清除动画效果 imageView.clearAnimation(); } }); } }
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/main_btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始动画" /> <Button android:id="@+id/main_btn_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除动画" /> <ImageView android:id="@+id/main_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/freedom1" /> </LinearLayout>原文链接:https://www.f2er.com/xml/300207.html