由于代码比较简单,这里主要贴出代码,重要的部分有注释

1.文件结构

2.activity_main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>                    android:background="@android:color/transparent"        android:onClick="isPlayOrPause"        android:src="@drawable/yy"/>

3.MainActivity.java文件

package com.example.greetingcard;import androidx.appcompat.app.AppCompatActivity;import androidx.core.app.ActivityCompat;import androidx.core.content.ContextCompat;import android.Manifest;import android.content.pm.PackageManager;import android.media.MediaPlayer;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.view.animation.RotateAnimation;import android.widget.ImageButton;public class MainActivity extends AppCompatActivity {    private MediaPlayer mediaPlayer;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void isPlayOrPause(View view){        ImageButton imageButton = (ImageButton)view;        //RotateAnimation animation = new RotateAnimation(0, 360);        //animation.setDuration(100000);        //animation.setRepeatCount(Animation.INFINITE);        //animation.setRepeatMode(Animation.RESTART);        if(mediaPlayer == null){            //imageButton.startAnimation(animation);            //获取应用内的音乐文件            mediaPlayer = MediaPlayer.create(this, R.raw.en);            mediaPlayer.start();        }        else if(mediaPlayer.isPlaying()){            mediaPlayer.pause();            //imageButton.clearAnimation();        }        else{            mediaPlayer.start();            //imageButton.startAnimation(animation);        }    }}

 

效果:

 

更多相关文章

  1. Android通过源码编译apk获得系统权限
  2. Android(安卓)白板代码实现
  3. [摘]android 入门xml布局文件
  4. Android通过源码编译apk获得系统权限
  5. 关于android xml文件中 android:id="@+id/my_button"的含义疑问
  6. Android(安卓)选择文件并返回路径
  7. Android(安卓)android:gravity属性介绍及效果图
  8. Android动画
  9. android自定义title

随机推荐

  1. 编写第一个Android应用
  2. 推荐几个非常有用的开发工具之Android(安
  3. android 获取sim卡信息
  4. Android仿人人客户端(v5.7.1)——网络模块
  5. android 关于Location of the Android(安
  6. Android(安卓)listView 继承ListActivity
  7. 微软支持Android的原因:打击Sun
  8. Android使用开源框架ANDROID-IMAGE-INDIC
  9. Android各种访问权限Permission详解
  10. android弹出框