2011-07-18 18 views

回答

4

我花了很長時間才弄清楚這一點,因此對於所有嘗試這樣做的人來說,下面是我電子書閱讀器的代碼。

它基於Shelves by Romain Guy,所以他值得信任原代碼。

package net.nightwhistler.pageturner.view; 

import net.nightwhistler.pageturner.R; 
import net.nightwhistler.pageturner.library.LibraryBook; 
import net.nightwhistler.pageturner.library.QueryResult; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.view.KeyEvent; 
import android.widget.GridView; 

public class BookCaseView extends GridView { 

    private Bitmap background; 

    private int mShelfWidth; 
    private int mShelfHeight; 

    private QueryResult<LibraryBook> result;  

    private LibraryBook selectedBook; 

    public BookCaseView(Context context, AttributeSet attributes) { 
     super(context, attributes); 

     this.setFocusableInTouchMode(true); 
     this.setClickable(false); 

     final Bitmap shelfBackground = BitmapFactory.decodeResource(context.getResources(), 
       R.drawable.shelf_single); 
     setBackground(shelfBackground); 
     this.setFocusable(true); 
    } 

    public void setBackground(Bitmap background) { 
     this.background = background; 

     mShelfWidth = background.getWidth(); 
     mShelfHeight = background.getHeight(); 
    } 

    protected void onClick(int bookIndex) { 
     LibraryBook book = this.result.getItemAt(bookIndex); 

     this.selectedBook = book; 
     invalidate(); 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     final int count = getChildCount(); 
     final int top = count > 0 ? getChildAt(0).getTop() : 0; 
     final int shelfWidth = mShelfWidth; 
     final int shelfHeight = mShelfHeight; 
     final int width = getWidth(); 
     final int height = getHeight(); 
     final Bitmap background = this.background; 

     for (int x = 0; x < width; x += shelfWidth) { 
      for (int y = top; y < height; y += shelfHeight) { 
       canvas.drawBitmap(background, x, y, null); 
      } 

      //This draws the top pixels of the shelf above the current one 

      Rect source = new Rect(0, mShelfHeight - top, mShelfWidth, mShelfHeight); 
      Rect dest = new Rect(x, 0, x + mShelfWidth, top);    

      canvas.drawBitmap(background, source, dest, null);    
     }   


     super.dispatchDraw(canvas); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK && this.selectedBook != null) { 
      this.selectedBook = null; 
      invalidate(); 
      return true; 
     } 

     return false; 
    } 

} 
+0

在這個解決方案中,它認爲貨架連接到單個物品。這可能會導致一些問題,當試圖在沒有任何間距的情況下在所有物品下繪製貨架時?或者當在架子上放置單個物品時? –

+0

不,它會獨立繪製與您前面繪製多少物品相關的架子。它所做的就是以適當滾動的方式繪製背景。 – NightWhistler

+0

請告訴我什麼是下面的代碼的使用: //這將繪製在當前之上的架子的頂部像素 Rect source = new Rect(0,mShelfHeight - top,mShelfWidth,mShelfHeight); Rect dest = new Rect(x,0,x + mShelfWidth,top); canvas.drawBitmap(background,source,dest,null); 因爲即使我刪除它,視圖也沒有受到影響.. –

-1

我以前的回答只會添加背景,但不會讓它隨着項目滾動。你想要什麼是NightWhistler的回答:) 對不起,誤解了這個問題。

+0

我正在考慮使用它,但問題是將顯示的項目數量。如果我只有4本書,並且每行的列數只有三個,那麼第二行將有兩個不會有背景的項目,使其看起來很奇怪。 – louieansonng

+3

我不認爲它的效果,背景不滾動與圖像 – louieansonng

+0

我也嘗試以上解決方案&我能夠繪製到GridView行之間的分隔符,但是當我滾動gridView時,只有項目項滾動不是分隔符。如何使用listView滾動分隔符。? – sachin003

1

我所做的是在n個gridview列和我的gridview中分割我的背景圖像getView方法根據網格中的位置添加視圖背景。 它工作完美。

如果你想要的代碼只是問。

+0

layout.setBackground(context.getResources()。getDrawable(R.drawable.shelf_panel));您要告訴的項目或gridview.setBackground(getResources()。getDrawable(R.drawable.bookshelf_empty));是否有可能分享代碼? –