【问题标题】:BlackBerry - How to set image as a background for ButtonField?BlackBerry - 如何将图像设置为 ButtonField 的背景?
【发布时间】:2011-02-08 05:48:51
【问题描述】:

如何在 BlackBerry 中将图像设置为 ButtonField 的背景?

【问题讨论】:

    标签: user-interface image blackberry button


    【解决方案1】:

    其他方法是扩展ButtonField并在paint上绘制图像:

    class BitmapButtonField extends ButtonField {
        Bitmap mNormal;
        Bitmap mFocused;
        Bitmap mActive;
    
        int mWidth;
        int mHeight;
    
        public BitmapButtonField(Bitmap normal, Bitmap focused, 
            Bitmap active) {
            super(CONSUME_CLICK);
            mNormal = normal;
            mFocused = focused;
            mActive = active;
            mWidth = mNormal.getWidth();
            mHeight = mNormal.getHeight();
            setMargin(0, 0, 0, 0);
            setPadding(0, 0, 0, 0);
            setBorder(BorderFactory
                            .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
            setBorder(VISUAL_STATE_ACTIVE, BorderFactory
                            .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        }
    
        protected void paint(Graphics graphics) {
            Bitmap bitmap = null;
            switch (getVisualState()) {
            case VISUAL_STATE_NORMAL:
                    bitmap = mNormal;
                    break;
            case VISUAL_STATE_FOCUS:
                    bitmap = mFocused;
                    break;
            case VISUAL_STATE_ACTIVE:
                    bitmap = mActive;
                    break;
            default:
                    bitmap = mNormal;
            }
            graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
                            bitmap, 0, 0);
        }
    
        public int getPreferredWidth() {
            return mWidth;
        }
    
        public int getPreferredHeight() {
            return mHeight;
        }
    
        protected void layout(int width, int height) {
            setExtent(mWidth, mHeight);
        }
    }
    

    sample of use

    【讨论】:

      【解决方案2】:

      我不相信您可以将图像设置为ButtonField。相反,您可以扩展BitmapField 类,覆盖trackwheelClick 函数并使用onFocus 来确定是否是该字段被单击。这将给出一个“可点击”的图像。

      【讨论】:

        猜你喜欢
        • 2014-06-23
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多