// Resize a bitmap proportionally (shrink or enlarge) to make it fit a maxX x maxY rectangle public static Bitmap getFitBitmapImage(String imagename, int maxX, int maxY) { EncodedImage image = EncodedImage.getEncodedImageResource(imagename); int currentWidthFixed32 = Fixed32.toFP(image.getWidth()); int currentHeightFixed32 = Fixed32.toFP(image.getHeight()); // double ratio = (double)ratioX / (double) ratioY; double rx = (double) image.getWidth() / (double) maxX; double ry = (double) image.getHeight() / (double) maxY; double r = 0; if (rx > ry) r = rx; else r = ry; double w = (double) image.getWidth() / r; double h = (double) image.getHeight() / r; int width = (int) w; int height = (int) h; int requiredWidthFixed32 = Fixed32.toFP(width); int requiredHeightFixed32 = Fixed32.toFP(height); int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32); int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32); image = image.scaleImage32(scaleXFixed32, scaleYFixed32); return image.getBitmap(); }
public BitmapField WriteSimpleImage( Manager parent, String resource, long style, int size, String listener) { EncodedImage ei = EncodedImage.getEncodedImageResource(resource); EncodedImage ei2 = getScaledImage(ei, size); BitmapField fImg = new BitmapField(ei2.getBitmap(), style); parent.add(fImg); return fImg; }
protected void init() { super.init(); if (SettingsManager.getSettings().isHomeScreenNotificationIconEnabled()) { // Register application icon. ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance(); reg.register( new ApplicationIcon(EncodedImage.getEncodedImageResource("small.png")), false, false); } if (SettingsManager.getSettings().isMessageIntegrationEnabled()) { // @todo message list integration } }
// Resize a bitmap proportionally (shrink or enlarge) by a ratio factor public static Bitmap getScaledBitmapImage(String imagename, double ratio) { EncodedImage image = EncodedImage.getEncodedImageResource(imagename); int currentWidthFixed32 = Fixed32.toFP(image.getWidth()); int currentHeightFixed32 = Fixed32.toFP(image.getHeight()); double w = (double) image.getWidth() * ratio; double h = (double) image.getHeight() * ratio; int width = (int) w; int height = (int) h; int requiredWidthFixed32 = Fixed32.toFP(width); int requiredHeightFixed32 = Fixed32.toFP(height); int scaleXFixed32 = Fixed32.div(currentWidthFixed32, requiredWidthFixed32); int scaleYFixed32 = Fixed32.div(currentHeightFixed32, requiredHeightFixed32); image = image.scaleImage32(scaleXFixed32, scaleYFixed32); return image.getBitmap(); }
public Manager mainScreen(Manager parent) { CInterface ci = new CInterface(); final int columnHeight0 = (int) ((Display.getHeight() / 2)); final int columnHeight1 = (int) (Display.getHeight() / 2); final int btnw = (int) ((float) Display.getWidth() * .75); final EncodedImage buttonoff = ci.getScaledImage(EncodedImage.getEncodedImageResource(button), btnw); final EncodedImage buttonon = ci.getScaledImage(EncodedImage.getEncodedImageResource(button_on), btnw); final EncodedImage friendsoff = ci.getScaledImage(EncodedImage.getEncodedImageResource(friends), btnw); final EncodedImage friendson = ci.getScaledImage(EncodedImage.getEncodedImageResource(friends_on), btnw); int imagew = (int) ((float) Display.getWidth() * .75); Bitmap blankbmp = Bitmap.getBitmapResource(blank); VerticalFieldManager fm_MainHolder = new VerticalFieldManager( Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.NO_HORIZONTAL_SCROLL | Manager.NO_VERTICAL_SCROLL) { public int getPreferredWidth() { return Display.getWidth(); } public int getPreferredHeight() { return Display.getHeight(); } protected void sublayout(int width, int height) { width = getPreferredWidth(); height = getPreferredHeight(); super.sublayout(width, height); super.setExtent(width, height); } }; Background back = bkg_main; fm_MainHolder.setBackground(back); // MAIN LOGO HorizontalFieldManager lgo = new HorizontalFieldManager( Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL | Manager.FIELD_HCENTER | Manager.FIELD_VCENTER | Manager.USE_ALL_WIDTH) { public int getPreferredHeight() { return columnHeight0; } protected void sublayout(int width, int height) { width = super.getPreferredWidth(); height = getPreferredHeight(); super.sublayout(width, height); super.setExtent(width, height); } }; BitmapField img = ci.WriteSimpleImage( lgo, logo, BitmapField.FIELD_VCENTER | BitmapField.FIELD_HCENTER, imagew, ""); fm_MainHolder.add(lgo); // BUTTON HOLDER VerticalFieldManager vfmButton = new VerticalFieldManager( Manager.FIELD_HCENTER | Manager.FIELD_VCENTER | Manager.USE_ALL_WIDTH) { public int getPreferredHeight() { return columnHeight1; } public int getPreferredWidth() { return Display.getWidth(); } protected void sublayout(int width, int height) { width = getPreferredWidth(); height = getPreferredHeight(); super.sublayout(width, height); super.setExtent(width, height); } }; final BitmapField bmFieldSpan = new BitmapField(blankbmp, BitmapField.NON_FOCUSABLE); CustomButtonField cbfFriends = new CustomButtonField( btnw, strings.getString("invite_friends"), friendsoff.getBitmap(), friendson.getBitmap(), Field.FOCUSABLE | Field.FIELD_HCENTER | Field.FIELD_VCENTER) { protected boolean navigationClick(int status, int time) { c.action_callback("web_friends", "", ""); return true; } }; CustomButtonField cbfButton = new CustomButtonField( btnw, strings.getString("read_qr"), buttonoff.getBitmap(), buttonon.getBitmap(), Field.FOCUSABLE | Field.FIELD_HCENTER | Field.FIELD_VCENTER) { protected boolean navigationClick(int status, int time) { c.action_callback("qr_start", "", ""); return true; } }; vfmButton.add(cbfButton); vfmButton.add(bmFieldSpan); vfmButton.add(cbfFriends); fm_MainHolder.add(vfmButton); parent.add(fm_MainHolder); return fm_MainHolder; }
public class ImageBrowserItemField extends Field { private static final String READING_LABEL = "Reading..."; private static final String ERROR_LABEL = "Error"; private static final int PADDING_LEFT = 10; private static final int PADDING_RIGHT = 12; private static final int PADDING_TOP = 10; private static final int PADDING_BOTTOM = 11; protected int paddingLeft, paddingRight, paddingTop, paddingBottom, totalPaddingX, totalPaddingY; private static final Bitmap BTN_STYLE_LEFT_CORNER = Bitmap.getBitmapResource("resources/images/btn_style/corner_left.png"); private static final Bitmap BTN_STYLE_RIGHT_CORNER = Bitmap.getBitmapResource("resources/images/btn_style/corner_right.png"); private static final EncodedImage BTN_STYLE_MIDDLE = EncodedImage.getEncodedImageResource("resources/images/btn_style/middle.png"); private int w; // Field's width private int h; // Field's height private boolean drawBtnStyle; private String imgUrl; private int desiredImgHeight, desiredImgWidth; private Bitmap imageBmp; private boolean isLoadingImage; private boolean errorLoadingImage; private String label; private int labelHeight, labelWidth; private int readingHeight, readingWidth; private int errorLoadingWidth, errorLoadingHeight; protected int labelToImageGap; /* package */ ImageBrowserItemField( IImageBrowserItemModel model, boolean isFocusable, boolean drawBtnStyle) { super(isFocusable ? Field.FOCUSABLE : Field.NON_FOCUSABLE); imgUrl = model.getImgUrl(); label = model.getLabel(); this.drawBtnStyle = drawBtnStyle; paddingLeft = PADDING_LEFT; paddingRight = PADDING_RIGHT; paddingTop = PADDING_TOP; paddingBottom = PADDING_BOTTOM; totalPaddingX = paddingRight + paddingLeft; totalPaddingY = paddingTop + paddingBottom; } /* package */ ImageBrowserItemField(IImageBrowserItemModel model) { this(model, true, true); } /* package */ void setDimention(int width, int height) { if (w != width || h != height) { // dimensions changed (probably screen was rotated), so reload imageBmp Bitmap imageBmp = null; isLoadingImage = false; } w = width; h = height; } public int getPreferredHeight() { return h; } public int getPreferredWidth() { return w; } protected void layout(int width, int height) { /*Logger.debug(this, "layout: entered for '" + label + "', width = " + width + ", height = " + height);*/ // ignore params, just use our custom w/h fields we've previously populated at setDimention() setExtent(w, h); } protected void drawFocus(Graphics gfx, boolean on) { // we don't call super.drawFocus(), completely overriding native behavior, // otherwise some devices (e.g. Storm) draw some native focus graphics // that looks a bit ugly for us if (on) { gfx.setBackgroundColor(0x005DE7); gfx.clear(); paint(gfx); } } protected void paintBackground(Graphics gfx) { if (drawBtnStyle) { gfx.setBackgroundColor(Color.BLACK); gfx.clear(); } super.paintBackground(gfx); } protected void paint(Graphics gfx) { // Logger.debug(this, "paint: entered for '" + label + '\''); final int fontHeight = gfx.getFont().getHeight(); if (label != null) { drawLabel(gfx, fontHeight); } if (imgUrl != null) { if (imageBmp != null) { drawBitmap(gfx); } else { if (errorLoadingImage) { drawError(gfx, fontHeight); } else { if (!isLoadingImage) { desiredImgHeight = h - labelToImageGap - labelHeight - totalPaddingY; desiredImgWidth = w - totalPaddingX; isLoadingImage = true; BitmapLoader.requestLoading(bitmapLoading); } if (isLoadingImage) { drawLoading(gfx, fontHeight); } } } } if (drawBtnStyle) { drawBtnStyle(gfx); } // Logger.debug(this, "paint: passed"); } protected boolean navigationClick(int status, int time) { fieldChangeNotify(0); return true; } protected boolean keyChar(char character, int status, int time) { if (character == Characters.ENTER) { fieldChangeNotify(0); return true; } return super.keyChar(character, status, time); } private void drawBtnStyle(Graphics gfx) { final int initialColor = gfx.getColor(); // draw border gfx.setColor(0x424242); gfx.drawLine(w - 1, 0, w - 1, h - 3); gfx.drawLine(0, h - 1, w - 1, h - 1); gfx.setColor(Color.BLACK); gfx.drawLine(w - 2, 0, w - 2, h - 2); gfx.drawLine(0, h - 2, w - 1, h - 2); gfx.setColor(initialColor); // corners are equal in size, so we can use just one value for both corners final int cornerWidth = BTN_STYLE_LEFT_CORNER.getWidth(); final int middleWidth = w - 2 * cornerWidth - 2; // height is equal for all three pieces final int height = BTN_STYLE_LEFT_CORNER.getHeight(); // draw left corner gfx.drawBitmap(0, 0, cornerWidth, height, BTN_STYLE_LEFT_CORNER, 0, 0); final EncodedImage middle = ImageUtils.resize(BTN_STYLE_MIDDLE, middleWidth + 1, height, false); // draw middle gfx.drawImage(cornerWidth, 0, middleWidth, height, middle, 0, 0, 0); // draw right corner gfx.drawBitmap(cornerWidth + middleWidth, 0, cornerWidth, height, BTN_STYLE_RIGHT_CORNER, 0, 0); } private void drawError(Graphics gfx, int fontHeight) { errorLoadingHeight = fontHeight; errorLoadingWidth = w - totalPaddingX; final int initialColor = gfx.getColor(); if (drawBtnStyle) { gfx.setColor(Graphics.WHITE); } gfx.drawText( ERROR_LABEL, paddingLeft, ((h - labelToImageGap - labelHeight - errorLoadingHeight - paddingBottom) >> 1), (DrawStyle.ELLIPSIS | DrawStyle.HCENTER), errorLoadingWidth); gfx.setColor(initialColor); } private void drawLoading(Graphics gfx, int fontHeight) { readingHeight = fontHeight; readingWidth = w - totalPaddingX; final int initialColor = gfx.getColor(); if (drawBtnStyle) { gfx.setColor(Graphics.WHITE); } gfx.drawText( READING_LABEL, paddingLeft, ((h - labelToImageGap - labelHeight - readingHeight - paddingBottom) >> 1), (DrawStyle.ELLIPSIS | DrawStyle.HCENTER), readingWidth); gfx.setColor(initialColor); } private void drawLabel(Graphics gfx, int fontHeight) { labelHeight = fontHeight; labelWidth = w - totalPaddingX; final int initialColor = gfx.getColor(); if (drawBtnStyle) { gfx.setColor(Graphics.WHITE); } gfx.drawText( label, paddingLeft, (h - labelHeight - paddingBottom), (DrawStyle.ELLIPSIS | DrawStyle.HCENTER), labelWidth); gfx.setColor(initialColor); } private void drawBitmap(Graphics gfx) { final int width = imageBmp.getWidth(); final int height = imageBmp.getHeight(); final int x = paddingLeft + ((desiredImgWidth - width) >> 1); final int y = paddingTop + ((desiredImgHeight - height) >> 1); gfx.drawBitmap(x, y, width, height, imageBmp, 0, 0); final int initialColor = gfx.getColor(); gfx.setColor(Color.GRAY); gfx.drawRect(x, y, width, height); gfx.setColor(initialColor); } private IBitmapLoading bitmapLoading = new IBitmapLoading() { public int getDesiredImgHeight() { return desiredImgHeight; } public int getDesiredImgWidth() { return desiredImgWidth; } public String getImgUrl() { return imgUrl; } public void setBitmap(final Bitmap b) { UiApplication.getUiApplication() .invokeLater( new Runnable() { public void run() { imageBmp = b; isLoadingImage = false; invalidate(); } }); } public void fail() { UiApplication.getUiApplication() .invokeLater( new Runnable() { public void run() { errorLoadingImage = true; isLoadingImage = false; invalidate(); } }); } }; }
/** * Retrieves a random predefined image * * @return The hard coded photo image */ public static EncodedImage getRandomPhotoImage() { String pictureName = "photo/" + PICTURES[_random.nextInt(PICTURES.length)]; return EncodedImage.getEncodedImageResource(pictureName); }