Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓最佳钢琴风琴布局_Android_Layout_Touch_Piano - Fatal编程技术网

Android 安卓最佳钢琴风琴布局

Android 安卓最佳钢琴风琴布局,android,layout,touch,piano,Android,Layout,Touch,Piano,我的应用程序有一个音乐风琴的背景图像,我使用了带有按钮的LinearLayouts,背景设置为@null作为键 我已经用视图实现了我的布局。OnTouchListener和OnTouch在MotionEvent.ACTION\u DOWN上播放相关声音,并在MotionEvent.ACTION\u UP上停止 这种方式很好,除了当我的手指滑过按钮(风琴键)时,因为它们是按钮(我想),除非我抬起并修饰按钮,否则不会触发新的ontouch事件 有谁能建议一个更好的布局,使用一些其他控件,也许可以让我

我的应用程序有一个音乐风琴的背景图像,我使用了带有
按钮的
LinearLayout
s,背景设置为
@null
作为键

我已经用
视图实现了我的布局。OnTouchListener
OnTouch
MotionEvent.ACTION\u DOWN
上播放相关声音,并在
MotionEvent.ACTION\u UP
上停止

这种方式很好,除了当我的手指滑过按钮(风琴键)时,因为它们是按钮(我想),除非我抬起并修饰按钮,否则不会触发新的ontouch事件

有谁能建议一个更好的布局,使用一些其他控件,也许可以让我检测到它们之间的滑动,以实现键盘上下滑动的效果

我真的不想从头开始画他们的关键点,因为我想利用背景图像中的图形(用于风琴键盘),这比画矩形更令人愉快。 谢谢



我假设您正在做一些数学运算,以知道按钮应放在何处,以便它们与背景图像相对应

如果您将
ontouch Listener
设置为整个
LinearLayout
,并将其实现为在
ACTION\u DOWN
ACTION\u UP
上执行操作,您可以使用相同的数学计算出用户触摸了
LinearLayout
的位置,以了解他或她实际要按下的键。所以你根本不需要按钮

然后,您将实现侦听器,使其也在
ACTION\u MOVE
上执行某些操作,并且借助您已经在做的数学运算,您可以计算出用户何时用手指留下一个键并移动到另一个键。因此,您基本上会将手指的当前坐标与之前调用
onTouch()
方法时的坐标进行比较,这样您就可以知道移动到哪个键,这样您就可以开始播放它的声音,也可以从哪个键开始播放,这样您就可以停止播放前一个键的声音。如果移动在同一关键点上开始和结束,则不执行任何操作

大约一年前,我在实现一个日历时做了类似的事情,用户可以通过从一个日历单元格滑动到另一个日历单元格来选择时间间隔。而且日历必须随着用户的滑动而更新,因此我还必须知道他或她何时离开一个单元格并将手指移动到另一个单元格。因此,我确信,如果你正确地实施它,它将起作用

我假设,因为它应该是一个模拟钢琴的应用程序,所以你需要允许用户不仅用一个手指滑动键盘,而且用两个(或更多)手指滑动键盘,对吗?我认为这也应该很容易实现,因为
MotionEvent
包含了关于做运动的指针(手指、鼠标指针等)的信息。虽然我没有这方面的经验,但您可以在
MotionEvent
文档中了解更多信息:

希望能有帮助。如果您还有其他问题,我很乐意回答(更具体地说,我还有日历代码。)

祝你好运

编辑以回答评论:

我不是说用
查看
s而不是
按钮
s。我的意思是,您的布局仅由设置了背景图像的
线性布局组成。就这样。然后在代码中,将一个
OnTouchListener
附加到它,并在
onTouch()
方法中计算位置。在我脑海中,我想你可以这样做:

我假设应用程序应该在横向模式下使用,并且钢琴的图像覆盖整个屏幕。假设你有8个白色的钢琴键。通过将屏幕宽度除以8,可以知道设备上一个钢琴键的宽度。您还知道一个关键点的起点和终点。假设一个键的宽度为50像素。然后第一个从第1个像素开始,到第50个像素结束。第二个从51号开始,到100号结束。等等

因此,如果触摸事件发生,您可以通过调用
MotionEvent.getX
MotionEvent.getY
来访问其坐标。现在我不知道你是需要
getX
还是
getY
,你必须弄清楚。假设这个方法返回一个像94这样的数字。当您将其除以之前计算的钢琴键宽度,并将结果四舍五入到最接近的整数时,您将知道用户按下了哪个键。因此,如果宽度为50,则执行94/50=1.88,将其四舍五入为2,然后使用第二个键

您应该将
getX
/
getY
变量存储在活动中,这样当您检测到下一个事件时,就可以知道发生了什么。因此,如果你在第二个键上按下一个
动作
,然后你得到一个
动作
,并且你计算它实际上已经在第三个键上,你就知道用户按下了第二个键,然后滑到了第三个键。所以你停止播放第二个键的声音,开始播放第三个键的声音


我希望这是有意义的,而且这不是一种太原始和愚蠢的方法

好主意,在线性布局上而不是在按钮上点击侦听器。。。现在要更改代码。。谢谢你提供的信息丰富的回复。如果这对你有帮助,我也很高兴你能接受这个答案。谢谢。目前使用线性布局中按钮的位置来确定ontouch中按下的布局部分。尝试使用整个布局而不是按钮,但无法找到一种方法来计算相对于包含器官键的背景图像按下的位置。还尝试使用视图而不是按钮,但当视图已打开时,MotionEvent.ACTION\u MOVE仍未触发
    <LinearLayout android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/vibrato_off" android:orientation="vertical" android:weightSum="95" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".main">
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="70"></LinearLayout>
<LinearLayout android:id="@+id/llblack" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="11">
    <View android:layout_width="1dp" android:layout_height="0dip" android:layout_weight="1.5"/>
    <android.view.View android:id="@+id/vib_off" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <View android:layout_width="1dp" android:layout_height="0dip" android:layout_weight="0.4"/>
    <android.view.View android:id="@+id/a2u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <android.view.View android:id="@+id/a2sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/b2u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <android.view.View android:id="@+id/c3u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <android.view.View android:id="@+id/c3sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/d3sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/e3u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <android.view.View android:id="@+id/f3u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <android.view.View android:id="@+id/f3sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/g3sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/a3sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/b3u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <android.view.View android:id="@+id/c4u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <android.view.View android:id="@+id/c4sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/d4sharp" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/e4u" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="0.5" android:background="@null"/>
    <View android:layout_width="1dp" android:layout_height="0dip" android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout android:id="@+id/llwhite" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9">
    <View android:layout_width="1dp" android:layout_height="0dip" android:layout_weight="1.5"/>
    <android.view.View android:id="@+id/vib_on" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <View android:layout_width="1dp" android:layout_height="0dip" android:layout_weight="0.4"/>
    <android.view.View android:id="@+id/a2" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/b2" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/c3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/d3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/e3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/f3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/g3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/a3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/b3" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/c4" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/d4" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <android.view.View android:id="@+id/e4" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null"/>
    <View android:layout_width="1dp" android:layout_height="0dip" android:layout_weight="0.4"/>
</LinearLayout>