04-14-2009 08:00 AM
hi,
How to draw any image in background of MainScreen ? the solutions mentioned in other threads of forum, is not working .
The solution mentioned using Background Class works but that API is provided only in 4.7.
For other JDK problem still remains.
04-14-2009 08:06 AM
04-14-2009 08:07 AM
package com.samples.backgroundImage; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*; public class BackgroundImage extends UiApplication { private Bitmap backgroundBitmap; private Bitmap fieldBitmap; public static void main(String[] args) { BackgroundImage theApp = new BackgroundImage(); theApp.enterEventDispatcher(); } public BackgroundImage() { //The background image. backgroundBitmap = Bitmap.getBitmapResource("background.png"); MainScreen mainScreen = new MainScreen(); HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_
ALL_WIDTH | HorizontalFieldManager.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. graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0); super.paint(graphics); } }; //The LabelField will show up through the transparent image. LabelField labelField = new LabelField("This is a label"); //A bitmap field with a transparent image. //The background image will show up through the transparent BitMapField image. BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("field.png")) ; //Add the manager to the screen. mainScreen.add(horizontalFieldManager); BasicEditField bef = new BasicEditField("To: ","",50,BasicEditField.FILTER_EMAIL); horizontalFieldManager.add(bef); //Add the fields to the manager. horizontalFieldManager.add(labelField); horizontalFieldManager.add(bitmapField); //Push the screen. pushScreen(mainScreen); } }
04-14-2009 09:50 AM - edited 04-14-2009 10:03 AM
04-15-2009 02:37 AM - edited 04-15-2009 03:07 AM
Try this:
class MSBMainScreen extends MainScreen { private VerticalFieldManager _container; private Bitmap _BG; public MSBMainScreen() { super(); setTitle( "Main Screen Background Hack" ); _BG = Bitmap.getBitmapResource( "background.png" ); _container = new VerticalFieldManager( Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR ) { protected void paint( Graphics g ) { // Instead of these next two lines, draw your bitmap int y = MSBMainScreen.this.getMainManager().getVerticalScr
oll(); g.drawBitmap( 0, y, _BG.getWidth(), _BG.getHeight(), _BG, 0, 0 ); super.paint( g ); } // The keydown and navigation overrides are a workaround // for an optimization hack RIM performed, perhaps assuming that // Managers would not try to take control of painting on // a MainScreen. protected boolean keyDown( int keycode, int status ) { invalidate(); return super.keyDown( keycode, status ); } protected boolean navigationMovement( int dx, int dy, int status, int time ) { invalidate(); return super.navigationMovement( dx, dy, status, time ); } }; super.add( _container ); } public void add( Field field ) { _container.add( field ); } public void delete( Field field ) { _container.delete( field ); } // Implement the rest of the field manipulation functions to redirect ro the new container }
Cheers
04-21-2009 12:28 AM
Hi,
Thanks a lot your solution it really works.
One more thing we have to also overide moveFocus() of Manager so that in case of trackball move , image get painted properly.
here is the code for that
protected int moveFocus(int amount, int status, int time){
invalidate();
return super.moveFocus(amount, status, time);
}
The reason behind using mainscreen might be to avoid implementation on touch events.
04-21-2009 01:07 AM
Hi Rajat,
Overriding navigationMovement() should be enough. Are you observing something different?
04-21-2009 01:11 AM
Hi,
Yes actually I am also adding listField in this manager, so when i scroll with in listfield the image in manager do not paint itself properly so i added that piece of code and it worked fined
04-21-2009 01:14 AM
That's very odd because that's exactly how I tested this one.
Check this out and let me know if there's something you do differently:
class MSBMain extends UiApplication { public static void main( String[] args ) { new MSBMain().enterEventDispatcher(); } MSBMain() { // The following is really lazy code. Do not do this in // a consumer grade app :) String[] objects = new String[20]; for ( int i = 0; i < 20; i++ ) { objects[i] = "Line: " + (i + 1); } ObjectListField olf = new ObjectListField(); olf.set( objects ); MSBMainScreen msbms = new MSBMainScreen(); msbms.add( olf ); pushScreen( msbms ); } }
04-27-2009 09:08 AM
Hi,
Sorry for replying late
below is the code which i am using
import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.ui.container.VerticalFieldManag
er; import net.rim.device.api.ui.component.BitmapField; import net.rim.device.api.ui.Graphics; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.component.ListField; import net.rim.device.api.ui.component.ListFieldCallback; import net.rim.device.api.system.Bitmap; import net.rim.device.api.ui.Manager; class MSBMainScreen extends MainScreen implements ListFieldCallback { String Data[] = {"Task ID","Priority","Task Name","Location","Task Type","State","AAA","BBB","CCCC","DDD","EEE","FFFF ","GGGG","HHHH","IIII"}; private VerticalFieldManager objManager; private Bitmap objBitmap; ListField objList = null; public MSBMainScreen(){ super(); drawComponent(); setTitle( "Inbox Screen" ); objBitmap = Bitmap.getBitmapResource( "watermark.png"); objManager = new VerticalFieldManager( Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR ) { protected void paint( Graphics g ){ int y = MSBMainScreen.this.getMainManager().getVerticalScr oll(); g.drawBitmap( 0, y, objBitmap.getWidth(), objBitmap.getHeight(), objBitmap, 0, 0 ); super.paint( g ); } protected boolean keyDown( int keycode, int status ){ invalidate(); return super.keyDown( keycode, status ); } protected boolean navigationMovement( int dx, int dy, int status, int time ){ invalidate(); return super.navigationMovement( dx, dy, status, time ); } protected int moveFocus(int amount, int status, int time){ invalidate(); return super.moveFocus(amount, status, time); } }; add(objList); super.add( objManager ); } public void add( Field field ){ objManager.add( field ); } public void delete( Field field ){ objManager.delete( field ); } public void drawListRow(ListField listField, Graphics g, int index, int y, int width){ g.drawText(Data[index], /*10*/10, /*CTIAMain.ROW_HEIGHT*index*/y ); } public int indexOfList(ListField listField, String prefix, int start){ return 0; } public Object get(ListField listField, int index){ return Data[index]; } public int getPreferredWidth(ListField listField){ return 0; } private void drawComponent(){ objList = new ListField(Data.length); objList.setCallback(this); } }
If i comment moveFocu() then , when i press trackball the image below list does not paint itself properly, but if i uncomment moveFocu() it works fine.