如何在BlackBerry中實現圖像按鈕?BlackBerry中的圖像按鈕
8
A
回答
20
在這裏你走,完整代碼:
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.ButtonField;
/**
* Button field with a bitmap as its label.
*/
public class BitmapButtonField extends ButtonField {
private Bitmap bitmap;
private Bitmap bitmapHighlight;
private boolean highlighted = false;
/**
* Instantiates a new bitmap button field.
*
* @param bitmap the bitmap to use as a label
*/
public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight) {
this(bitmap, bitmapHighlight, ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER);
}
public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight, long style) {
super(style);
this.bitmap = bitmap;
this.bitmapHighlight = bitmapHighlight;
}
/* (non-Javadoc)
* @see net.rim.device.api.ui.component.ButtonField#layout(int, int)
*/
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
/* (non-Javadoc)
* @see net.rim.device.api.ui.component.ButtonField#getPreferredWidth()
*/
public int getPreferredWidth() {
return bitmap.getWidth();
}
/* (non-Javadoc)
* @see net.rim.device.api.ui.component.ButtonField#getPreferredHeight()
*/
public int getPreferredHeight() {
return bitmap.getHeight();
}
/* (non-Javadoc)
* @see net.rim.device.api.ui.component.ButtonField#paint(net.rim.device.api.ui.Graphics)
*/
protected void paint(Graphics graphics) {
super.paint(graphics);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap b = bitmap;
if (highlighted)
b = bitmapHighlight;
graphics.drawBitmap(0, 0, width, height, b, 0, 0);
}
public void setHighlight(boolean highlight)
{
this.highlighted = highlight;
}
}
+0
感謝您的快速和準確的答覆.... – 2010-05-26 13:43:15
+3
我知道這是回來一段時間,但我需要添加這些方法得到這個工作 - 保護void onFocus(int direction){ \t this.setHighlight(true); \t super.onFocus(direction); \t} \t保護無效onUnfocus(){ \t \t this.setHighlight(假); \t super.onUnfocus(); \t} – 2011-02-17 16:32:48
7
使用RIM的高級UI包。
這包含BitmapButton場和大量的有用的UI工具。
(毫無疑問Reflogs例子是好的,但我認爲,新的BB開發商登陸此頁面的高級UI包是更有益的)
4
perfect ImageButton for Blackberry , According to user point of view a Imagebutton should have four states
1. Normal
2. Focus
3. Selected Focus
4. Selected unfocus
the Following code maintain all four states (Field-Change-Listener and Navigation)
if you want to maintain all four states than use 1st Constructor, If you just want to handle Focus/Un-Focu state of the button than use 2nd one
########################################
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
public class ImageButton extends Field
{
Bitmap mNormalIcon;
Bitmap mFocusedIcon;
Bitmap mActiveNormalIcon;
Bitmap mActiveFocusedIcon;
Bitmap mActiveBitmap;
String mActiveText;
int mHeight;
int mWidth;
boolean isStateActive = false;
boolean isTextActive = false;
public boolean isStateActive()
{
return isStateActive;
}
public ImageButton(Bitmap normalIcon, Bitmap focusedIcon)
{
super(Field.FOCUSABLE | FIELD_VCENTER);
mNormalIcon = normalIcon;
mFocusedIcon = focusedIcon;
mActiveBitmap = normalIcon;
mActiveFocusedIcon = focusedIcon;
mActiveNormalIcon = normalIcon;
// isTextActive = false;
}
public ImageButton(Bitmap normalIcon, Bitmap focusedIcon, Bitmap activeNormalIcon, Bitmap activeFocusedIcon)
{
super(Field.FOCUSABLE | FIELD_VCENTER);
mNormalIcon = normalIcon;
mFocusedIcon = focusedIcon;
mActiveFocusedIcon = activeFocusedIcon;
mActiveNormalIcon = activeNormalIcon;
mActiveBitmap = normalIcon;
// isTextActive = true;
}
protected void onFocus(int direction)
{
if (!isStateActive)
{
mActiveBitmap = mFocusedIcon;
}
else
{
mActiveBitmap = mActiveFocusedIcon;
}
}
protected void onUnfocus()
{
super.onUnfocus();
if (!isStateActive)
{
mActiveBitmap = mNormalIcon;
}
else
{
mActiveBitmap = mActiveNormalIcon;
}
}
protected boolean navigationClick(int status, int time)
{
mActiveBitmap = mActiveNormalIcon;
toggleState();
invalidate();
fieldChangeNotify(1);
return true;
}
public void toggleState()
{
isStateActive = !isStateActive;
}
public int getPreferredWidth()
{
return mActiveBitmap.getWidth() + 20;
}
public int getPreferredHeight()
{
return mActiveBitmap.getHeight() + 10;
}
protected void layout(int width, int height)
{
mWidth = getPreferredWidth();
mHeight = getPreferredHeight();
setExtent(mWidth, mHeight);
}
protected void paint(Graphics graphics)
{
graphics.drawBitmap(0, 5, mWidth, mHeight, mActiveBitmap, 0, 0);
// graphics.setColor(0xff0000);
// graphics.drawText(mActiveText, (mActiveBitmap.getWidth() -
// this.getFont().getAdvance("ON"))/2, mActiveBitmap.getHeight());
}
protected void drawFocus(Graphics graphics, boolean on)
{
}
public void activate()
{
mActiveBitmap = mActiveNormalIcon;
isStateActive = true;
invalidate();
}
public void deactivate()
{
mActiveBitmap = mNormalIcon;
isStateActive = false;
invalidate();
}
}
0
最簡單的方式做到這一點:
第1步:用特定座標繪製圖像(imageX,imageY)。 第2步:添加一個方法在你的代碼:
void pointerControl(int x, int y) {
if (x>imageX && x<imageX+imageName.getWidth() && y>imageY && y < imageY+imageName.getHeight) {
//to do code here
}
}
where imageName:name of image
imageX=x coordinate of image(Top-Left)
imageY=y coordinate of image(Top-Left)
相關問題
- 1. Blackberry - 將圖像添加到按鈕?
- 2. 如何在BlackBerry 10 Cascades中創建圖像按鈕?
- 3. 按鈕VS JAVA中的圖像按鈕
- 4. BlackBerry應用程序可以有圖像按鈕嗎?
- 5. iPhone中的按鈕圖像
- 6. 按鈕中的圖像-j2me
- 7. XAML中的圖像按鈕
- 8. GWT按鈕中的圖像
- 9. 按鈕中的GIF圖像
- 10. BlackBerry Air中的自定義按鈕?
- 11. 的按鈕圖像
- 12. 按鈕圖像
- 13. 圖像按鈕,圖像resoultion
- 14. BlackBerry ChoiceField按鈕尺寸
- 15. 像旋鈕一樣的圖像按鈕
- 16. 居中按鈕或圖像
- 17. iOS圖像按鈕按下按鈕
- 18. 作爲一個按鈕但沒有按鈕圖像的圖像
- 19. Android中的圖像vs視圖(按鈕,單選按鈕....)
- 20. .NET按鈕圖像
- 21. 圖像按鈕ASP.NET
- 22. Java圖像按鈕
- 23. 與按鈕圖像
- 24. 圖像按鈕Ajax.ActionLink
- 25. HTML - 圖像按鈕
- 26. UIToolBar按鈕圖像
- 27. iPhone圖像按鈕
- 28. Android圖像按鈕
- 29. 圖像和按鈕
- 30. Android圖像按鈕
要創建BlackBerry自定義圖像按鈕,您可以檢查此鏈接[點擊這裏](http://www.javacodeone.blogspot.in/2012/10/ blackberry-custom-playpause-image-button.html)在這裏你可以得到完整的源代碼和解釋 – String 2012-10-19 09:51:29