今天我们来看看Android中的动画效果,实例比较简单:
先贴代码:
package com.example.demo5_03_animation; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView mImg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImg = (ImageView) findViewById(R.id.img); Button btn_alpha = (Button) findViewById(R.id.btn_alpha); btn_alpha.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animAlpha = new AlphaAnimation(1.0f, 0.0f); animAlpha.setDuration(1000*3); mImg.startAnimation(animAlpha); } }); Button btn_scale = (Button) findViewById(R.id.btn_scale); btn_scale.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animScale = new ScaleAnimation(1.0f, 1.3f, 1.0f, 1.3f); animScale.setDuration(1000*3); mImg.startAnimation(animScale); } }); Button btn_translate = (Button) findViewById(R.id.btn_translate); btn_translate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animAlpha = new TranslateAnimation(-mImg.getWidth(), 0, 0, 0); animAlpha.setDuration(1000*1); mImg.startAnimation(animAlpha); } }); Button btn_rotate = (Button) findViewById(R.id.btn_rotate); btn_rotate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Animation animAlpha = new RotateAnimation(0.0f, 90.0f); animAlpha.setDuration(1000*3); mImg.startAnimation(animAlpha); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }也没有什么好讲的,这里主要是给大家提供实例。
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="2000" android:fromAlpha="1.0" android:toAlpha="0.5" /> </set>
Button btn_alpha = (Button) findViewById(R.id.btn_alpha); btn_alpha.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Animation animAlpha = new AlphaAnimation(1.0f, 0.0f); // animAlpha.setDuration(1000*3); // mImg.startAnimation(animAlpha); Animation animationAlpha = AnimationUtils.loadAnimation(mContext, R.anim.anim_alpha); mImg.startAnimation(animationAlpha); } });这里是把上一个例子中的透明度动画注释了,然后用xml加载方式写的。
好了,动画其实使用起来很简单,其他的请大家自己去研究!!
Android入门笔记 - 界面开发 - Animation
原文地址:http://blog.csdn.net/u013647382/article/details/41811273