Android实战简易教程<六十三>(动画实现唱片播放界面)
2015-10-16 09:02
639 查看
对于Android动画的使用,唱片播放是十分经典的一例,我们通过实现唱片播放效果来对Android动画进行学习,具有很高的趣味性和实用性。
1.首先我们定义一下布局文件-pan_layout.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_pan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/game_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="歌曲"
android:textColor="@color/white"
android:textSize="20sp" />
</FrameLayout>
<FrameLayout
android:layout_width="260dp"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/game_disc" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/game_disc_light" />
<ImageButton
android:id="@+id/btn_play_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/play_button_icon" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="50sp"
android:layout_height="140sp"
android:layout_gravity="right"
android:src="@drawable/index_pin" /><!-- 拨杆 -->
</FrameLayout>
</LinearLayout>
这里我们广泛使用了FrameLayout布局,可以进行嵌套。
2.定义动画文件,这里我们共使用了三个动画问价
a.rotate.xml(中间盘片的旋转动画)
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 一次2400毫秒,重复3次 -->
<rotate
android:duration="2400"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="3"
android:toDegrees="359" />
</set>
b.rotate_45.xml(拨杆进入盘片动画):
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="300"
android:fromDegrees="0"
android:pivotX="45%"
android:pivotY="10%"
android:repeatCount="0"
android:toDegrees="20" />
</set>
c.rotate_d_45.xml(拨杆离开盘片动画):
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="300"
android:fromDegrees="20"
android:pivotX="45%"
android:pivotY="10%"
android:repeatCount="0"
android:toDegrees="0" />
</set>
3.MainActivity.java:
[java] view
plaincopy
package com.yayun.guessmusic.ui;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.yayun.guessmusic.R;
public class MainActivity extends Activity {
// 唱片相关动画
private Animation mPanAnim;
private LinearInterpolator mPanLin;//动画匀速播放
private Animation mBarInAnim;
private LinearInterpolator mBarInLin;//动画匀速播放
private Animation mBarOutAnim;
private LinearInterpolator mBarOutLin;//动画匀速播放
// 唱片控件
private ImageView mViewPan;
// 拨杆控件
private ImageView mViewPanBar;
// Play 按键事件
private ImageButton mBtnPlayStart;
// 当前动画是否正在运行
private boolean mIsRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnPlayStart = (ImageButton)findViewById(R.id.btn_play_start);
mBtnPlayStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
handlePlayButton();
}
});
mViewPanBar = (ImageView)findViewById(R.id.imageView2);
mPanLin = new LinearInterpolator();
mPanAnim.setInterpolator(mPanLin);
mPanAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 拨杆开始动画
mViewPanBar.startAnimation(mBarOutAnim);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mBarInAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_45);
mBarInLin = new LinearInterpolator();
mBarInAnim.setFillAfter(true);
mBarInAnim.setInterpolator(mBarInLin);
mBarInAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 唱片动画
mViewPan.startAnimation(mPanAnim);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mBarOutAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_d_45);
mBarOutLin = new LinearInterpolator();
mBarOutAnim.setFillAfter(true);
mBarOutAnim.setInterpolator(mBarOutLin);
mBarOutAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 整套动画播放完毕
mIsRunning = false;
mBtnPlayStart.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
/**
* 处理圆盘中间的播放按钮,就是开始播放音乐
*/
private void handlePlayButton() {
if (mViewPanBar != null) {
if (!mIsRunning) {
mIsRunning = true;
// 拨杆进入动画,开始按钮不可见
mViewPanBar.startAnimation(mBarInAnim);
mBtnPlayStart.setVisibility(View.INVISIBLE);
}
}
}
@Override
public void onPause() {
mViewPan.clearAnimation();
super.onPause();
}
}
运行实例:
![](https://img-blog.csdn.net/20151008105600216?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
源码下载
喜欢的朋友关注我!谢谢
1.首先我们定义一下布局文件-pan_layout.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_pan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="30dp"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/game_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="歌曲"
android:textColor="@color/white"
android:textSize="20sp" />
</FrameLayout>
<FrameLayout
android:layout_width="260dp"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/game_disc" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/game_disc_light" />
<ImageButton
android:id="@+id/btn_play_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/play_button_icon" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="50sp"
android:layout_height="140sp"
android:layout_gravity="right"
android:src="@drawable/index_pin" /><!-- 拨杆 -->
</FrameLayout>
</LinearLayout>
这里我们广泛使用了FrameLayout布局,可以进行嵌套。
2.定义动画文件,这里我们共使用了三个动画问价
a.rotate.xml(中间盘片的旋转动画)
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 一次2400毫秒,重复3次 -->
<rotate
android:duration="2400"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="3"
android:toDegrees="359" />
</set>
b.rotate_45.xml(拨杆进入盘片动画):
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="300"
android:fromDegrees="0"
android:pivotX="45%"
android:pivotY="10%"
android:repeatCount="0"
android:toDegrees="20" />
</set>
c.rotate_d_45.xml(拨杆离开盘片动画):
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="300"
android:fromDegrees="20"
android:pivotX="45%"
android:pivotY="10%"
android:repeatCount="0"
android:toDegrees="0" />
</set>
3.MainActivity.java:
[java] view
plaincopy
package com.yayun.guessmusic.ui;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.yayun.guessmusic.R;
public class MainActivity extends Activity {
// 唱片相关动画
private Animation mPanAnim;
private LinearInterpolator mPanLin;//动画匀速播放
private Animation mBarInAnim;
private LinearInterpolator mBarInLin;//动画匀速播放
private Animation mBarOutAnim;
private LinearInterpolator mBarOutLin;//动画匀速播放
// 唱片控件
private ImageView mViewPan;
// 拨杆控件
private ImageView mViewPanBar;
// Play 按键事件
private ImageButton mBtnPlayStart;
// 当前动画是否正在运行
private boolean mIsRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnPlayStart = (ImageButton)findViewById(R.id.btn_play_start);
mBtnPlayStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
handlePlayButton();
}
});
mViewPanBar = (ImageView)findViewById(R.id.imageView2);
mPanLin = new LinearInterpolator();
mPanAnim.setInterpolator(mPanLin);
mPanAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 拨杆开始动画
mViewPanBar.startAnimation(mBarOutAnim);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mBarInAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_45);
mBarInLin = new LinearInterpolator();
mBarInAnim.setFillAfter(true);
mBarInAnim.setInterpolator(mBarInLin);
mBarInAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 唱片动画
mViewPan.startAnimation(mPanAnim);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mBarOutAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_d_45);
mBarOutLin = new LinearInterpolator();
mBarOutAnim.setFillAfter(true);
mBarOutAnim.setInterpolator(mBarOutLin);
mBarOutAnim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// 整套动画播放完毕
mIsRunning = false;
mBtnPlayStart.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
/**
* 处理圆盘中间的播放按钮,就是开始播放音乐
*/
private void handlePlayButton() {
if (mViewPanBar != null) {
if (!mIsRunning) {
mIsRunning = true;
// 拨杆进入动画,开始按钮不可见
mViewPanBar.startAnimation(mBarInAnim);
mBtnPlayStart.setVisibility(View.INVISIBLE);
}
}
}
@Override
public void onPause() {
mViewPan.clearAnimation();
super.onPause();
}
}
运行实例:
源码下载
喜欢的朋友关注我!谢谢
相关文章推荐
- Android Screen Monitor
- android activity与任务原理解析及设置
- Android Api Demos登顶之路(九十九)Text-->Animation->Push
- Android动画学习(缓动动画与属性动画的区别)
- Android动画学习(概述):
- Failed to create directory C:\ Program Files\Android\android-sdk\temp
- Android经典底部选项卡集成方式之一
- 【转】Android必备的Java知识点
- Android计时器和倒计时
- android 16 带返回值的activity
- 定制Android系统开发之一——提供接口的方式
- android 15 activity跳转
- Android_Android系统服务—Vibrator(振动器)
- android 14 进度条和拖动条
- Android Studio常用快捷键
- Android中View的事件分发和拦截机制
- android 13 5种click事件不同实现方式 比较
- Android 修改项目名称
- 四种途径将HTML5 web应用变成android应用
- 适配不同的Android屏幕