Split an image and use it as button

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Split an image and use it as button

Postby suresh » Sun May 03, 2009 9:31 am

Hello everybody

I need to split an image into 4 parts. And use each part as a button/link. I need to split image from java(similar to an ImageMap). Please suggest me the way to do it.

Regards
Suresh
Learning android
suresh
Freshman
Freshman
 
Posts: 3
Joined: Fri Apr 24, 2009 7:35 am
Location: Nepal

Top

Postby andreleitao » Tue May 12, 2009 9:57 pm

Hi, Suresh!

I'll tell a bit of my experience with your problem, ok?


PROBLEM 1 - CLIPPING AN IMAGE

To do it I use Bitmap class because it can be drawn by Canvas in parts using this method from Canvas class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. drawBitmap  (Bitmap bitmap, Rect src, Rect dst, Paint paint)
Parsed in 0.030 seconds, using GeSHi 1.0.8.4



The first argument is clear, you will put your bitmap object.
The second one (src) you will put a Rectangle to tell the clip area on your bitmap.
The third one (dst) you will put a Rectangle to tell WHERE your bitmap location on the screen AND your bitmap width and height.
The last one you can put null, if a Paint object is not needed for you.


PROBLEM 2 - 'BUTTON' CLICK

You will simulate a button using an image, right? I prefer use a Rectangle to know whether user has been touched the button.

I most common cases the Rectangle will have the same width, height and location of your image. You don't need to paint it, it will be there :). You are using
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onTouchEvent(MotionEvent event)
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
method, right? When a touch is done, you verify if the user has touched your button this way:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(buttonRect.contains((int)event.getX(), (int)event.getY()){
  2.    //do the action
  3. }
  4.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I hope I was usefull.

Regards,
André Leitão
andreleitao
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Mar 10, 2009 9:56 pm
Location: Recife - Brazil

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests