下面的代碼爲customButtonField具有兩個圖像。一個用於焦點圖像,另一個用於正常圖像。
更新按鈕圖像,你只需要調用setBitmap方法正常圖像。 您可以根據您的要求修改以下代碼。調用setBitmap方法後,您需要調用invalidate()方法。
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
class BitmapButtonField extends BitmapField
{
Bitmap mNormal;
Bitmap mFocused;
String text;
int mWidth;
int mHeight;
public Bitmap bitmap = null;
public BitmapButtonField(String text,Bitmap normal, Bitmap focused)
{
super(normal,FOCUSABLE);
mNormal = normal;
mFocused = focused;
mWidth = mNormal.getWidth();
mHeight = mNormal.getHeight();
this.text=text;
setMargin(0, 0, 0, 0);
setPadding(0, 0, 0, 0);
}
public void setBitmap(Bitmap bitmap)
{
mNormal=bitmap;
this.bitmap=bitmap;
}
public void setfocusBitmap(Bitmap bitmap)
{
mFocused=bitmap;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text=text;
}
protected void paint(Graphics graphics) {
Bitmap bitmap = mNormal;
if(isFocus())
{
bitmap = mFocused;
}
else
{
bitmap = mNormal;
}
graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
bitmap, 0, 0);
LabelField l=new LabelField(text);
graphics.drawText(text, bitmap.getWidth()/2-l.getPreferredWidth()/2+3, bitmap.getHeight()/2-l.getPreferredHeight()/2);
}
protected void drawFocus(Graphics graphics, boolean on) {
}
protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}
protected void onUnfocus() {
invalidate();
super.onUnfocus();
}
public int getPreferredWidth() {
return mWidth;
}
public int getPreferredHeight() {
return mHeight;
}
protected void layout(int width, int height) {
setExtent(mWidth, mHeight);
}
}
不....上面的代碼是關注焦點並且不重點關注customButtonField。圖片應該改變的時候會有單擊event.Plz幫助... –
您可以使用相同的影像對焦和無焦點的方式。您需要在按鈕單擊時調用setBitmap(new_image)和setfocusBitmap(new_image)。試試吧,這會很好地工作。 –
確定thanx您親切的幫助......其實我沒有注意到的setBitmap()函數,現在它很好地運行,可執行... –