简体   繁体   中英

How to create layout for piano

I am working on piano app and I am facing some problems with the layout creation. I want to create a layout like below:

在此输入图像描述

But I am able to create only this

在此输入图像描述

Now I want to add all the black buttons but problem is that I am not able to add view above those buttons. How can I do this? My current layout code is below, please suggest me how can I achieve this. Now I am able to click all the buttons, once I add all the buttons then each button should be clickable.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:weightSum="1.5" >

        <ImageView
            android:id="@+id/bw1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw3"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw4"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw5"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw6"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw7"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw8"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw9"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw10"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw11"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw12"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw13"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw14"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw15"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />
    </LinearLayout>

</RelativeLayout> 

I would suggest drawing these white and black keys on your own. Use the View class, override draw() method and go for it. Of course you can try to use RelativeLayout, but later you'll have more problems with that (like multiple touches and slides detection).

class Piano extends View {
    public Piano(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    Bitmap whiteKey, blackKey;
    Paint paint = new Paint();

    public void draw(Canvas canvas) {
        if (whiteKey == null) {
            whiteKey = BitmapFactory.decodeResource(getResources(), R.drawable.whiteKey);
        }
        if (blackKey == null) {
            blackKey = BitmapFactory.decodeResource(getResources(), R.drawable.blackKey);
        }

        int keys = 10;

        // draw white keys
        for (int i = 0; i < keys; i++) {
            canvas.drawBitmap(whiteKey, i * whiteKey.getWidth(), 0, paint);
        }
        // draw black keys
        for (int i = 0; i < keys; i++) {
            if (i != 3 && i != 7) {
                canvas.drawBitmap(blackKey, i * blackKey.getWidth()+blackKey.getWidth()*0.5f, 0, paint);
            }
        }
    }
};

你可以通过使用布局容器得到你想要的东西,这样可以叠加按钮和标签

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM