【移動應用開發技術】Android如何實現簡易版彈鋼琴效果_第1頁
【移動應用開發技術】Android如何實現簡易版彈鋼琴效果_第2頁
【移動應用開發技術】Android如何實現簡易版彈鋼琴效果_第3頁
【移動應用開發技術】Android如何實現簡易版彈鋼琴效果_第4頁
【移動應用開發技術】Android如何實現簡易版彈鋼琴效果_第5頁
已閱讀5頁,還剩10頁未讀, 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發技術】Android如何實現簡易版彈鋼琴效果

這篇文章給大家分享的是有關Android如何實現簡易版彈鋼琴效果的內容。在下覺得挺實用的,因此分享給大家做個參考,一起跟隨在下過來看看吧。具體內容如下目標效果:1.drawable下新建button_selector.xml頁面:<?xml

version="1.0"

encoding="utf-8"?>

<selector

xmlns:android="/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="/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="/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="/apk/res/android"

xmlns:tools="/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[];//

按鈕數組

private

PanioMusic

utils;//

工具類

private

View

parent;//

父視圖

private

int

buttonId[];//

按鈕id

private

boolean

havePlayed[];//

是否已經播放了聲音,當手指在同一個按鈕內滑動,且已經發聲,就為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;//

標記是否是在按鍵上移動

temp

=

isInAnyScale(event.getX(count),

event.getY(count),

button);

if

(temp

!=

-1)

{//

事件對應的是當前點

switch

(event.getActionMasked())

{

case

MotionEvent.ACTION_DOWN:

//

//

單獨一根手指或最先按下的那個

//

pressedkey

=

temp;

case

MotionEvent.ACTION_POINTER_DOWN:

Log.i("--",

"count"

+

count);

pressedkey[count]

=

temp;

if

(!havePlayed[temp])

{//

在某個按鍵范圍內

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--)

{

//

當在兩端的按鈕時,會有一邊越界

if

(i

<

0

||

i

>=

button.length)

{

continue;

}

if

(isInScale(event.getX(count),

event.getY(count),

button[i]))

{//

在某個按鍵內

moveflag

=

true;

if

(i

!=

temp)

{//

在相鄰按鍵內

boolean

laststill

=

false;

boolean

nextstill

=

false;

//

假設手指已經從上一個位置抬起,但是沒有真的抬起,所以不移位

pressedkey[count]

=

-1;

for

(int

j

=

0;

j

<

pointercount;

j++)

{

if

(pressedkey[j]

==

temp)

{

laststill

=

true;

}

if

(pressedkey[j]

==

i)

{

nextstill

=

true;

}

}

if

(!nextstill)

{//

移入的按鍵沒有按下

//

設置當前按鍵

button[i]

.setBackgroundResource(R.drawable.button_pressed);

//

發音

utils.soundPlay(i);

havePlayed[i]

=

true;

}

pressedkey[count]

=

i;

if

(!laststill)

{//

沒有手指按在上面

//

設置上一個按鍵

button[temp]

.setBackgroundResource(R.drawable.button);

havePlayed[temp]

=

false;

}

break;

}

}

}

break;

case

MotionEvent.ACTION_UP:

case

MotionEvent.ACTION_POINTER_UP:

//

事件與點對應

tempIndex

=

event.getActionIndex();

if

(tempIndex

==

count)

{

Log.i("--",

"index"

+

tempIndex);

boolean

still

=

false;

//

當前點已抬起

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)

{//

已經沒有手指按在該鍵上

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;

}

}

/**

*

判斷某個點是否在某個按鈕的范圍內

*

*

@param

x

橫坐標

*

@param

y

縱坐標

*

@param

button

按鈕對象

*

@return

在:true;不在:false

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論