Android 在布局中使用每个子项
我在一篇文章中发现了这段代码,该文章在touch listener上用于线性布局。 在线性布局中,有一些子布局,每个子布局播放相同的声音,但具有音调。现在我如何使用每个孩子来播放不同的声音。 换句话说,我怎样才能接触到每个孩子Android 在布局中使用每个子项,android,eclipse,Android,Eclipse,我在一篇文章中发现了这段代码,该文章在touch listener上用于线性布局。 在线性布局中,有一些子布局,每个子布局播放相同的声音,但具有音调。现在我如何使用每个孩子来播放不同的声音。 换句话说,我怎样才能接触到每个孩子 public class MainActivity extends Activity { LinearLayout pianoKeysContainer; @Override protected void onCreate(Bundle savedInstanceStat
public class MainActivity extends Activity {
LinearLayout pianoKeysContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
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.dinger14);
pressedKeys.add(pianoKey);
}
if (lastPlayedKey != pianoKey)
playKey(pitch);
lastPlayedKey = pianoKey;
break;
}
if (lastPlayedKey != null)
{
pianoKey.setBackgroundResource(R.drawable.dinger14);
lastPlayedKey.setBackgroundResource(R.drawable.dinger14);
}
}
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP)
{
lastPlayedKey = null;
for (FrameLayout pressedKey : pressedKeys)
{
pressedKey.setBackgroundResource(R.drawable.dinger14);
}
}
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.chitare3, 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);
}
});
}
公共类MainActivity扩展活动{
线性布局平面集装箱;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u横向);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
pianoKeysContainer=(LinearLayout)findViewById(R.id.key_容器);
pianoKeysContainer.setOnTouchListener(onYourViewTouchListener);
}
//在这里,我们在渲染后加载视图位置,并用这些位置填充数组
private List positionsleftu whiteKeys=new ArrayList();
private List positionsrright_whiteKeys=new ArrayList();
WindowFocusChanged上的公共无效(布尔hasFocus)
{
super.onWindowFocusChanged(hasFocus);
对于(int i=0;i=0&&positionsRight\u whiteKeys.size()>=0)
{
如果(位置x>positionsLeft_白键.get(x)和位置x
}如果您不需要家长触摸事件,则直接在每个孩子身上应用相同的事件
pianoKeysContainer1 = (LinearLayout) findViewById(R.id.key_child1);
pianoKeysContainer2 = (LinearLayout) findViewById(R.id.key_child2);
pianoKeysContainer1.setOnTouchListener(onYourViewTouchListener);
pianoKeysContainer2.setOnTouchListener(onYourViewTouchListener);
您可以对每个子级应用onYourViewTouchListener onTouchListener,如上所示。
然后在视图中,通过检查视图id检查调用了哪个子侦听器
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)
{
// Check **view id** here and apply required action based on this view id.
.............
.............
public View.OnTouchListener onYourViewTouchListener=new View.OnTouchListener()
{
浮动位置X;
框架布局pianoKey;
FrameLayout lastPlayedKey;
ArrayList pressedKeys=新建ArrayList();
@凌驾
公共布尔onTouch(视图、运动事件、运动事件)
{
//在此处选中**视图id**并基于此视图id应用所需操作。
.............
.............