How do I draw a radial gradient button in BlackBerry? I found “Drawing Radial Gradients” on the BlackBerry support forums. All I am able to implement on my own is a linear gradient.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a little tricky. Drawing linear gradients on field backgrounds is easy. Drawing radial gradients on field backgrounds is harder. Doing it on a button is harder still.
First of all, the example you link to does indeed look really bad. The biggest problem with that code is that it uses
Graphics.drawArc()to construct the gradient out of concentric circles (lines). This is not at all smooth.The biggest improvement you need to make over that is to use
Graphics.fillArc()instead, which will look much smoother (although there may be a performance impact to this …).Your question didn’t say anything about how you wanted the button to look when focused, or whether the corners needed to be rounded. That’s where some of the difficulty comes in.
If you just extend the RIM
ButtonFieldclass, you’ll probably have trouble with the default drawing for focus, and edge effects. It’s probably necessary to directly extend the baseFieldclass in a new, written-from-scratch, button field. I wouldn’t necessarily recommend that you do all this yourself, since buttons require focus handling, click handling, etc. You should probably start with something like the BaseButtonField from the BlackBerry AdvancedUI open source library.I have prototyped this for you, using that class as a base. (so, you’ll need to download and include that source file in your project if you use this).
I created a GradientButtonField subclass:
To use this, the
Managerthat contains the button will need to constrain the button’s size in itssublayout()implementation. Or, you can edit myGradientButtonFieldclass to hardcode a certain size (viagetPreferredWidth(),layout(), etc.), or whatever you want.I did not round the corners, as that’s a bit of work. Depending on what kind of backgrounds you’re putting these buttons on, it might be easiest to create a PNG mask image (in your favorite drawing program), which is mostly transparent, and then just has filled corners that mask off the corners of the gradient below it. Then, use
Graphics.drawBitmap()in thepaint()method above, after you’ve drawn the radial gradient.For focus highlighting, I just put in some simple code to brighten the colors when the button is focused. Again, you didn’t say what you wanted for that, so I just did something simple.
Here’s the result of the code above. The bottom button is focused: