整个按键OntouchListener阻力 [英] OntouchListener drag across buttons
问题描述
我为Android开发一个钢琴应用程序。我试图实施 OnTouchListener
所有的8个按钮,我在我的活动。因此,当用户拖动或猛击他的手指我希望所有的按钮来播放声音。我想下面的图片说明了它更好的。
I am developing a piano app for android. I am trying to implement OnTouchListener
for all the 8 buttons that I have in my activity. So, when the user drags or swipes his finger I want all the buttons to play the sound. I think the picture below explains it better.
请参阅?当用户把他的手放在第一个按钮,然后拖动,直到最后一个按钮,我希望所有的8个按钮播放的歌曲。但我不能够去实现它。下面的Java code仅适用于第一个按钮,但7个按钮的其余部分没有得到pressed。
See? When the user will place his hand on the first button and then drag till the last button, I want all the 8 buttons to play the song. But I am not able to achieve it. The below java code works only for the first button but the rest of the 7 buttons don't get pressed.
这是:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getID())
{
case(R.id.btn1):
//play button 1 sound
break;
case(R.id.btn2):
//play button 2 sound
break;
case(R.id.btn3):
//play button 3 sound
break;
case(R.id.btn4):
//play button 4 sound
break;
case(R.id.btn5):
//play button 1 sound
break;
case(R.id.btn6):
//play button 6 sound
break;
case(R.id.btn7):
//play button 7 sound
break;
case(R.id.btn8):
//play button 8 sound
break;
}
return true;
}
我已经看到<一href="http://stackoverflow.com/questions/21813946/change-textview-while-moving-finger-across-buttons">this问题和这与也没有找到一个答案在那里。
I have already seen this question and this as well but couldn't find an answer there.
请帮助我。感谢您对您的时间!
Please help me out. Thank you for you time!
推荐答案
您应该实现onTouchListner上包含钢琴按键的布局
You should implement onTouchListner on the layout which contains the piano buttons
我们的想法是让屏幕上的所有钢琴按键的位置(X,Y),使其后。
The idea is to get all piano buttons positions on the screen (x , y) after render them.
然后得到触摸位置使用的getX()和getY()以方法。
then get touch position by using getX() and getY() methods.
在这一点上,你可以通过检查处理,如果触摸x和y的看法开始和之间
At this point you can handle it by checking if the touch x and y is between the view start and
结束x和y,然后播放视图的声音。
end x and y, then play the sound of the view.
在这个例子中我'使用的Soundpool和俯仰播放不同的声音(我知道,这不是完美的方式),我也正致力于X因素仅仅是因为我使用的唯一的钢琴白键。
At this example i'am using soundpool and pitch to play different sounds (i know that it's not the perfect way), and i'm also working on x factor only because i'm using piano white keys only.
import android.app.Activity;
import android.media.*;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity
{
//The layout that holds the piano keys.
LinearLayout pianoKeysContainer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pianoKeysContainer = (LinearLayout) findViewById(R.id.key_container);
pianoKeysContainer.setOnTouchListener(onYourViewTouchListener);
}
//Here we load the view positions after render it and fill the array with the positions
private List<Integer> positionsLeft_whiteKeys = new ArrayList<Integer>();
private List<Integer> positionsRight_whiteKeys = new ArrayList<Integer>();
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
for (int i = 0; i < pianoKeysContainer.getChildCount(); i++)
{
//positionsLeft_whiteKeys holds the start x of each view.
positionsLeft_whiteKeys.add(pianoKeysContainer.getChildAt(i).getLeft());
//positionsRight_whiteKeys holds the end x of each view.
positionsRight_whiteKeys.add(pianoKeysContainer.getChildAt(i).getRight());
}
}
public View.OnTouchListener onYourViewTouchListener = new View.OnTouchListener()
{
float positionX;
FrameLayout pianoKey;
FrameLayout lastPlayedKey;
ArrayList<FrameLayout> pressedKeys = new ArrayList<FrameLayout>();
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
positionX = motionEvent.getX();
float pitch;
//Looping on the child of the layout which contains the piano keys
for (int x = 0; x < ((LinearLayout) view).getChildCount(); x++)
{
// Calculating the pitch to get good chords
pitch = (float) Math.pow(Math.pow(2.0, 1 / 12.0), (float) x);
pianoKey = (FrameLayout) ((LinearLayout) view).getChildAt(x);
if (positionsLeft_whiteKeys.size() >= 0 && positionsRight_whiteKeys.size() >= 0)
{
if (positionX > positionsLeft_whiteKeys.get(x) && positionX < positionsRight_whiteKeys.get(x))
{
pianoKey = (FrameLayout) ((LinearLayout) view).getChildAt(x);
if (pianoKey != null)
{
pianoKey.setBackgroundResource(R.drawable.piano_key_pressed);
pressedKeys.add(pianoKey);
}
if (lastPlayedKey != pianoKey)
playKey(pitch);
lastPlayedKey = pianoKey;
break;
}
if (lastPlayedKey != null)
{
pianoKey.setBackgroundResource(R.drawable.piano_key);
lastPlayedKey.setBackgroundResource(R.drawable.piano_key);
}
}
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP)
{
lastPlayedKey = null;
for (FrameLayout pressedKey : pressedKeys)
{
pressedKey.setBackgroundResource(R.drawable.piano_key);
}
}
return false;
}
};
//This is sound play method
SoundPool sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 1);
public void playKey(final float pitch)
{
//here you should store your piano sound at res/raw then load it
sp.load(this, R.raw.piano, 1);
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener()
{
@Override
public void onLoadComplete(SoundPool soundPool, int i, int i2)
{
soundPool.play(i, 0.99f, 0.99f, 1, 0, pitch);
}
});
}
}
这是XML文件(main.xml中)
And this is the xml file (main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:id="@+id/key_container">
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"
/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
<FrameLayout
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_width="48dp"
android:background="@drawable/piano_key"/>
</LinearLayout>
</LinearLayout>
注意:你应该做的,完成这个例子的唯一的事情是把 你的钢琴键图像(pressed而不是pressed),并把钢琴的声音 在RES /生的
Note: the only thing you should do to complete the example is to put you piano key image (pressed and not pressed) and put the piano sound at res/raw
这篇关于整个按键OntouchListener阻力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!