安卓之动画制作

来源:互联网 发布:运动会班服创意知乎 编辑:程序博客网 时间:2024/06/05 21:51

1.知识图谱

(补间动画和帧动画)



1.补间动画的效果图:

(alpha透明度)                                                                                     (ratate旋转)


XML代码:

MainActivity.xml代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  5.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     tools:context="com.example.cookie.android0626animation.MainActivity">  
  9.   
  10.     <!--alpha透明度-->  
  11.     <ImageView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:background="@drawable/eat1"  
  15.         android:id="@+id/iv_main_image"  
  16.         android:layout_marginTop="100dp"/>  
  17.     <!--rotate旋转-->  
  18.     <!--scale缩放-->  
  19.     <!--translate移动-->  
  20.   
  21.     <HorizontalScrollView  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         >  
  25.     <LinearLayout  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:orientation="horizontal"  
  29.         >  
  30.   
  31.         <Button  
  32.             android:layout_width="0dp"  
  33.             android:layout_height="wrap_content"  
  34.             android:id="@+id/bu_image_alpha"  
  35.             android:layout_weight="1"  
  36.             android:onClick="operation"  
  37.             android:text="alpha"/>  
  38.   
  39.         <Button  
  40.             android:layout_width="0dp"  
  41.             android:layout_height="wrap_content"  
  42.             android:id="@+id/bu_image_rotate"  
  43.             android:layout_weight="1"  
  44.             android:onClick="operation"  
  45.             android:text="rotate"/>  
  46.   
  47.         <Button  
  48.             android:layout_width="0dp"  
  49.             android:layout_height="wrap_content"  
  50.             android:id="@+id/bu_image_scale"  
  51.             android:layout_weight="1"  
  52.             android:onClick="operation"  
  53.             android:text="scale"/>  
  54.   
  55.         <Button  
  56.             android:layout_width="0dp"  
  57.             android:layout_height="wrap_content"  
  58.             android:id="@+id/bu_image_translate"  
  59.             android:layout_weight="1"  
  60.             android:onClick="operation"  
  61.             android:text="translate"/>  
  62.   
  63.         <Button  
  64.             android:layout_width="0dp"  
  65.             android:layout_height="wrap_content"  
  66.             android:layout_weight="1"  
  67.             android:text="all"  
  68.             android:onClick="operation"  
  69.             android:id="@+id/bu_image_all"/>  
  70.   
  71.     </LinearLayout>  
  72.     </HorizontalScrollView>  
  73.   
  74. </LinearLayout>  



在Project里找到你写的现在写动画的项目,然后在res下新建一个anim文件夹,在该文件夹下新建所需动画效果的xml类

iv_alpha.xml代码

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromAlpha="1.0"  
  4.     android:toAlpha="0.3"  
  5.     android:duration="3000"  
  6.     android:fillAfter="true"  
  7.     >  
  8.       
  9. </alpha>  



iv_rotate.xml代码

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromDegrees="0"  
  4.     android:toDegrees="720"  
  5.     android:pivotX="50%"  
  6.     android:pivotY="50%"  
  7.     android:duration="3000"  
  8.     android:fillAfter="true"  
  9.     >  
  10.   
  11. </rotate>  


iv_scale.xml代码

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXScale="100%"  
  4.     android:fromYScale="100%"  
  5.     android:toXScale="50%"  
  6.     android:toYScale="50%"  
  7.     android:pivotX="50%"  
  8.     android:pivotY="50%"  
  9.     android:duration="3000"  
  10.     android:fillAfter="true"  
  11.     >  
  12.   
  13. </scale>  



iv_translate.xml代码

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <translate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXDelta="0"  
  4.     android:fromYDelta="0"  
  5.     android:toXDelta="50%"  
  6.     android:toYDelta="50%"  
  7.     android:duration="3000"  
  8.     android:fillAfter="true"  
  9.     >  
  10.   
  11. </translate>  


iv_all.xml代码

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="3000"  
  4.     android:fillAfter="true">  
  5.   
  6.     <alpha  
  7.         android:fromAlpha="1.0"  
  8.         android:toAlpha="0.3"  
  9.         ></alpha>  
  10.   
  11.     <rotate  
  12.         android:fromDegrees="0"  
  13.         android:toDegrees="360"  
  14.         android:pivotX="50%"  
  15.         android:pivotY="50%"  
  16.         ></rotate>  
  17.   
  18.     <scale  
  19.         android:fromXScale="100%"  
  20.         android:fromYScale="100%"  
  21.         android:toXScale="50%"  
  22.         android:toYScale="50%"  
  23.         android:pivotX="50%"  
  24.         android:pivotY="50%"  
  25.         ></scale>  
  26.   
  27.     <translate  
  28.         android:fromXDelta="0"  
  29.         android:fromYDelta="0"  
  30.         android:toXDelta="50%"  
  31.         android:toYDelta="50%"  
  32.         ></translate>  
  33.   
  34.   
  35.   
  36. </set>  



MainActivity.Java代码:

[html] view plain copy
  1. package com.example.cookie.android0626animation;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.AnimationUtils;  
  8. import android.widget.ImageView;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.   
  12.     private ImageView iv_main_image;  
  13.     private Animation animation;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         iv_main_image = (ImageView) findViewById(R.id.iv_main_image);  
  20.     }  
  21.   
  22.     public void operation(View view){  
  23.         switch (view.getId()) {  
  24.             case R.id.bu_image_alpha:  
  25.                 //加载动画  
  26.                 animation = AnimationUtils.loadAnimation(this, R.anim.iv_alpha);  
  27.                 //给图片开启动画效果  
  28.                 iv_main_image.startAnimation(animation);  
  29.                 break;  
  30.             case R.id.bu_image_rotate:  
  31.                 //加载动画  
  32.                 animation=AnimationUtils.loadAnimation(this,R.anim.iv_rotate);  
  33.                 //给图片开启动画效果  
  34.                 iv_main_image.startAnimation(animation);  
  35.                 break;  
  36.             case R.id.bu_image_scale:  
  37.                 animation=AnimationUtils.loadAnimation(this,R.anim.iv_scale);  
  38.                 //给图片开启动画效果  
  39.                 iv_main_image.startAnimation(animation);  
  40.                 break;  
  41.             case R.id.bu_image_translate:  
  42.                 //加载动画  
  43.                 animation=AnimationUtils.loadAnimation(this,R.anim.iv_translate);  
  44.                 //给图片开启动画效果  
  45.                 iv_main_image.startAnimation(animation);  
  46.                 break;  
  47.             case R.id.bu_image_all:  
  48.                 //加载动画  
  49.                 animation=AnimationUtils.loadAnimation(this,R.anim.iv_all);  
  50.                 //给图片开启动画效果  
  51.                 iv_main_image.startAnimation(animation);  
  52.                 break;  
  53.         }  
  54.     }  
  55.   
  56.   
  57.   
  58. }