簡體   English   中英

Android最佳鋼琴風琴布局

[英]Android best piano organ layout

我的應用程序有一個音樂風琴的背景圖像,我使用了帶有ButtonLinearLayout ,其背景設置為@null作為鍵。

我實現了我的布局與View.OnTouchListenerOnTouch我玩上相關的音響MotionEvent.ACTION_DOWN並停止它MotionEvent.ACTION_UP

這種工作正常,但是當我將手指滑過按鈕(風琴鍵)時,由於它們是按鈕(我認為),除非我抬起並修飾按鈕,否則不會觸發新的ontouch事件。

任何人都可以建議一個更好的布局,使用一些其他控件,也許這將使我能夠檢測到它們之間的幻燈片,以實現向上或向下滑動鍵盤效果?

我真的不想從頭開始繪制它們,因為我想利用背景圖像中的圖形(對於風琴鍵盤)比繪制的矩形更令人愉悅。 謝謝

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

我假設你正在做一些數學知道在哪里放置按鈕,以便它們與背景圖像對應。

如果將OnTouchListener設置為整個LinearLayout並將其實現為在ACTION_DOWNACTION_UP上執行操作,則可以使用相同的數學運算來確定用戶觸摸LinearLayout以了解他或她實際要按哪個鍵。 所以你根本不需要按鈕。

然后,您將實現偵聽器以在ACTION_MOVE上執行某些ACTION_MOVE ,並且在您已經執行的數學運算的幫助下,您可以找出用戶何時用他或她的手指離開一個鍵並移動到另一個鍵。 因此,當你先前調用onTouch()方法時,你基本上將手指的當前坐標與其坐標進行比較,這樣你就可以知道移動的哪個鍵,以便你可以開始播放它的聲音,以及從哪個鍵開始這樣你就可以停止播放前一個的聲音了。 如果運動在相同的鍵上開始和結束,則不執行任何操作。

大約一年前,當我實現一個日歷時,我做了類似的事情,用戶可以通過從一個日歷單元格滑動到另一個日歷單元格來選擇間隔。 並且日歷必須在用戶滑動時更新,因此我還必須知道他或她何時離開一個單元並將手指移動到另一個單元。 所以我相信,如果你正確實現它,它將起作用。

我假設,因為它應該是一個模擬鋼琴的應用程序,你需要允許用戶不僅用一個,而且用兩個(或更多)手指滑動鍵盤,對吧? 我認為這也很容易實現,因為MotionEvent攜帶有關做MotionEvent的指針(手指,鼠標指針......)的信息。 我對此沒有任何經驗,但您可以在MotionEvent文檔中閱讀更多相關信息: http//developer.android.com/reference/android/view/MotionEvent.html

希望能幫助到你。 如果您還有其他問題,我很樂意回答(更具體地說 - 我還有日歷的代碼。)

祝好運。

編輯回答評論:

我並不是說使用View而不是Button 我的意思是你的布局只包含一個設置了背景圖像的LinearLayout 就是這樣。 然后在代碼中,將OnTouchListener附加到它,並在onTouch()方法中計算位置。 在我的頭頂,我認為你可以這樣做:

我假設應用程序應該以橫向模式使用,並且鋼琴的圖像覆蓋整個屏幕。 假設你有8個白色鋼琴鍵。 通過將屏幕寬度除以8,您可以知道設備上的鋼琴鍵有多寬。 您還知道一個鍵的開始位置和結束位置。 假設一個密鑰寬50 px。 然后第一個從第一個px開始到第50個結束。 第二個從第51個開始到第100個結束。 等等。

那么,如果觸摸事件發生,您可以通過調用MotionEvent.getXMotionEvent.getY來訪問其坐標。 現在我不知道你是否需要getXgetY ,你必須弄明白。 因此,假設該方法返回一個類似於94的數字。當您將其除以先前計算的鋼琴鍵寬度,並將結果四舍五入到最接近的整數時,您將知道用戶按下了哪個鍵。 因此,如果寬度為50,則執行94/50 = 1.88,將其四舍五入為2,然后使用第二個鍵。

並且您應該在活動中存儲getX / getY變量,這樣當您檢測到下一個事件時,您可以告訴發生了什么。 因此,如果您在第二個鍵上獲得了ACTION_DOWN ,然后您獲得了一個ACTION_MOVE並且您計算它實際上已經在第三個鍵上,您知道用戶已按下第二個鍵然后滑到第三個鍵。 所以你停止播放第二個鍵的聲音並開始播放第三個鍵的聲音。

我希望它是有道理的,它不是一個太原始和愚蠢的方法。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM