黑莓添加边框图像时,焦点 [英] Blackberry add border to image when focus

查看:132
本文介绍了黑莓添加边框图像时,焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使能集中时,我通过在设备可触摸按钮滚动它触及的形象,这是我的code为可触摸的图像:

I'm trying to make touchable image which can focus when I scroll it by touchable button on device this is my code for touchable image:

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

但问题是,只有图像的透明部分可以集中精力,我resising此位图之前创建TouchBitmapField,当你知道也许resising位取出透明部分和黑色替换它。我想这个类的http://www.patchou.com/2010/10/resizing-transparent-bitmaps-with-the-blackberry-jde/调整大小的图像,但我有我的屏幕上25张图片,它的效率不高。任何想法如何使边境上的形象?任何有效的方法来绘制边界 DRAWFOCUS(图形克,布尔值on)方法?

But the problem is that only transparent part of image can focus and I'm resising this Bitmap before create TouchBitmapField and as you maybe know resising bitmap remove transparent part and replace it with black. I tried this class http://www.patchou.com/2010/10/resizing-transparent-bitmaps-with-the-blackberry-jde/ to resize image but I have 25 images on my screen and it's inefficient. Any idea how to make border on image? Any efficient way to draw border in drawFocus(Graphics g, boolean on) method?

推荐答案

你之所以可以只注重图像的透明部分是因为(据我所知) DRAWFOCUS 油漆,导致最后的位图要绘制之前被激发。

The reason why you can only focus the transparent parts of an image is because (as far as I know) drawFocus is fired before paint, causing the bitmap to be drawn last.

我觉得这个解决方案是你在找什么。如果你想比纯色的边框以外的东西,你可以玩的 BorderFactory

I think this solution is what you're looking for. If you want something other than a solid colour as the border, you can play around with the 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);
    }
}

如果你希望你的边界是重叠的图像,则可以覆盖你的油漆方法,只要你想得出。

If you want your border to be overlapping your image, you can override your paint method and draw as you want.

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

这篇关于黑莓添加边框图像时,焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆