如何使用GridView實現背景滾動?如果聽起來含糊不清,我的意思是像使用gridview實現一個書架,其中貨架圖像附加到gridview中的項目。使用GridView中的項目滾動背景
回答
我花了很長時間才弄清楚這一點,因此對於所有嘗試這樣做的人來說,下面是我電子書閱讀器的代碼。
它基於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;
}
}
我以前的回答只會添加背景,但不會讓它隨着項目滾動。你想要什麼是NightWhistler的回答:) 對不起,誤解了這個問題。
我正在考慮使用它,但問題是將顯示的項目數量。如果我只有4本書,並且每行的列數只有三個,那麼第二行將有兩個不會有背景的項目,使其看起來很奇怪。 – louieansonng
我不認爲它的效果,背景不滾動與圖像 – louieansonng
我也嘗試以上解決方案&我能夠繪製到GridView行之間的分隔符,但是當我滾動gridView時,只有項目項滾動不是分隔符。如何使用listView滾動分隔符。? – sachin003
我所做的是在n個gridview列和我的gridview中分割我的背景圖像getView方法根據網格中的位置添加視圖背景。 它工作完美。
如果你想要的代碼只是問。
layout.setBackground(context.getResources()。getDrawable(R.drawable.shelf_panel));您要告訴的項目或gridview.setBackground(getResources()。getDrawable(R.drawable.bookshelf_empty));是否有可能分享代碼? –
- 1. 滾動GridView並更改項目網格中的背景顏色
- 2. Gridview項目上滾動的背景資源
- 3. 如何製作Gridview滾動的背景
- 4. 設置每個gridView項目的背景?
- 5. 滾動背景
- 6. 滾動背景
- 7. 滾動背景
- 8. Android中的背景滾動
- 9. 用UICollectionView滾動UICollectionView背景
- 10. 禁用背景滾動
- 11. 滾動場景背景 - Unity
- 12. VirtualizingTilePanel背景滾動
- 13. 滾動背景,JAVA
- 14. Android滾動背景
- 15. 滾動背景邊
- 16. 滾動時背景?
- 17. XNA滾動背景
- 18. jquery - 滾動背景
- 19. 在ListView/GridView中按下的項目的默認背景顏色
- 20. 在ListView/GridView中按下的項目的默認背景顏色
- 21. listview項目的滾動背景顏色變化,
- 22. 列表可滾動的項目背景UL
- 23. 在gridview中更改背景顏色的項目觸摸
- 24. 使用Cocos2D和Box2D滾動背景
- 25. 使用CSS漸變背景滾動
- 26. 使用透明背景滾動文本
- 27. RecyclerView - 滾動時重複,當項目被觸摸,它改變項目的背景
- 28. 在Modal中滾動使得Safari上的背景滾動條
- 29. GridView項目不滾動裏面Expandable list
- 30. Android的OpenGLES滾動背景
在這個解決方案中,它認爲貨架連接到單個物品。這可能會導致一些問題,當試圖在沒有任何間距的情況下在所有物品下繪製貨架時?或者當在架子上放置單個物品時? –
不,它會獨立繪製與您前面繪製多少物品相關的架子。它所做的就是以適當滾動的方式繪製背景。 – NightWhistler
請告訴我什麼是下面的代碼的使用: //這將繪製在當前之上的架子的頂部像素 Rect source = new Rect(0,mShelfHeight - top,mShelfWidth,mShelfHeight); Rect dest = new Rect(x,0,x + mShelfWidth,top); canvas.drawBitmap(background,source,dest,null); 因爲即使我刪除它,視圖也沒有受到影響.. –