【问题标题】:Image Button in BlackBerry黑莓中的图像按钮
【发布时间】:2010-05-26 10:54:29
【问题描述】:

如何在 BlackBerry 中实现图像按钮?

【问题讨论】:

  • 要创建一个 BlackBerry 自定义图像按钮,您可以查看此链接 click here 此处提供完整的源代码和说明

标签: blackberry java-me


【解决方案1】:

给你,完整的代码:

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;           
        }
}

【讨论】:

  • 我知道这已经过去了一段时间,但我需要添加这些方法才能使其正常工作 - protected void onFocus(int direction) { this.setHighlight(true); super.onFocus(方向); } 受保护的无效 onUnfocus() { this.setHighlight(false); super.onUnfocus(); }
【解决方案2】:

使用 RIM 的高级 UI 包。

http://supportforums.blackberry.com/t5/Java-Development/Implement-advanced-buttons-fields-and-managers/ta-p/488276

这包含一个 BitmapButton 字段和大量有用的 UI 工具。

(毫无疑问,Reflogs 示例很好,但我认为对于登陆此页面的新 BB 开发人员来说,高级 UI 包更有益)

【讨论】:

    【解决方案3】:
    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();
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      最简单的方法:

      步骤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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        相关资源
        最近更新 更多