Making multiple ImageButtons from one image

Problems with Canvas, OpenGL, etc...

Making multiple ImageButtons from one image

Postby c0dege3k » Wed Mar 23, 2011 2:41 pm

So I have an image like this: http://dl.dropbox.com/u/8051037/disk_ab1.png

I need to keep it all as one image, but I need to divide it into eight sections using code, and make each section an ImageButton. I have NO idea how to this, so any help would be appreciated.
c0dege3k
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 23, 2011 2:36 pm

Top

Re: Making multiple ImageButtons from one image

Postby rahul9586 » Wed Apr 13, 2011 12:52 pm

I would suggest to make a custom view and draw the image in the onDraw method. Now add a onTouchEvent() function to the view... create listeners for each part of the image touched and return the appropriate listener for the area touched.

your View class would be something like this:

Code: Select all
class MyVeiw extends Veiw{
TopOnClickListener topListener;

onDraw(Canvas canvas){
canvas.drawBitmap(//your image);


onTouchEvent(MotionEvent event){
flaot x=event.getX();
flaot y=event.getY();

if(//x and y are between specific values that define your region[/color]){
//carry out some event
//or still better: return a listener
// eg: topListener.onClick(this);
// if you want to emulate click and not touch, check if MotionEvent.Action.equals(MotionEvent.ACtionDOWN) and UP lie within the same area and then return the listener event.
}

private interface TopOnClickListener(){
void onClick(View v)
}

public TopOnClickListener setTopOnClickListener(OnTopClickListener l){
this.topListener=l;
}

}


and in your activity:

Code: Select all
MyView mView=new MyView(this);
mView.setOnTopClickListener(new OnTopClickListener(){
onClick(View V){
// carry out whatever you want to do
});




The tricky bit would be to map all the areas that you want to be clickable.

Do tell me if this method works for you. all the best!
rahul9586
Freshman
Freshman
 
Posts: 5
Joined: Fri Jan 21, 2011 5:15 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests