Android實現(xiàn)簡易版彈鋼琴效果
本文實例為大家分享了Android實現(xiàn)彈鋼琴效果展示的具體代碼,供大家參考,具體內(nèi)容如下
目標(biāo)效果:

1.drawable下新建button_selector.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_pressed" android:state_pressed="true"></item> <item android:drawable="@drawable/button"></item> </selector>
2.drawable下新建button.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" > </corners> <stroke android:width="2dp" android:color="#605C59" /> <gradient android:angle="270" android:endColor="#FFFFFF" android:startColor="#F5F5F5" /> </shape>
3.drawable下新建button_pressed.xml頁面:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#A4A4A4" /> <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" > </corners> <stroke android:width="2dp" android:color="#605C59" /> </shape>
4.新建PanioMusic.java類
package com.example.weixu.view;
/**
* 音樂播放幫助類
*/
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import com.example.weixu.playpanio.R;
public class PanioMusic {
// 資源文件
int Music[] = {R.raw.do1, R.raw.re2, R.raw.mi3, R.raw.fa4, R.raw.sol5,
R.raw.la6, R.raw.si7,};
SoundPool soundPool;
HashMap<Integer, Integer> soundPoolMap;
public PanioMusic(Context context) {
soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 100);
soundPoolMap = new HashMap<Integer, Integer>();
for (int i = 0; i < Music.length; i++) {
soundPoolMap.put(i, soundPool.load(context, Music[i], 1));
}
}
public int soundPlay(int no) {
return soundPool.play(soundPoolMap.get(no), 100, 100, 1, 0, 1.0f);
}
public int soundOver() {
return soundPool.play(soundPoolMap.get(1), 100, 100, 1, 0, 1.0f);
}
@Override
protected void finalize() throws Throwable {
soundPool.release();
super.finalize();
}
}
5.activity_main.xml頁面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/llparent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:id="@+id/llKeys" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:orientation="horizontal" android:padding="10dp" > <Button android:id="@+id/btPanioOne" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/button" android:text="1" /> <Button android:id="@+id/btPanioTwo" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/button" android:text="2" /> <Button android:id="@+id/btPanioThree" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/button" android:text="3" /> <Button android:id="@+id/btPanioFour" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/button" android:text="4" /> <Button android:id="@+id/btPanioFive" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/button" android:text="5" /> <Button android:id="@+id/btPanioSix" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/button" android:text="6" /> <Button android:id="@+id/btPanioSeven" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/button" android:text="7" /> </LinearLayout> </LinearLayout>
6.MainActivity.java頁面:
package com.example.weixu.playpanio;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import com.example.weixu.view.PanioMusic;
public class MainActivity extends Activity {
private Button button[];// 按鈕數(shù)組
private PanioMusic utils;// 工具類
private View parent;// 父視圖
private int buttonId[];// 按鈕id
private boolean havePlayed[];// 是否已經(jīng)播放了聲音,當(dāng)手指在同一個按鈕內(nèi)滑動,且已經(jīng)發(fā)聲,就為true
private View keys;// 按鈕們所在的視圖
private int pressedkey[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
parent = (View) findViewById(R.id.llparent);
parent.setClickable(true);
parent.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int temp;
int tempIndex;
int pointercount;
pointercount = event.getPointerCount();
for (int count = 0; count < pointercount; count++) {
boolean moveflag = false;// 標(biāo)記是否是在按鍵上移動
temp = isInAnyScale(event.getX(count), event.getY(count),
button);
if (temp != -1) {// 事件對應(yīng)的是當(dāng)前點
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// // 單獨一根手指或最先按下的那個
// pressedkey = temp;
case MotionEvent.ACTION_POINTER_DOWN:
Log.i("--", "count" + count);
pressedkey[count] = temp;
if (!havePlayed[temp]) {// 在某個按鍵范圍內(nèi)
button[temp]
.setBackgroundResource(R.drawable.button_pressed);
// 播放音階
utils.soundPlay(temp);
Log.i("--", "sound" + temp);
havePlayed[temp] = true;
}
break;
case MotionEvent.ACTION_MOVE:
temp = pressedkey[count];
for (int i = temp + 1; i >= temp - 1; i--) {
// 當(dāng)在兩端的按鈕時,會有一邊越界
if (i < 0 || i >= button.length) {
continue;
}
if (isInScale(event.getX(count),
event.getY(count), button[i])) {// 在某個按鍵內(nèi)
moveflag = true;
if (i != temp) {// 在相鄰按鍵內(nèi)
boolean laststill = false;
boolean nextstill = false;
// 假設(shè)手指已經(jīng)從上一個位置抬起,但是沒有真的抬起,所以不移位
pressedkey[count] = -1;
for (int j = 0; j < pointercount; j++) {
if (pressedkey[j] == temp) {
laststill = true;
}
if (pressedkey[j] == i) {
nextstill = true;
}
}
if (!nextstill) {// 移入的按鍵沒有按下
// 設(shè)置當(dāng)前按鍵
button[i]
.setBackgroundResource(R.drawable.button_pressed);
// 發(fā)音
utils.soundPlay(i);
havePlayed[i] = true;
}
pressedkey[count] = i;
if (!laststill) {// 沒有手指按在上面
// 設(shè)置上一個按鍵
button[temp]
.setBackgroundResource(R.drawable.button);
havePlayed[temp] = false;
}
break;
}
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
// 事件與點對應(yīng)
tempIndex = event.getActionIndex();
if (tempIndex == count) {
Log.i("--", "index" + tempIndex);
boolean still = false;
// 當(dāng)前點已抬起
for (int t = count; t < 5; t++) {
if (t != 4) {
if (pressedkey[t + 1] >= 0) {
pressedkey[t] = pressedkey[t + 1];
} else {
pressedkey[t] = -1;
}
} else {
pressedkey[t] = -1;
}
}
for (int i = 0; i < pressedkey.length; i++) {// 是否還有其他點
if (pressedkey[i] == temp) {
still = true;
break;
}
}
if (!still) {// 已經(jīng)沒有手指按在該鍵上
button[temp]
.setBackgroundResource(R.drawable.button);
havePlayed[temp] = false;
Log.i("--", "button" + temp + "up");
}
break;
}
}
}
//
if (event.getActionMasked() == MotionEvent.ACTION_MOVE
&& !moveflag) {
if (pressedkey[count] != -1) {
button[pressedkey[count]]
.setBackgroundResource(R.drawable.button);
havePlayed[pressedkey[count]] = false;
}
}
}
return false;
}
});
keys = (View) findViewById(R.id.llKeys);
}
private void init() {
// 新建工具類
utils = new PanioMusic(getApplicationContext());
// 按鈕資源Id
buttonId = new int[7];
buttonId[0] = R.id.btPanioOne;
buttonId[1] = R.id.btPanioTwo;
buttonId[2] = R.id.btPanioThree;
buttonId[3] = R.id.btPanioFour;
buttonId[4] = R.id.btPanioFive;
buttonId[5] = R.id.btPanioSix;
buttonId[6] = R.id.btPanioSeven;
button = new Button[7];
havePlayed = new boolean[7];
// 獲取按鈕對象
for (int i = 0; i < button.length; i++) {
button[i] = (Button) findViewById(buttonId[i]);
button[i].setClickable(false);
havePlayed[i] = false;
}
pressedkey = new int[5];
for (int j = 0; j < pressedkey.length; j++) {
pressedkey[j] = -1;
}
}
/**
* 判斷某個點是否在某個按鈕的范圍內(nèi)
*
* @param x 橫坐標(biāo)
* @param y 縱坐標(biāo)
* @param button 按鈕對象
* @return 在:true;不在:false
*/
private boolean isInScale(float x, float y, Button button) {
// keys.getTop()是獲取按鈕所在父視圖相對其父視圖的右上角縱坐標(biāo)
if (x > button.getLeft() && x < button.getRight()
&& y > button.getTop() + keys.getTop()
&& y < button.getBottom() + keys.getTop()) {
return true;
} else {
return false;
}
}
/**
* 判斷某個點是否在一個按鈕集合中的某個按鈕內(nèi)
*
* @param x 橫坐標(biāo)
* @param y 縱坐標(biāo)
* @param button 按鈕數(shù)組
* @return
*/
private int isInAnyScale(float x, float y, Button[] button) {
// keys.getTop()是獲取按鈕所在父視圖相對其父視圖的右上角縱坐標(biāo)
for (int i = 0; i < button.length; i++) {
if (x > button[i].getLeft() && x < button[i].getRight()
&& y > button[i].getTop() + keys.getTop()
&& y < button[i].getBottom() + keys.getTop()) {
return i;
}
}
return -1;
}
}
7.AndroidManifest.xml頁面對某個Activity頁面進(jìn)行設(shè)置橫屏
android:screenOrientation="landscape"
8.另外,每個按鍵的音效需要提前導(dǎo)入res下raw文件夾中。
源碼:點擊打開鏈接
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中創(chuàng)建快捷方式及刪除快捷方式實現(xiàn)方法
這篇文章主要介紹了Android中創(chuàng)建快捷方式及刪除快捷方式實現(xiàn)方法,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-06-06
Android Jetpack 狠活Lifecycles與LiveData使用詳解
這篇文章主要為大家介紹了Android Jetpack 狠活Lifecycles與LiveData使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
這篇文章主要介紹了Android編程自定義搜索框?qū)崿F(xiàn)方法,涉及Android界面布局、數(shù)據(jù)加載、事件響應(yīng)等相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
Android開發(fā)之HttpClient異步請求數(shù)據(jù)的方法詳解【附demo源碼下載】
這篇文章主要介紹了Android開發(fā)之HttpClient異步請求數(shù)據(jù)的方法,結(jié)合實例形式較為詳細(xì)的分析了Android HttpClient異步請求數(shù)據(jù)的相關(guān)操作技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2017-11-11
Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程
這篇文章主要介紹了Android 實現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式實例分享
這篇文章主要介紹了android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式,有需要的朋友可以參考一下2013-12-12
如果你在Android Studio碰到gradle的各種問題就來看這篇文章吧(強(qiáng)烈建議收藏)
這篇文章主要介紹了你可能會在Android Studio碰到gradle的各種問題,完美解決關(guān)于gradle的全部問題,切記收藏以防需要的時候找不到了哦2021-08-08

