How to get the dimensions of my Button in advance?
Hello guys I am basically doing a piano app. After much thought I think
I've figured how most piano apps are done and I'm stuck here and this
seems to be very crucial to get all the other functionality such as the
slide,multitouch,adding keys,etc.
Is it possible to know the dimensions of my drawable button before Hand?
Say I have basically two drawable key button, piano keys C and D:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="bottom" >
<Button
android:id="@+id/ckey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/keybutton" />
<Button
android:id="@+id/dkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/keybutton" />
For a piano app(white keys), they both use the same selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/key"
android:state_pressed="true"/>
<item
android:drawable="@drawable/key_pressed"/>
</selector>
But I would like to know the dimensions OR the location BEFOREHAND of the
Button created so that I can draw a region on Top of that button and use
that region for onTouchListener. I need that region so that I can use
onTouch.
@Override
public boolean onTouch(View v, MotionEvent event) {
int numberOfKeys = 2;
Region[] keyBoard = new Region[2]; //for 2 White Piano Keys
Integer pointerIndex = event.getActionIndex();
Float x = event.getX(pointerIndex);
Float y = event.getY(pointerIndex);
for(int j=0;j<1;j++){
if(this.keyBoard[j].contains(x.intValue(),y.intValue())){
//play corresponding sound
}
}
So knowing the dimensions of my images: key.png and key_pressed.png and
the screen width and height and maybe other parameters I don't know. is It
possible to know beforehand the dimensions or COORDINATES or Location of
my buttons before the app is launched?
Otherwise, how can I get the coordinates? it seems getTop() and getLeft()
are not good options because they return 0 since the images take time to
load therefore the code cannot retrieve it.
Thanks guys. I'm super noob by the way. I apologize if I missed something.
No comments:
Post a Comment