【问题标题】:Blackberry add border to image when focus黑莓对焦时为图像添加边框
【发布时间】:2015-10-28 19:56:04
【问题描述】:

我正在尝试制作可触摸的图像,当我通过设备上的可触摸按钮滚动它时可以聚焦这是我的可触摸图像代码:

class TouchBitmapField extends BitmapField {
    Bitmap bitmap;
    boolean second;
    int width;
    public TouchBitmapField(Bitmap startBitmap, long style, int width, boolean second) {
        super(startBitmap, style);
        this.second = second;
        bitmap = startBitmap;
        this.width = width;
    }

    protected void drawFocus(Graphics g, boolean on){
        g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true );
        if(on){
           drawHighlightRegion(g, Field.HIGHLIGHT_FOCUS, on, width-(bitmap.getWidth()/2), 0, bitmap.getWidth(), bitmap.getHeight());
        }

        paint(g);
    }
    protected boolean touchEvent(TouchEvent message) {
        if (TouchEvent.CLICK == message.getEvent()) {
            FieldChangeListener listener = getChangeListener();
            if (null != listener)
                listener.fieldChanged(this, 1);
        }
        return super.touchEvent(message);
    }
}

但问题是只有图像的透明部分可以聚焦,我在创建 TouchBitmapField 之前调整了这个位图,你可能知道调整位图删除透明部分并将其替换为黑色。我尝试使用 http://www.patchou.com/2010/10/resizing-transparent-bitmaps-with-the-blackberry-jde/ 这个类来调整图像大小,但我的屏幕上有 25 张图像,而且效率低下。知道如何在图像上制作边框吗?在drawFocus(Graphics g, boolean on) 方法中绘制边框的任何有效方法?

【问题讨论】:

    标签: blackberry java-me


    【解决方案1】:

    之所以只能聚焦图像的透明部分,是因为(据我所知)drawFocuspaint 之前被触发,导致位图最后绘制。

    我认为这个解决方案正是您所寻找的。如果你想用纯色以外的东西作为边框,你可以试试BorderFactory

    class TouchBitmapField extends BitmapField
    {
        Bitmap bitmap;
        Border defaultBorder;
        Border focusBorder;
    
        public TouchBitmapField(Bitmap startBitmap, long style)
        {
            super(startBitmap, style | FOCUSABLE);
            bitmap = startBitmap;
    
            XYEdges thickness = new XYEdges(5, 5, 5, 5);
            XYEdges colours = new XYEdges(0xff0000, 0xff0000, 0xff0000, 0xff0000);
            XYEdges alpha = new XYEdges(0, 0, 0, 0);
    
            // Transparent border used by default, so that adding the focus border does not resize the view
            defaultBorder = BorderFactory.createSimpleBorder(thickness, colours, alpha, Border.STYLE_SOLID); 
            focusBorder = BorderFactory.createSimpleBorder(thickness, colours, Border.STYLE_SOLID);
    
            setBorder(defaultBorder);
        }
    
        protected void drawFocus(Graphics graphics, boolean on)
        {
            // Override default blue highlight
        }
    
        protected void onFocus(int direction)
        {
            super.onFocus(direction);
            setBorder(focusBorder);
        }
        protected void onUnfocus()
        {
            super.onUnfocus();
            setBorder(defaultBorder);
        }
    }
    

    如果您希望边框与图像重叠,您可以覆盖 paint 方法并根据需要进行绘制。

    protected void paint(Graphics graphics)
        {
            super.paint(graphics);
    
            if(isFocus())
            {
                int tempCol = graphics.getColor();
                int tempAlpha = graphics.getGlobalAlpha();
    
                graphics.setColor(0xff0000);
                graphics.setGlobalAlpha(128);
    
                for(int i = 0; i < 5; i++)
                {
                    graphics.drawRect(i, i, getWidth() - i * 2, getHeight() - i * 2);
                }
    
                // Reset back to original state
                graphics.setColor(tempCol);
                graphics.setGlobalAlpha(tempAlpha);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多