2012-06-13 34 views
1

我一直有想實現對黑莓的圖片庫6上無焦點的方式和滾動(黑莓)定製BitmapField錯誤

一切正常時,這個惱人的問題,然而,當從頂部按鈕焦點更改的說法進一步在屏幕下方顯示圖像,圖像似乎有故障並且不能正確繪製自己。請看下面的圖片爲例:

(重點是屏幕(未顯示)的頂部) initial screen

(現在的重點是左下角的圖像上,注意最上面的圖片是現在空白,原因不明)

after scroll screen

而這種情況無論有多少照片,我添加到TUMBNAIL畫廊。

現在,這裏是我的代碼,(它關於縮略圖的繪圖部分)

public ProductImage(String productName){ 
    super(VERTICAL_SCROLL|VERTICAL_SCROLLBAR); 

    currentProduct = productName; 

    createGUI(); 
} 
public void createGUI(){ 
    deleteAll(); 
    try{ 
     Storage.loadPicture(); 
    }catch(NullPointerException e){ 
     e.printStackTrace(); 
    } 
    this.setTitle(new LabelField(_resources.getString(PRODUCT_IMAGE), Field.FIELD_HCENTER)); 
    if(ToolbarManager.isToolbarSupported()) 
    { 
     Toolbar tb = new Toolbar(); 
     setToolbar(tb.createToolBar()); 
    } 
    else{ 
     Toolbar tb = new Toolbar(); 
     add(tb.createNavBar()); 
    } 

    picVector = Storage.getPicture(currentProduct); 

    EncodedImage enc = EncodedImage.getEncodedImageResource("camera.png"); 
    EncodedImage sizeEnc = ImageResizer.sizeImage(enc, Display.getHeight(), Display.getHeight()); 

    takenPicture = new BitmapField(enc.getBitmap()); 

    vfMain = new VerticalFieldManager(); 
    vfMain.add(logo); 
    vfMain.add(new SeparatorField()); 
    add(vfMain); 
    prepareBmpFields(); 
} 

private void prepareBmpFields() { 

     System.out.println("This is the vector size: " + picVector.getPicVector().size()); 

     LayoutManager manager = new LayoutManager(); 


     FieldChangeListener itemListener = new ButtonListener(); 


     mBmpFields = new ImageButtonField[picVector.getPicVector().size()]; 

     for (int i = 0; i < picVector.getPicVector().size(); i++) { 
       /*EncodedImage image = EncodedImage 
           .getEncodedImageResource((String)imageVector.elementAt(i));*/ 
      byte[] data = getData((String)picVector.getPicVector().elementAt(i)); 
      //Encode and Resize image 
      EncodedImage eImage = EncodedImage.createEncodedImage(data,0,data.length); 

      eImage = ImageResizer.resizeImage(eImage, mImgWidth, mImgHeight); 
      ImageButtonField currentImage = new ImageButtonField(eImage.getBitmap()); 
      currentImage.setAssociatedPath((String)picVector.getPicVector().elementAt(i)); 
      mBmpFields[i] = currentImage; 
      mBmpFields[i].setChangeListener(itemListener); 
      manager.add(mBmpFields[i]); 

     } 
     vfMain.add(manager); 
    } 

    private class LayoutManager extends VerticalFieldManager { 
     public LayoutManager() { 
      super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR); 
     } 

     protected void sublayout(int width, int height) { 
      int columns = mScrWidth/(mImgWidth + 2 * mImgMargin); 

      int scrWidth = Display.getWidth(); 

       int rows = mBmpFields.length/columns 
       + (mBmpFields.length % columns > 0 ? 1 : 0); 
       int counter = 0; 
       for (int i = 0; i < rows; i++) { 
       for (int j = 0; j < columns; j++) { 
       int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin; 
       int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin; 

       if(mBmpFields.length > counter){ 
        Field field = mBmpFields[counter]; 
        layoutChild(field, mImgWidth, mImgHeight); 
        setPositionChild(field, posX, posY); 
        counter++; 
       }; 
       } 
      } 
       if(Display.getWidth() < Display.getHeight()){ 
       setExtent(mScrWidth, (int)(mScrHeight*1.25)); 
       } 
       else{ 
        setExtent(mScrWidth, (int)(mScrHeight*2)); 
       } 
     } 


     public int getPreferredWidth() { 
      return mScrWidth; 
     } 
     public int getPreferredHeight() { 
      return mScrHeight; 
     } 
    } 
} 

我已經刪除了代碼的許多不相關的部分,但所需要的代碼是存在的。

有誰知道什麼可能導致這個問題?謝謝你的幫助!

編輯:根據要求,這裏是我的執行ImageButtonField類:

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.system.Characters; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.component.BitmapField; 

public class ImageButtonField extends BitmapField{ 

String associatedPath =""; 
BitmapField image2; 

public ImageButtonField(Bitmap image) { 
    super(image); 
} 
public void setAssociatedPath(String path){ 
    associatedPath = path; 
} 
public String getAssociatedPath(){ 
    return associatedPath; 
} 
public boolean isFocusable() { 
    return true; 
} 
protected void applyTheme(Graphics arg0, boolean arg1) { 
} 

protected void drawFocus(Graphics graphics, boolean on) { 
} 
protected void onFocus(int direction) { 
     // only change appearance if this button is enabled (aka editable) 
     if (isEditable()) { 
     invalidate(); // repaint 
     } 
     super.onFocus(direction); 
    } 

    public void onUnfocus() { 
     invalidate(); // repaint 
     super.onUnfocus(); 
    } 
protected boolean navigationClick(int status, int time) { 
    fieldChangeNotify(0); 
    return true; 
} 

protected boolean trackwheelClick(int status, int time) { 
    fieldChangeNotify(0); 
    return true; 
} 

protected void paint(Graphics graphics) { 
    super.paint(graphics); 
    if (isFocus()) { 
     graphics.setGlobalAlpha(128); 
     graphics.setColor(0x888888); 
     graphics.fillRect(0, 0, getWidth(), getHeight()); 
    }else{ 
     graphics.setGlobalAlpha(0); 
     graphics.setColor(0x000000); 
     graphics.fillRect(0, 0, getWidth(), getHeight()); 
     //graphics.drawBitmap(0, 0, getWidth(), getHeight(), image2.getB, 0, 0); 
    } 
} 

protected boolean keyChar(char character, int status, int time) { 
    if(Characters.ENTER == character || Characters.SPACE == character) { 
     fieldChangeNotify(0); 
     return true; 
    } 
    return super.keyChar(character, status, time); 
} 

}

+0

只是想讓你知道我可以重現這個問題。我正在看它... – Nate

回答

1

好的,你可以不理我的第一個答案,但是因爲我當時沒有你的ImageButtonField代碼,所以我不想把它扔掉......也許別人會覺得它有用。

最後,我不需要對ImageButtonField做任何更改,但我確實更改了LayoutManager類。我發現問題所在的方式是我開始用內置替換你的自定義UI類。我用BitmapField替換ImageButtonField。這並沒有解決它。然後,我用FlowFieldManager替換了LayoutManager,並修復了它。所以,我知道問題出在哪裏。

我的解決辦法:

private class LayoutManager extends Manager { 
    public LayoutManager() { 
    super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR); 
    } 

    protected void sublayout(int width, int height) { 
    setExtent(width, height); 
    // TODO: maybe always set the same virtual extent? 
    if (Display.getWidth() < Display.getHeight()) { 
     setVirtualExtent(mScrWidth, (int) (mScrHeight * 1.25)); 
    } else { 
     setVirtualExtent(mScrWidth, (int) (mScrHeight * 2)); 
    } 

    int columns = mScrWidth/(mImgWidth + 2 * mImgMargin); 
    // int scrWidth = Display.getWidth(); 

    int rows = mBmpFields.length/columns + (mBmpFields.length % columns > 0 ? 1 : 0); 
    int counter = 0; 
    for (int i = 0; i < rows; i++) { 
     for (int j = 0; j < columns; j++) { 
      int posX = j * (mImgWidth + 2 * mImgMargin) + mImgMargin; 
      int posY = i * (mImgHeight + 2 * mImgMargin) + mImgMargin; 

      if (mBmpFields.length > counter) { 
       Field field = mBmpFields[counter]; 
       layoutChild(field, mImgWidth, mImgHeight); 
       setPositionChild(field, posX, posY); 
       counter++; 
      } 
     } 
    } 
    } 

    public int getPreferredWidth() { 
    return mScrWidth; 
    } 

    public int getPreferredHeight() { 
    return mScrHeight; 
    } 
} 

我不能肯定地說,我明白了爲什麼你原來的代碼是行不通的,但我可以說,我也不會做在少數人的事原代碼:

  1. 的原代碼擴展VerticalFieldManager但做所有的工作本身,在sublayout()。所以,我認爲沒有任何意見延伸VerticalFieldManager。我改變它只是延長Manager

  2. 原始代碼被稱爲setExtent()不同大小。我不認爲這就是你想要的。 程度Field的實際尺寸。 虛擬範圍是虛擬尺寸,爲了啓用滾動,您希望設置的尺寸大於實際範圍。您不需要動態計算不同的範圍肖像與風景,因爲widthheight參數傳遞到sublayout()將已經反映了這一點。我不確定你甚至需要設置不同的虛擬盤區。我想你大概應該是總是設置的虛擬盤區高度爲圖片高度的行數,佔邊距。

  3. 在原始代碼中有一個未使用的變量scrWidth。我在上面評論過。

+0

這樣做的工作,現在工作得很好!感謝您付出的努力,懷疑我會自己想出來的! –

+0

@BananaMan,不用擔心,夥伴。我知道傳統的BlackBerry UI類可能會很棘手,但我承認我不得不求助於簡單的試驗和錯誤,以便縮小問題出在哪裏。祝你好運! – Nate

0

posted this question recently,對不對?我是否正確地認爲你在這裏提到的ImageButtonField與你在另一個問題中所做的一樣?

我看不到您的ImageButtonField的完整實現,您應該在這裏發佈。然而,看看你的其他問題的答案,我有一種感覺,你在ImageButtonField做一些自定義的焦點處理,也許它沒有做得很對。無論如何,這個類可能是問題所在。

我有一個類似Field子我自己的,這裏都是重點處理方法我定義:

public class CustomButtonField extends Field { 

    private Bitmap _button; // the currently displayed button image 
    private Bitmap _on;  // image for 'on' state (aka in-focus) 
    private Bitmap _off;  // image for 'off' state (aka out-of-focus) 

    protected void onFocus(int direction) { 
     // only change appearance if this button is enabled (aka editable) 
     if (isEditable()) { 
     _button = _on; 
     invalidate(); // repaint 
     } 
     super.onFocus(direction); 
    } 

    protected void onUnfocus() { 
     _button = _off; 
     invalidate(); // repaint 
     super.onUnfocus(); 
    } 

    protected void drawFocus(Graphics graphics, boolean on) { 
     // override superclass implementation and do nothing 
    } 

    public boolean isFocusable() { 
     return true; 
    } 

我也有paint()自定義實現。我不會讓這一切都在這裏,因爲有很多的代碼可能有無關,與你的問題,但我paint()不包括這一呼籲:

graphics.drawBitmap(_padding, _padding, _fieldWidth, _fieldHeight, _button, 0, 0); 

你可能不關心的事實,我有獨立的用於聚焦和未聚焦狀態的圖像......也許您始終顯示相同的圖像。

但是,可能要檢查的是你的onFocus()onUnfocus()方法。我可能需要添加invalidate()的電話。

看Rupak對你的其他問題的答案,它也將是很好的檢查你的ImageButtonField.paint()方法,並確保你不忽略做重要的繪畫步驟,如果該領域不重點。

+0

我會在我的ImageButtonField類中嘗試一些事情,如果沒有任何工作,我會發布我原來的ImageButtonField類。感謝您的迴應,很快就會更新! –

+0

嗯,我已經嘗試了一些東西,但沒有一個我的修改似乎解決了這個問題。我已經將我的ImageButtonField實現添加到問題中。 –

+0

@BananaMan謝謝。我今晚會用你的ImageButtonField代碼再試一次。 – Nate