这篇和最早写的那篇都是实现动画效果而上一篇Android上运用Animation开发简单动画程序是通过代码实现的补间动画
主程序的布局以及实现效果都和代码实现的那一片一致,所以在这里只给出动画的xml文件内容,以及MainActivity的内容
Alpha.xml 明暗变化
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
rotate.xml 旋转变化
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="2400" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:repeatCount="3" android:toDegrees="359" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <scale android:duration="1000" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.0" android:toYScale="0.0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="2000" android:fromXDelta="0%" android:fromYDelta="0%" android:toXDelta="100%" android:toYDelta="100%" /> </set>
最后给出MainActivity
public class MainActivity extends Activity { // 旋转按钮 private Button mRotateBtn = null; // 缩放按钮 private Button mScaleBtn = null; // 淡入淡出按钮 private Button mAlphaBtn = null; // 移动按钮 private Button mTranslateBtn = null; // 图片视图 private ImageView image = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animation); // 初始化imageView image = (ImageView)findViewById(R.id.image); // 初始化旋转按钮 mRotateBtn = (Button) findViewById(R.id.rotatebtn); mRotateBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation anim = AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate); anim.setInterpolator(new LinearInterpolator()); image.startAnimation(anim); } }); // 初始化缩放按钮 mScaleBtn = (Button)findViewById(R.id.scalebtn); mScaleBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation anim = AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale); image.startAnimation(anim); } }); // 初始化淡入淡出按钮 mAlphaBtn = (Button)findViewById(R.id.alphabtn); mAlphaBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation anim = AnimationUtils.loadAnimation(MainActivity.this,R.anim.alpha); image.startAnimation(anim); } }); // 初始化移动按钮 mTranslateBtn = (Button)findViewById(R.id.translatebtn); mTranslateBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Animation anim = AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate); image.startAnimation(anim); } }); } }
效果见 点击打开链接 原文链接:https://www.f2er.com/xml/297256.html