D1Android-钢琴模拟实现
生活随笔
收集整理的這篇文章主要介紹了
D1Android-钢琴模拟实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、效果圖:
二、操作步驟:
java功能:①M(fèi)ainActivity.java
package cn.itcast.e24_pianoplayer_application;import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.os.Bundle;import android.view.View; import android.widget.ImageView;import java.util.HashMap;public class MainActivity extends Activity implements View.OnClickListener {private SoundPool soundpool;private HashMap<Integer,Integer> map = new HashMap<>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化界面控件,并為控件添加點(diǎn)擊事件的監(jiān)聽器ImageView iv_do = findViewById(R.id.iv_do);ImageView iv_re = findViewById(R.id.iv_re);ImageView iv_mi = findViewById(R.id.iv_mi);ImageView iv_fa = findViewById(R.id.iv_fa);ImageView iv_so = findViewById(R.id.iv_so);ImageView iv_la = findViewById(R.id.iv_la);ImageView iv_si = findViewById(R.id.iv_si);iv_do.setOnClickListener(this);iv_re.setOnClickListener(this);iv_mi.setOnClickListener(this);iv_fa.setOnClickListener(this);iv_so.setOnClickListener(this);iv_la.setOnClickListener(this);iv_si.setOnClickListener(this);initSoundPool();//初始化SoundPool}private void initSoundPool() {if(soundpool == null){//創(chuàng)建SoundPool對(duì)象soundpool = new SoundPool(7, AudioManager.STREAM_SYSTEM, 0);}//加載音頻文件,并將文件存儲(chǔ)到HashMap集合中map.put(R.id.iv_do,soundpool.load(this,R.raw.music_do,1));map.put(R.id.iv_re,soundpool.load(this,R.raw.music_re,1));map.put(R.id.iv_mi,soundpool.load(this,R.raw.music_mi,1));map.put(R.id.iv_fa,soundpool.load(this,R.raw.music_fa,1));map.put(R.id.iv_so,soundpool.load(this,R.raw.music_so,1));map.put(R.id.iv_la,soundpool.load(this,R.raw.music_la,1));map.put(R.id.iv_si,soundpool.load(this,R.raw.music_si,1));}@Overridepublic void onClick(View v) {play(v.getId());}private void play(int i){soundpool.play(map.get(i),1.0f,1.0f,0,0,1.0f); //播放音頻}@Overrideprotected void onDestroy() {super.onDestroy();if (soundpool != null) {soundpool.autoPause(); //暫停播放音頻soundpool.release(); //釋放Soundpool對(duì)象占用的資源soundpool = null;}} }?背景布局:②ic_launcher_background.xml
<?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="108dp"android:height="108dp"android:viewportWidth="108"android:viewportHeight="108"><pathandroid:fillColor="#008577"android:pathData="M0,0h108v108h-108z" /><pathandroid:fillColor="#00000000"android:pathData="M9,0L9,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M19,0L19,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M29,0L29,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M39,0L39,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M49,0L49,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M59,0L59,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M69,0L69,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M79,0L79,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M89,0L89,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M99,0L99,108"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,9L108,9"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,19L108,19"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,29L108,29"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,39L108,39"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,49L108,49"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,59L108,59"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,69L108,69"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,79L108,79"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,89L108,89"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M0,99L108,99"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M19,29L89,29"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M19,39L89,39"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M19,49L89,49"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M19,59L89,59"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M19,69L89,69"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M19,79L89,79"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M29,19L29,89"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M39,19L39,89"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M49,19L49,89"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M59,19L59,89"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M69,19L69,89"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /><pathandroid:fillColor="#00000000"android:pathData="M79,19L79,89"android:strokeWidth="0.8"android:strokeColor="#33FFFFFF" /> </vector>各個(gè)音符(1/7):③icon_do_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/icon_do" android:state_pressed="false"/><item android:drawable="@drawable/icon_do_pressed"android:state_pressed="true"/> </selector>三、代碼講解:待填……
四、代碼資源:
Android鋼琴模擬器-Android文檔類資源-CSDN文庫(kù)?
總結(jié)
以上是生活随笔為你收集整理的D1Android-钢琴模拟实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑开机卡在android,如何修复它:
- 下一篇: 常规调幅系统matlab结果,基于MAT