黑莓在焦点时为图像添加边框
Blackberry add border to image when focus
我正在尝试制作可触摸图像,当我通过设备上的可触摸按钮滚动它时可以聚焦这是我的可触摸图像代码:
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 之前调整此位图的大小,正如您可能知道的那样,调整位图会删除透明部分并将其替换为黑色。我试过 class http://www.patchou.com/2010/10/resizing-transparent-bitmaps-with-the-blackberry-jde/ 来调整图像大小,但我的屏幕上有 25 张图像,而且效率很低。知道如何在图像上制作边框吗?在 drawFocus(Graphics g, boolean on)
方法中绘制边框的任何有效方法?
之所以只能聚焦图像的透明部分,是因为(据我所知)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);
}
}
我正在尝试制作可触摸图像,当我通过设备上的可触摸按钮滚动它时可以聚焦这是我的可触摸图像代码:
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 之前调整此位图的大小,正如您可能知道的那样,调整位图会删除透明部分并将其替换为黑色。我试过 class http://www.patchou.com/2010/10/resizing-transparent-bitmaps-with-the-blackberry-jde/ 来调整图像大小,但我的屏幕上有 25 张图像,而且效率很低。知道如何在图像上制作边框吗?在 drawFocus(Graphics g, boolean on)
方法中绘制边框的任何有效方法?
之所以只能聚焦图像的透明部分,是因为(据我所知)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);
}
}