android 动画

版权所有,禁止匿名转载;禁止商业使用。

android的动画有两种  tween动画和Frame动画


tween动画;,透明度,缩放,旋转,平移效果


Animation   动画


AlphaAnimation 渐变透明度


RotateAnimation 画面旋转


ScaleAnimation 渐变尺寸缩放


TranslateAnimation 位置移动


AnimationSet  动画集


使用方式


在res文件下创建anim文件;


1,透明度效果


aniation_xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="1000"
    >
</alpha>

操作透明度xml的类


public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView images = (ImageView) this.findViewById(R.id.images);
    // 透明度
    //加载动画的文件
    Animation animation = AnimationUtils.loadAnimation(this,
        R.anim.aniation_xml);
        //设置执行后不变
    animation.setFillAfter(true);
    //启动动画
    images.startAnimation(animation);
}}

2,平移动画


<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
  android:fromXDelta="10%"
  android:fromYDelta="10%"
  android:toXDelta="50%p"
  android:toYDelta="50%p"
  android:duration="5000"
  >
</translate>
public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView images = (ImageView) this.findViewById(R.id.images);
    //加载动画的文件
     //平移
     Animation animation =AnimationUtils.loadAnimation(this,
     R.anim.translate_xml);
        //设置执行后不变
    animation.setFillAfter(true);
    //启动动画
    images.startAnimation(animation);

3,缩放动画   操作与上面类似


<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="5000"
  android:fromXScale="1.0"
  android:fromYScale="1.0"
  android:pivotX="50%"
  android:pivotY="50%"
  android:toXScale="1.4"
  android:toYScale="0.6" >
</scale>

4,旋转动画


<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
  android:fromDegrees="0"
  android:toDegrees="360"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="5000" 
  >
</rotate>

5,Frame动画  相当于GIF图片


在放图片的文件下创建文件存放资源 frame


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
   
    <item android:drawable="@drawable/ic_launcher" android:duration="1000" />
     <item android:drawable="@drawable/mainback" android:duration="2000" />
</animation-list>

duration 设置时间


animation-list 里面存放图片


操作Frame动画


@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView images = (ImageView) this.findViewById(R.id.images);
  
           //获得资源文件
     images.setBackgroundResource(R.drawable.frame);
     //设置背景图片
     AnimationDrawable animation=(AnimationDrawable)images.getBackground();
     animation.start();//启动


0 0