0

I am developing a musical instrument which is round with notes on the inside. When a note is clicked a sound is heard. However i am having a problem scaling this for different sized android devices.

The code that i am using is not really scalable... how can i change this to scale the code with the coordinates referencing the polygon shapes to different sized devices.....

I have nine different instruments, each with many notes so mapping each note to a different screen resolution is a nightmare...

//test if point clicked is within defined chord

 `private boolean testIfFSharp6(int x, int y){
    boolean _retVal = false;
    if(resolutionType == "480X800"){
        int[] xPts = new int[] { 182,223,221,180,182};
        int[] yPts = new int[] { 172,172,215,214,172};
        _retVal = in_or_out_of_polygon(xPts, yPts, x, y); 

    }else if(resolutionType == "320X480"){
        int[] xPts = new int[] { 120,146,145,120,120};
        int[] yPts = new int[] { 116,116,146,142,116};
        _retVal = in_or_out_of_polygon(xPts, yPts, x, y); 
    }else if(resolutionType == "240X400"){
        int[] xPts = new int[] { 89,110,109,90,89 };
        int[] yPts = new int[] { 87,86,108,111,87};
        _retVal = in_or_out_of_polygon(xPts, yPts, x, y); 

    }else{
        int[] xPts = new int[] { 231,281,277,231,231 };
        int[] yPts = new int[] { 222,223,277,272,222};
        _retVal = in_or_out_of_polygon(xPts, yPts, x, y); 

    }
    return _retVal;
}`

Screenshot of instrument

Second screenshot of instrument

1 Answers1

0

This looks like pure math when you want to scale this for all types of screens. I think instead of hardcoding things you should work with the total width and height available on the device. Since your instrument is circular you can figure out the radius/Diameter you have available for the instruments.

Now since u have three layers(three concentric circles) of button in the instrument you need to decided how much percentage of diameter you want to allocate to the different layers. That you can hardcode in your application. Then use those percentages to do all the other calculations like for every click you will first need to figure out in which concentric circle the touch point lies and then within that you will need to figure out which note was clicked.

Also when you are creating the notes you can save the center and radius of the notes so that once the user taps anywhere you can figure out in which circle the touch lies so that you can play the appropriate note. hope you get the idea.

Getting the size of the screen:Get screen dimensions in pixels

For Every instrument you will have to first figure out the geometry and decide on parameters(like the percentage for the above example) which you can use to actually construct those instruments.

What you can do is You can have a base abstract class Instrument and then for every instrument you can extend the base class and those classes will be responsible for creating the geometry from the available screen size and notes with the parameters that you have decided for that instrument. That would make it easy for you to add a new instrument and also tweak anything you want for any instrument.

Every class can have a function for handling the click and can figure out the note which was clicked based on the geometry.

Some code:

BaseInstrument Class:

public abstract class BaseInstrument {
Point size;
int noNotes;
Context context;

public BaseInstrument(Context context, Point size, int notes) {
    this.context = context;
    this.size = size;
    this.noNotes = notes;
}

public abstract void playNote(int clickX, int clickY);

public int getNotesSize() {
    return noNotes;
}

// you can add more methods which are common for all instruments
}

Instrument1 Class:

public class Instrument1 extends BaseInstrument {

private static final int outerCirclePercentage = 40;
private static final int middleCirclePercentage = 30;
private static final int innerCirclePercentage = 30;
private static final int notes = 15; // some number

public Instrument1(Context context, Point p) {
    super(context, p, notes);

    // based on the geometry compute the positions and geometry of the notes for this intrument

}

@Override
public void playNote(int clickX, int clickY) {
    // do you calculation based on the geometry of the instrument
}

}
Community
  • 1
  • 1
pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • There are multiple instruments and they are not always in three rows like this... so i would want to map once and then have it scale upwards... Any thoughts? – Quinten Questel Oct 09 '15 at 23:46
  • Hi I added another screenshot of an instrument showing that 1 of the instrument has 6 separate instruments... If i use the following coordinates as a base for a note ... how do i implement as in your example? ` 'int[] xPts = new int[] { 231,281,277,231,231 }; int[] yPts = new int[] { 222,223,277,272,222}; ` – Quinten Questel Oct 10 '15 at 01:52