05-05-2012 04:34 PM
Hello guys, how do I listen to a click on a bitmap image in my code. I know
bitmapField1 = new BitmapField(mImgTable , BitmapField.FOCUSABLE)
{
protected boolean navigationClick(int status, int time )
{
}
}That will do it, but my code is different. Here is where i declare my bitmap images
public class CustomButtonScreen extends MainScreen{
NewFlowFieldManager _manager = new NewFlowFieldManager();
public CustomButtonScreen()
{
super();
super.add(_manager);
Bitmap composePic = Bitmap.getBitmapResource("compose.png");
Bitmap homePic = Bitmap.getBitmapResource("home.png");
Bitmap profilePic = Bitmap.getBitmapResource("profile.png");
Bitmap searchPic = Bitmap.getBitmapResource("search.png");
/*
homePic.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field f, int arg1) {
NextScreen ns = new NextScreen();
UiApplication.getUiApplication().pushScreen(ns);
}
});
*/
_manager.add(new BitmapButtonField(homePic));
_manager.add(new BitmapButtonField(searchPic));
_manager.add(new BitmapButtonField(profilePic));
_manager.add(new BitmapButtonField(composePic));
setTitle("LWKMD 1.0");
}
public void paintBackground(Graphics graphics)
{
graphics.setBackgroundColor(Color.DARKRED);
graphics.clear();
super.paintBackground(graphics);
}
}and the class where i draw the bitmap and add focus method
public class BitmapButtonField extends BaseButtonField{
private Bitmap[] _bitmaps;
private static final int NORMAL = 0;
private static final int FOCUS = 1;
private static final int CURVE_X = 12; // X-axis inset of curve
private static final int CURVE_Y = 12; // Y-axis inset of curve
private static final int BACKGROUND_COLOR = 0xFFFFFF; // White
private static final byte[] PATH_POINT_TYPES = {
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT ,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT ,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT ,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_END_POINT,
Graphics.CURVEDPATH_QUADRATIC_BEZIER_CONTROL_POINT ,
Graphics.CURVEDPATH_END_POINT,
};
// Colors array for rounded rectangle gradient. Each color corresponds
// to one of the points in the point types array. Top light, bottom black.
private static final int[] PATH_GRADIENT = {
0xE3E3E3, 0xE3E3E3, 0xE3E3E3, 0xE3E3E3, 0xE3E3E3, 0xE3E3E3,
0xC4C4C4, 0xC4C4C4, 0xC4C4C4, 0xC4C4C4, 0xC4C4C4, 0xC4C4C4
};
public BitmapButtonField( Bitmap normalState ){
this( normalState, normalState, 0 );
}
public BitmapButtonField( Bitmap normalState, Bitmap focusState){
this( normalState, focusState, 0 );
}
public BitmapButtonField( Bitmap normalState, Bitmap focusState, long style ){
super( Field.FIELD_HCENTER | Field.FIELD_VCENTER | Field.FIELD_BOTTOM |Field.FOCUSABLE );
if( (normalState.getWidth() != focusState.getWidth()) || (normalState.getHeight() != focusState.getHeight()) ){
throw new IllegalArgumentException( "Image sizes don't match" );
}
setPadding(5,5,5,5);
_bitmaps = new Bitmap[] { normalState, focusState };
}
public void setImage( Bitmap normalState ){
_bitmaps[NORMAL] = normalState;
invalidate();
}
public void setFocusImage( Bitmap focusState ){
_bitmaps[FOCUS] = focusState;
invalidate();
}
public int getPreferredWidth() {
return _bitmaps[NORMAL].getWidth();
}
public int getPreferredHeight() {
return _bitmaps[NORMAL].getHeight();
}
protected void layout( int width, int height ) {
setExtent( _bitmaps[NORMAL].getWidth(), _bitmaps[NORMAL].getHeight());
}
protected void paint( Graphics g ) {
int index = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? FOCUS : NORMAL;
g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
}
/**
* With this commented out the default focus will show through
* If an app doesn't want focus colours then it should override this and do nothing
**/
/*
protected void paintBackground( Graphics g ) {
g.clear();
}
*/
protected void drawFocus( Graphics g, boolean on ) {
// Paint() handles it all
g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true );
int width = getPreferredWidth();
int height = getPreferredHeight();
int[] xPts = {
0, 0, CURVE_X, width - CURVE_X, width, width,
width, width, width - CURVE_X, CURVE_X, 0, 0
};
int[] yPts = {
CURVE_Y, 0, 0, 0, 0, CURVE_Y,
height - CURVE_Y, height, height, height, height, height - CURVE_Y
};
if(on){
//drawHighlightRegion(g, Field.HIGHLIGHT_FOCUS, on, 0, 0, 500, 200);
g.clear();
g.setGlobalAlpha(255);
g.setBackgroundColor(BACKGROUND_COLOR);
g.drawShadedFilledPath(xPts, yPts, PATH_POINT_TYPES, PATH_GRADIENT, null);
g.drawRoundRect(0, 0, height, width, CURVE_X * 2, CURVE_Y * 2);
// g.fillRoundRect(0, 0, height, width, CURVE_X * 2, CURVE_Y * 2);
// g.setColor(BORDER_COLOR);
}
paint(g);
}
}
05-06-2012 11:07 AM
hi,
you can override navigationclick or trackwheel click in you BitmapButtonField.
05-06-2012 12:27 PM
And just to give you some code:
Bitmap homePic = Bitmap.getBitmapResource("home.png");
BitmapButtonField homepicField = new BitmapButtonField(homePic));
homepicField.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field f, int arg1) {
NextScreen ns = new NextScreen();
UiApplication.getUiApplication().pushScreen(ns);
}
});
_manager.add(homepicField);