CustomBitmapField

Hi, friends, I implemented a CustomBitmapField but I need one highlights when I focus on it so that I implement:

protected void drawFocus (Graphics g, boolean on) {}
Paint (g);
System.out .println ("drawFocus en is on?:" + I "");
If (on) {}
int prevAlpha = g.getGlobalAlpha ();
int prevColor = g.getColor ();
g.setGlobalAlpha (128);
g.setColor (Color.ALICEBLUE);
g.fillRect (0, 0, getWidth(), getHeight());
g.setColor (prevColor);
g.setGlobalAlpha (prevAlpha);
}
}

but the way she's only highligth my when the land is on:

1 I do not understand when is on

2. I have this bitmapfields 5 in a Manager, but it is only trackball go right but when I go to the left is not isn't highligth

can help me some body explain me about this

Looks like my code I posted in this forum some time ago.

Anyway - I'm not sure that I fully understand what you're asking, but:

(1) in drawFocus is true if the field has the focus and false otherwise. So this code will give your image Bitmap a tinge of ALICEBLUE when it has focus and will normally paint what it doesn't. Is that what you see?

(2) what exactly do you see when you roll the trackball to the left? No target field? If yes then I saw something similar on different BlackBerry simulators and phones every now and then - although in my case, the fields remained focused, but had a few graphical artifacts on the bottom. I guess that some of the code to update/unfocusing in older versions of BlackBerry OS is broken.

Tags: BlackBerry Developers

Similar Questions

  • CustomBitmapField to display the text

    I want my custom BitmapField to display stationary text from the image and this is my code:

    package com.codemobiles.orientthai.field;
    
    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.DrawStyle;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.Ui;
    import net.rim.device.api.ui.component.BitmapField;
    
    public class CustomBitmapField extends BitmapField {
    
        public String aText = "";
        public Bitmap focusBitmap;
        public Bitmap unfocusBitmap;
        private boolean hasfocus;
        public Font FONT_PLAIN;
        public int m_MarginTop;
    
        protected void onFocus(int direction) {
            // TODO Auto-generated method stub
            hasfocus = true;
        }
    
        protected void onUnfocus() {
            // TODO Auto-generated method stub
            hasfocus = false;
        }
    
        public CustomBitmapField(String aText, Bitmap onfocusB, Bitmap unfocusB) {
    
            this.aText = aText;
            this.focusBitmap = onfocusB;
            this.unfocusBitmap = unfocusB;
            FONT_PLAIN = Font.getDefault().derive(Font.PLAIN, 5, Ui.UNITS_pt);
    
        }
    
        protected void layout(int width, int height) {
            // TODO Auto-generated method stub
            super.layout(width, height);
            setExtent(focusBitmap.getWidth(), focusBitmap.getHeight());
        }
    
        protected void paint(Graphics graphics) {
            // TODO Auto-generated method stub
            if (focusBitmap instanceof Bitmap) {
                m_MarginTop = (focusBitmap.getHeight() - FONT_PLAIN.getHeight() / 4);
                if (hasfocus == true) {
                    graphics.drawBitmap(0, 0, focusBitmap.getWidth(), focusBitmap.getHeight(), focusBitmap, 0, 0);
                } else {
                    graphics.drawBitmap(0, 0, unfocusBitmap.getWidth(), unfocusBitmap.getHeight(), unfocusBitmap, 0, 0);
    
                }
                graphics.drawText(aText, 0, m_MarginTop, DrawStyle.ELLIPSIS,
                        getWidth() - 15);
                invalidate();
            }
        }
    }
    

    I tried this code and it showing on the picture just of the screen.

    I did something wrong?

    Help, please

    Thank you

    Concerning

    It works now. I changed the drawtext to become this:

    graphics.drawText(aText, getWidth()/3, getHeight()/3, DrawStyle.ELLIPSIS,
                        focusBitmap.getWidth() );
    

    Hope it will help.

    Concerning

  • custom bitmaps field not change image when click on

    I created a custom bitmap field and I change the image on the center of my field of custom bitmaps and unfocus

    but when I implement the method of navigation click to change the image when you click on this image does not change...

    my custom bitmap field filed only to focus or unfocus on the image does not change my field of custom bitmaps.

    Bitmap onfocus = Bitmap.getBitmapResource("111.png");
                    Bitmap onunfocus = Bitmap.getBitmapResource("666.png");
                    mybutton1 = new CustomBitmapField(0,"",onunfocus,onfocus,Field.FOCUSABLE){
                         protected boolean navigationClick(int status, int time) {
    
                             mybutton1.setBitmap(Bitmap.getBitmapResource("favu.png"));
    
                             return true;
    
                         }
    
                    };
    add(mybutton1);
    

    and my class of field customBitmap is

    public class CustomBitmapField extends BitmapField {
    
        Bitmap Unfocus_img, Focus_img, current_pic;
        int width;
        String text;
        Font font; 
    
        public CustomBitmapField(int width, String text, Bitmap onFocus, Bitmap onUnfocus, long style) {
            // TODO Auto-generated constructor stub
            super();
            Unfocus_img = onUnfocus;
            Focus_img = onFocus;
            current_pic = onFocus;
            this.text = text;
            this.width = width;
    
        }
        protected void layout(int width, int height)
        {
            setExtent(current_pic.getWidth(), current_pic.getHeight());
        }
        protected void paint(Graphics graphics)
        {
            /*try
            {
                    FontFamily fntFamily = FontFamily.forName("BBAlpha Sans");
                    font = fntFamily.getFont(Font.BOLD,14);
            }
            catch(Exception e)
            {
                font = Font.getDefault();
    
            }
            graphics.setFont(font);
            graphics.setColor(Color.WHITE); */
            graphics.drawBitmap(0, 0, current_pic.getWidth(), current_pic.getHeight(), current_pic, 0, 0);
    
            //graphics.drawText(text, width, 7);
        }
        protected void onFocus(int direction)
        {
            super.onFocus(direction);
            current_pic = Unfocus_img;
    
            this.invalidate();
        }
      protected void drawFocus(Graphics graphics, boolean on)
      {
    
        }
        protected void onUnfocus()
        {
            super.onUnfocus();
            current_pic = Focus_img;
            invalidate();
        }
        public boolean isFocusable() {
            return true;
        }
        protected boolean navigationClick(int status, int time) {
    
            fieldChangeNotify(0);
            return true;
        }
    
    }
    

    When you debug this code, which of your methods of navigationClick, is actually used?

    When you expect the bitmap change, the code should lead object so that to happen.  Paint running?  This is the correct Bitmap painting?

    If you answer these questions, I think that you figure to yourself what is the problem.

    While you're there, you can also watch the following Threads that you have started or contributed to.  Can you solve or continue to contribute to these?

    http://supportforums.BlackBerry.com/T5/Java-development/gif-animation-not-displaying-properlly/m-p/2...

    http://supportforums.BlackBerry.com/T5/Java-development/timepicker-issue/m-p/2387819

    http://supportforums.BlackBerry.com/T5/Java-development/ListField-focus-color-problem/m-p/2407881#m2...

  • Storm Long RichTextField

    Hello comrades!

    I had a situation associated with RichTextField that contains a long text on the device of the storm.

    The problem is: I am able to scroll the text, but I can't move the focus to a CustomBitmapField, are complemented by the text Manager, until I reach the end of the text.

    The button appears on the screen, but when the user clicks the text above simply scroll down.

    All entries? Thanks in advance.

    public class Screen extends FullScreen implements FieldChangeListener {
    
        private Bitmap startQuizNormal, startQuizFocused, bitmapBackground;
        private CustomBitmapField startQuiz;
        private BitmapField introImage;
    
        private VerticalFieldManager introTextouterManager;
    
        private int startQuizHeight, introTextHeight;
    
        private String text;
    
        private int width;
        private int height;
    
        public IntroScreen() {
            super(DEFAULT_CLOSE | USE_ALL_HEIGHT | USE_ALL_WIDTH | Screen.NO_VERTICAL_SCROLL);
    
            this.width = Display.getWidth();
            this.height = Display.getHeight();
            this.renderScreen();
        }
    
        private void renderScreen() {
    
            RichTextField introduction;
    
            Font fonts[] = new Font[2];
            int[] offset = new int[7];
            byte[] attribute = new byte[6];
    
            fonts[0] = Font.getDefault().derive(Font.PLAIN, 18, Ui.UNITS_px);
            fonts[1] = Font.getDefault().derive(Font.BOLD, 19, Ui.UNITS_px);
    
            text = "long text...";
    
            offset[0] = 0;
            attribute[0] = 1;
    
            offset[1] = 15;
            attribute[1] = 0;
    
            offset[2] = 1377;
            attribute[2] = 1;
    
            offset[3] = 1396;
            attribute[3] = 0;
    
            offset[4] = 1652;
            attribute[4] = 1;
    
            offset[5] = 1664;
            attribute[5] = 0;
    
            offset[6] = text.length();
    
            if (this.width == 360) {
                this.bitmapBackground = Bitmap.getBitmapResource("background360x480.jpg");
            } else {
                this.bitmapBackground = Bitmap.getBitmapResource("background480x360.jpg");
            }
    
            startQuizNormal = Bitmap.getBitmapResource("startquiznormal.png");
            startQuizFocused = Bitmap.getBitmapResource("startquizfocused.png");
    
            startQuizHeight = startQuizNormal.getHeight();
    
            introTextHeight = this.height - (startQuizHeight + 16);
    
            introduction = new RichTextField(text, offset, attribute, fonts, RichTextField.USE_TEXT_WIDTH | RichTextField.READONLY );
    
            startQuiz = new CustomBitmapField(startQuizFocused, startQuizNormal, FIELD_HCENTER | FIELD_VCENTER);
            startQuiz.setChangeListener(this);
    
            introTextouterManager = new VerticalFieldManager(FIELD_HCENTER | VERTICAL_SCROLL | NON_FOCUSABLE) {
                protected void sublayout(int maxWidth, int maxHeight) {
                    super.sublayout(width - 40, introTextHeight);
                    super.setExtent(width - 40, introTextHeight);
                }
            };
    
            introTextouterManager.add(introduction);        
    
            add(introTextouterManager);
    
            add(startQuiz);
    
        }
    
        protected void paintBackground(Graphics g) {
            g.drawBitmap(0, 0, this.width, this.height, this.bitmapBackground, 0, 0);
        }
    
        public void fieldChanged(Field field, int context) {
            if (context == 0)
                UiApplication.getUiApplication().pushScreen(new MenuScreen());
        }
    
        protected void sublayout(int width, int height) {
            if (this.width != width) {
                this.width = width;
                this.height = height;
                UiApplication.getUiApplication().invokeLater(new Runnable(){
                    public void run() {
                        renderScreen();
                    }
                });
            }
            super.sublayout(width, height);
        }
    }
    

    Everyday learning!

    A big thank you Vignesh J. for the idea.

    So, I just replace the touchEvent at the level of the screen, but rather to return false when the target button was not clicked on return "super.touchEvent (event)". And then it works! Not sure if this solution is the best.

    In addition, as suggested, we composed a ClickListener instead of implements FieldChangeListener.

    public interface ClickListener {
       public boolean onNavigationBallClicked(Field f,int status, int time);
    }
    
    public class IntroScreen extends FullScreen implements ClickListener {
    
    ...
    
      startQuiz = new CustomBitmapField(startQuizFocused, startQuizNormal,
                    FIELD_HCENTER | FIELD_VCENTER);
      startQuiz.setClickListener(this);
    
    ...
    
    protected boolean touchEvent(TouchEvent event){
        int x = event.getX(1);
        int y = event.getY(1);
        int index = getFieldAtLocation(x, y);
        try {
            Field f = getField(index);
            if (f instanceof CustomBitmapField) {
                switch(event.getEvent()){
                case TouchEvent.DOWN:
                    f.setFocus();
                    invalidate();
                    return true;
                case TouchEvent.UNCLICK:
                    return ((CustomBitmapField)f).getClickListener().onNavigationBallClicked(this, 0, 0);
                }
            }
        } catch (Exception e) {
            return super.touchEvent(event);
        }
        return super.touchEvent(event);
      }
    
    }
    
  • How to resize the Bitmap when running?

    Hello

    I created a Bitmap that reads directly from a file. The actual size of the image (test.png) is 360 x 50. I displayed this bitmap image in a BitmapField.  Now, I want to display this image in my application so that it should take the width of the screen and adjust (expand or compress) so based on the TILT of the device (in the case of BB storm). My code is as below:

    {
       Bitmap image = Bitmap.getBitmapResource("mainmenu_green.png");
        BitmapField bmpMainMenu = new BitmapField(image);
    
        MenuScreenVFM verticalManager = new MenuScreenVFM();
        verticalManager.add(bmpMainMenu);
        add(verticalManager );
    }
    
    //MenuScreenVFM  Class..
     static class MenuScreenVFM extends VerticalFieldManager
     {
            // Constructor
            private MenuScreenVFM()
            {
                super(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | VerticalFieldManager.FIELD_HCENTER);
            }
    
           net.rim.device.api.ui.container.VerticalFieldManager#sublayout(int,int)
             */
            protected void sublayout(int width, int height)
            {
                int nTop = 65;
                Field field = getField(0);
                layoutChild(field, Display.getWidth(), 40);
                setPositionChild(field, 0, nTop);
                setExtent( Display.getWidth(), Display.getHeight());
            }
    }
    

    The image must stretch/compress based on the width of the screen. Any info/code would be really appreciated.

    Thank you

    -DK

    I created a CustomBitmapField extends the field and for the scaling of the image, I used the following code:

       public static EncodedImage scaleImageToWidth(EncodedImage encoded, int newWidth) {
                 return scaleToFactor(encoded, encoded.getWidth(), newWidth);
       }
    
        public static EncodedImage scaleImageToHeight(EncodedImage encoded, int newHeight) {
               return scaleToFactor(encoded, encoded.getHeight(), newHeight);
        }
    
        public static EncodedImage scaleToFactor(EncodedImage encoded, int curSize, int newSize) {
             int numerator = Fixed32.toFP(curSize);
             int denominator = Fixed32.toFP(newSize);
             int scale = Fixed32.div(numerator, denominator);
    
             return encoded.scaleImage32(scale, scale);
        }
    
      protected void paint(Graphics graphics) {
            graphics.setColor(backgroundColour);
            graphics.drawImage(0, 0, Display.getWidth(), fieldHeight,   this.scaleImageToWidth(bmpBG, Display.getWidth()), 0, 0, 0);
               }
    

    I would like to know if it works... If works please set Bravo... that will encourage those who post the solution

    Kind regards

    -DK

  • Why all my target areas have blue background

    Please tell me how to hide or remove the blue background of my focused fields.

    The non-target fields have no blue background.

    Thank you

    public class SignInScreen extends MainScreen {
        public int displayWidth = Display.getWidth();
        public int displayHeight = Display.getHeight();
        CustomInputField cifUser = new CustomInputField((int)(displayWidth/4), (int)(displayHeight/4), (int)(displayWidth/2), (int)(displayHeight/16), 16, "Username", "Textbox.png");
        CustomInputField cifPW = new CustomInputField((int)(displayWidth/4), (int)(displayHeight/24), (int)(displayWidth/2), (int)(displayHeight/16), 16, "Password", "Textbox.png");
        public SignInScreen() {
            super(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR | USE_ALL_WIDTH | USE_ALL_HEIGHT);
            VerticalFieldManager vfManager = new VerticalFieldManager(NO_VERTICAL_SCROLL | NO_VERTICAL_SCROLLBAR | USE_ALL_WIDTH | USE_ALL_HEIGHT)
            {
                //Override the paint method to draw the background image.
                public void paint(Graphics graphics)
                {
                    //Draw the background image and then call paint.
                    Bitmap newBGB = GPATools.ResizeTransparentBitmap(Bitmap.getBitmapResource("bg_pizza.jpg"), displayWidth,displayHeight, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
                    graphics.drawBitmap(0,0,displayWidth,displayHeight,newBGB,0,0);
                    super.paint(graphics);
                }
             };
            HorizontalFieldManager hfManager = new HorizontalFieldManager() {
                // overrides the managers paintBackground method, this draws the background
                protected void paintBackground(Graphics graphics) {
                    Bitmap newBar = GPATools.ResizeTransparentBitmap(Bitmap.getBitmapResource("back-bar.png"), displayWidth, displayHeight/10, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
                    graphics.drawBitmap(0,0,displayWidth,(int)(displayHeight/16),newBar,0,0);
                    super.paint(graphics);
                }
                protected void sublayout( int maxWidth, int maxHeight )
                {
                    super.sublayout( maxWidth, maxHeight );
                    XYRect extent = getExtent();
                    int width = Math.max( extent.width, displayWidth);
                    int height = Math.max( extent.height, (int)(displayHeight/16));
                    setExtent( width, height);
                }
    
            CustomBitmapField bbfBack = new CustomBitmapField((int)(displayWidth/90), (int)(displayHeight/216),(int)(displayWidth/5), (int)(displayHeight/18), FIELD_VCENTER | FIELD_LEFT | FOCUSABLE, "back-button.png", "back-button-on.png");
            CustomBitmapField bbfForgotPW = new CustomBitmapField((int)(displayWidth*0.45), 0, (int)(displayWidth/3), (int)(displayHeight/16), FIELD_VCENTER | FIELD_RIGHT | FOCUSABLE, "forgot-password-on.png", "forgot-password-on.png");
            CustomBitmapField bbfLogo = new CustomBitmapField((int)(displayWidth/4), (int)(displayHeight/24), (int)(displayWidth/2), (int)(displayHeight/6), FIELD_VCENTER | FIELD_HCENTER, "logo_pizza_onebitedefault.png", "logo_pizza_onebitedefault.png");
            CustomBitmapField bbfSubmit = new CustomBitmapField((int)(displayWidth*0.3), (int)(displayHeight/24), (int)(displayWidth*0.4), (int)(displayHeight/12), FIELD_VCENTER | FIELD_HCENTER | FOCUSABLE, "button_submit_pizza.png", "Submit-button-on.png");
            CustomInputField noAccount = new CustomInputField((int)(displayWidth*0.3), (int)(displayHeight/24), (int)(displayWidth*0.6), (int)(displayHeight/18), 22, "Don't Have an Account?", "No Account");
            CustomBitmapField bbfSignUp = new CustomBitmapField((int)(displayWidth*0.3), 0, (int)(displayWidth*0.4), (int)(displayHeight/12), FIELD_VCENTER | FIELD_HCENTER | FOCUSABLE, "Sign-In-button-off.png", "Sign-In-button-on.png");
            bbfBack.setChangeListener(backListener);
            bbfForgotPW.setChangeListener(backListener);
            bbfSubmit.setChangeListener(submitListener);
            bbfSignUp.setChangeListener(backListener);
            hfManager.add(bbfBack);
            hfManager.add(bbfForgotPW);
            vfManager.add(hfManager);
            vfManager.add(bbfLogo);
            vfManager.add(cifUser);
            vfManager.add(cifPW);
            vfManager.add(bbfSubmit);
            vfManager.add(noAccount);
            vfManager.add(bbfSignUp);
            add(vfManager);
            noAccount.setFocus();
         }
    }
    
    package oneBiteScreen;
    
    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.Graphics;
    /**
     * Button field with a bitmap as its label.
     */
    public class CustomBitmapField extends Field {
            private Bitmap b, bitmap, bitmapHighlight;
            private int x, y, width, height;
            public CustomBitmapField(int x, int y, int width, int height, long style, String offBitmap, String onBitmap) {
                super(style);
                this.width = width;
                this.height = height;
                this.bitmap = resizedImage(offBitmap, width, height);
                this.bitmapHighlight = resizedImage(onBitmap, width, height);
                this.b = this.bitmap;
                setPosition(x,y);
            }
            protected void fieldChangeNotify(int context) {
                try {
                    this.getChangeListener().fieldChanged(this, context);
                    } catch (Exception exception) {
                        }
                }
            protected boolean navigationClick(int status, int time){
                fieldChangeNotify(1);
                return true;
                }
            protected void layout(int width, int height) {
                setExtent(getPreferredWidth(), getPreferredHeight());
            }
            public int getPreferredWidth() {
              return width;
            }
            public int getPreferredHeight() {
              return height;
            }
            protected void onFocus(int direction){
                b = bitmapHighlight;
                invalidate();
                }
            protected void onUnfocus(){
                b = bitmap;
                invalidate();
                }
            protected void paint(Graphics graphics) {
                graphics.drawBitmap(0, 0, width, height, b, 0, 0);
            }
            public static Bitmap resizedImage(String imageName, int width, int height) {
                Bitmap actualBitmap = Bitmap.getBitmapResource(imageName);
                Bitmap resizedBitmap = new Bitmap(width, height);
                resizedBitmap.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);
                actualBitmap.scaleInto(resizedBitmap, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_STRETCH);
                return resizedBitmap;
            }
    }
    

    Two immediate things:

    (1) for your main problem replace drawFocus nothing to do:

    protected void drawFocus(Graphics g, boolean on) {
    }
    

    (2) onUnfocus is not always called by the framework (it's a bug I've seen several versions of BlackBerry OS). You are much better to use FieldChangeListener for switching of bitmaps depending on the State concentrate/blurry.

  • Draw focus problem

    Hi all

    I create a field when custombitmap gets field exchange focus bitmap image in the field. I want the focus by default to be transparent... I need to remove the default focus color (usually blue focus) here is my code.

    public class CustomBitmapField extends BitmapField {
        Bitmap pressImg,normalImg;
        public CustomBitmapField(Bitmap pressImg,Bitmap normalImg) {
            super(normalImg,Manager.FOCUSABLE);
            this.pressImg=pressImg;
            this.normalImg=normalImg;
    
        }
        protected void onFocus(int direction) {
            setBitmap(pressImg);
            super.onFocus(direction);
    
        }
    
        protected void onUnfocus() {
            setBitmap(normalImg);
            super.onUnfocus();
        }
    
        protected void drawFocus(Graphics graphics, boolean on) {
            int code = graphics.getColor();
            graphics.setBackgroundColor(Color.YELLOW);
            graphics.setGlobalAlpha(0);
            graphics.clear();
            graphics.setColor(code);
            super.drawFocus(graphics, on);
        }
    

    Working in eclipse with BB JRE 5.0. Everything works very well Simulator cannot see changes to bitmap and clear focus. But when I test it on the development of the device comes in black color. Entry field becomes black when he put me to the point... suite unfocus everything works fine... Please help me

    [Tests on the curve bb with OS 7.0 9360)

    Thank you

    Hi Sandra,.

    That sipmly follow these steps.

    protected void drawFocus(Graphics graphics, boolean on) { }
    

Maybe you are looking for