Android实战简易教程<六十三>(动画实现唱片播放界面)

来源:互联网 发布:过期域名 编辑:程序博客网 时间:2024/05/16 14:20

对于Android动画的使用,唱片播放是十分经典的一例,我们通过实现唱片播放效果来对Android动画进行学习,具有很高的趣味性和实用性。

1.首先我们定义一下布局文件-pan_layout.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout_pan"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:layout_centerHorizontal="true"  
  7.     android:layout_centerVertical="true"  
  8.     android:layout_marginTop="30dp"  
  9.     android:gravity="center"  
  10.     android:orientation="vertical" >  
  11.   
  12.     <FrameLayout  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content" >  
  15.   
  16.         <ImageView  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:src="@drawable/game_title" />  
  20.   
  21.         <TextView  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_gravity="center"  
  25.             android:text="歌曲"  
  26.             android:textColor="@color/white"  
  27.             android:textSize="20sp" />  
  28.     </FrameLayout>  
  29.   
  30.     <FrameLayout  
  31.         android:layout_width="260dp"  
  32.         android:layout_height="wrap_content" >  
  33.   
  34.         <ImageView  
  35.             android:id="@+id/imageView1"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             android:layout_gravity="center"  
  39.             android:src="@drawable/game_disc" />  
  40.   
  41.         <ImageView  
  42.             android:layout_width="wrap_content"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_gravity="center"  
  45.             android:src="@drawable/game_disc_light" />  
  46.   
  47.         <ImageButton  
  48.             android:id="@+id/btn_play_start"  
  49.             android:layout_width="wrap_content"  
  50.             android:layout_height="wrap_content"  
  51.             android:layout_gravity="center"  
  52.             android:background="@drawable/play_button_icon" />  
  53.   
  54.         <ImageView  
  55.             android:id="@+id/imageView2"  
  56.             android:layout_width="50sp"  
  57.             android:layout_height="140sp"  
  58.             android:layout_gravity="right"  
  59.             android:src="@drawable/index_pin" /><!-- 拨杆 -->  
  60.     </FrameLayout>  
  61.   
  62. </LinearLayout>  

这里我们广泛使用了FrameLayout布局,可以进行嵌套。

2.定义动画文件,这里我们共使用了三个动画问价

a.rotate.xml(中间盘片的旋转动画)

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3. <!-- 一次2400毫秒,重复3次 -->  
  4.     <rotate  
  5.         android:duration="2400"  
  6.         android:fromDegrees="0"  
  7.         android:pivotX="50%"  
  8.         android:pivotY="50%"  
  9.         android:repeatCount="3"  
  10.         android:toDegrees="359" />  
  11.   
  12. </set>  

b.rotate_45.xml(拨杆进入盘片动画):

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <rotate  
  5.         android:duration="300"  
  6.         android:fromDegrees="0"  
  7.         android:pivotX="45%"  
  8.         android:pivotY="10%"  
  9.         android:repeatCount="0"  
  10.         android:toDegrees="20" />  
  11.   
  12. </set>  

c.rotate_d_45.xml(拨杆离开盘片动画):

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <rotate  
  5.         android:duration="300"  
  6.         android:fromDegrees="20"  
  7.         android:pivotX="45%"  
  8.         android:pivotY="10%"  
  9.         android:repeatCount="0"  
  10.         android:toDegrees="0" />  
  11.   
  12. </set>  
3.MainActivity.java:

[java] view plaincopy
  1. package com.yayun.guessmusic.ui;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.view.animation.Animation;  
  8. import android.view.animation.Animation.AnimationListener;  
  9. import android.view.animation.AnimationUtils;  
  10. import android.view.animation.LinearInterpolator;  
  11. import android.widget.ImageButton;  
  12. import android.widget.ImageView;  
  13.   
  14. import com.yayun.guessmusic.R;  
  15.   
  16.   
  17. public class MainActivity extends Activity {  
  18.   
  19.     // 唱片相关动画  
  20.     private Animation mPanAnim;  
  21.     private LinearInterpolator mPanLin;//动画匀速播放  
  22.       
  23.     private Animation mBarInAnim;  
  24.     private LinearInterpolator mBarInLin;//动画匀速播放  
  25.       
  26.     private Animation mBarOutAnim;  
  27.     private LinearInterpolator mBarOutLin;//动画匀速播放  
  28.       
  29.     // 唱片控件  
  30.     private ImageView mViewPan;  
  31.     // 拨杆控件  
  32.     private ImageView mViewPanBar;  
  33.       
  34.     // Play 按键事件  
  35.     private ImageButton mBtnPlayStart;  
  36.       
  37.     // 当前动画是否正在运行  
  38.     private boolean mIsRunning = false;  
  39.       
  40.     @Override  
  41.     protected void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.activity_main);  
  44.           
  45.         mBtnPlayStart = (ImageButton)findViewById(R.id.btn_play_start);  
  46.         mBtnPlayStart.setOnClickListener(new View.OnClickListener() {  
  47.               
  48.             @Override  
  49.             public void onClick(View arg0) {  
  50.                 handlePlayButton();  
  51.             }  
  52.         });  
  53.           
  54.         mViewPanBar = (ImageView)findViewById(R.id.imageView2);  
  55.           
  56.         mPanLin = new LinearInterpolator();  
  57.         mPanAnim.setInterpolator(mPanLin);  
  58.         mPanAnim.setAnimationListener(new AnimationListener() {  
  59.   
  60.             @Override  
  61.             public void onAnimationStart(Animation animation) {  
  62.   
  63.             }  
  64.   
  65.             @Override  
  66.             public void onAnimationEnd(Animation animation) {  
  67.                 // 拨杆开始动画  
  68.                 mViewPanBar.startAnimation(mBarOutAnim);  
  69.   
  70.             }  
  71.   
  72.             @Override  
  73.             public void onAnimationRepeat(Animation animation) {  
  74.   
  75.             }  
  76.         });  
  77.           
  78.         mBarInAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_45);  
  79.         mBarInLin = new LinearInterpolator();  
  80.         mBarInAnim.setFillAfter(true);  
  81.         mBarInAnim.setInterpolator(mBarInLin);  
  82.         mBarInAnim.setAnimationListener(new AnimationListener() {  
  83.   
  84.             @Override  
  85.             public void onAnimationStart(Animation animation) {  
  86.   
  87.             }  
  88.   
  89.             @Override  
  90.             public void onAnimationEnd(Animation animation) {  
  91.                 // 唱片动画  
  92.                   
  93.                 mViewPan.startAnimation(mPanAnim);  
  94.                   
  95.             }  
  96.   
  97.             @Override  
  98.             public void onAnimationRepeat(Animation animation) {  
  99.   
  100.             }  
  101.         });  
  102.           
  103.         mBarOutAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_d_45);  
  104.         mBarOutLin = new LinearInterpolator();  
  105.         mBarOutAnim.setFillAfter(true);  
  106.         mBarOutAnim.setInterpolator(mBarOutLin);  
  107.         mBarOutAnim.setAnimationListener(new AnimationListener() {  
  108.   
  109.             @Override  
  110.             public void onAnimationStart(Animation animation) {  
  111.   
  112.             }  
  113.   
  114.             @Override  
  115.             public void onAnimationEnd(Animation animation) {  
  116.                 // 整套动画播放完毕  
  117.                 mIsRunning = false;  
  118.                 mBtnPlayStart.setVisibility(View.VISIBLE);  
  119.   
  120.             }  
  121.   
  122.             @Override  
  123.             public void onAnimationRepeat(Animation animation) {  
  124.                   
  125.             }  
  126.         });  
  127.           
  128.           
  129.     }  
  130.       
  131.     /** 
  132.      * 处理圆盘中间的播放按钮,就是开始播放音乐 
  133.      */  
  134.     private void handlePlayButton() {  
  135.         if (mViewPanBar != null) {  
  136.             if (!mIsRunning) {  
  137.                 mIsRunning = true;  
  138.                   
  139.                 // 拨杆进入动画,开始按钮不可见  
  140.                 mViewPanBar.startAnimation(mBarInAnim);  
  141.                 mBtnPlayStart.setVisibility(View.INVISIBLE);  
  142.             }  
  143.         }  
  144.     }  
  145.       
  146.     @Override  
  147.     public void onPause() {  
  148.         mViewPan.clearAnimation();  
  149.           
  150.         super.onPause();  
  151.     }  
  152.       
  153. }  



运行实例:


源码下载

喜欢的朋友关注我!谢谢


0 0
原创粉丝点击