我正在尝试制作可触摸的图像,当我通过设备上的可触摸按钮滚动它时,这是可触摸图像的代码:
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);
}
}
但问题是只有图像的透明部分可以聚焦,我在创建TouchBitmapField之前调整此Bitmap,因为您可能知道调整位图删除透明部分并将其替换为黑色。我尝试了这个类http://www.patchou.com/2010/10/resizing-transparent-bitmaps-with-the-blackberry-jde/来调整图像大小,但是我的屏幕上有25个图像,效率很低。知道如何在图像上制作边框吗?在drawFocus(Graphics g, boolean on)
方法中绘制边框的任何有效方法吗?
答案 0 :(得分:1)
您只能对图像的透明部分进行聚焦的原因是(据我所知)drawFocus
之前paint
被触发,导致最后绘制位图。
我认为这个解决方案是您正在寻找的。如果您想要纯色以外的其他内容作为边框,则可以使用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);
}
}
如果您希望边框与图片重叠,可以覆盖paint
方法并根据需要进行绘制。
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);
}
}