1

i follow this link to design my piano app. I am able to design nodes for piano. Now i am having problem to recognize which node user touches so that i can play particular node.

my code for custom piano key is:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

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.white_up);
        }
        if (blackKey == null) {
            blackKey = BitmapFactory.decodeResource(getResources(), R.drawable.black_up);
        }

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

and in Activity i am calling setContentView method like this.

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Piano piano = new Piano(this);
        setContentView(piano);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

How would i get id of bitmap so that i can play node.

Community
  • 1
  • 1
Lalit Chattar
  • 1,914
  • 8
  • 27
  • 49
  • Can you elaborate on your problem a little more? Post the code that's causing problems, also what have you tried so far? – Bobbake4 Jul 12 '13 at 16:01

2 Answers2

0

You have to get a position where user touched, check out this post and here is how to set onTouchListener. When you have the x,y position, you have to test which key was pressed.

Try to run a for loop first for all black keys, because they overlap white keys. If you didn't found any pressed black key, then iterate over all white keys. The loop for white keys can look something like this:

boolean keyfound=false;
int pressedkey=-1;
for (int i = 0; i < keys; i++) {
    if( (x>=(i*whiteKey.getWidth())) && (x<((i+1)*whiteKey.getWidth()))
        && (y>0) && (y<whiteKey.getHeight()) )
    {
         pressedkey=i;
         keyfound=true;
         break;
    }
}
Community
  • 1
  • 1
nio
  • 5,141
  • 2
  • 24
  • 35
0

Set a setOnTouchListener for the Canvas. The OnTouchListener implements a function that looks like this: onTouch(View v, MotionEvent event). The MotionEvent in it has info about where the touch actually happened in the form of getX() and getY(). Using the data find the button which was clicked. I think that should do the trick.

x=event.getX();
y=event.getY();
let whitewidth and blackwidth be the width of these keys and similarly whiteheight and    blackheight.
for (int i = 0; i < num_of_keys; i++) {
    if( (x>(i*whitewidth)) && (x<((i+1)*whitewidth))
        && (y>0) && (y<whiteheight) )
    {
         pressedkey=i;
    }
}

Using the id of key pressed you can play specific mp3 files.

Ankit Aggarwal
  • 2,367
  • 24
  • 30