How to create layout for piano

前端 未结 2 388
梦谈多话
梦谈多话 2020-12-17 05:41

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

2条回答
  •  囚心锁ツ
    2020-12-17 06:25

    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);
                }
            }
        }
    };
    

提交回复
热议问题