【问题标题】:background scrolling with item in gridview背景滚动与gridview中的项目
【发布时间】:2011-07-18 14:40:11
【问题描述】:

如何使用 gridview 实现背景滚动?如果听起来含糊不清,我的意思是像使用网格视图实现书架,其中书架图像附加到网格视图中的项目。

【问题讨论】:

    标签: android android-layout android-gridview


    【解决方案1】:

    我花了很长时间才弄清楚这一点,所以对于所有试图这样做的人,这里是我的电子书阅读器的代码。

    它基于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;
        }   
    
    }
    

    【讨论】:

    • 在这个解决方案中,它认为货架是附加到单个项目上的。尝试在所有物品下方绘制没有任何间距的架子时,这可能会导致一些问题吗?或者当货架上只有一件商品时?
    • 不,它会根据您在其前面绘制的物品数量来绘制架子。它所做的只是以正确滚动的方式绘制背景。
    • 请告诉我下面的代码有什么用处: //这会在当前一个矩形上方绘制架子的顶部像素 Rect source = new Rect(0, mShelfHeight - top, mShelfWidth, mShelfHeight) ;矩形 dest = new Rect(x, 0, x + mShelfWidth, top ); canvas.drawBitmap(背景,源,目标,空);因为即使我删除它,视图也不会受到影响..
    • 如果没有该代码,当您滚动时,您有时会在屏幕顶部看到一小条空白区域,因为顶行从视图顶部下方的几个像素处开始,但是尚未显示之前的行。
    【解决方案2】:

    我所做的是将我的背景图像拆分为 n 个 gridview 列,并在我的 gridview getView 方法中根据网格中的位置添加视图背景。 效果很好。

    如果您想要代码,请询问。

    【讨论】:

    • layout.setBackground(context.getResources().getDrawable(R.drawable.shelf_panel));对于您正在讲述的项目或 gridview.setBackground(getResources().getDrawable(R.drawable.bookshelf_empty));可以分享代码吗??
    【解决方案3】:

    我之前的回答只添加了背景,但不让它随着项目滚动。你想要的是 NightWhistler 的回答 :) 抱歉误解了这个问题。

    【讨论】:

    • 我正在考虑使用它,但问题是要显示的项目数量。如果我只有 4 本书,每行只有 3 列,那么第二行会有 2 个项目没有背景,看起来很奇怪。
    • 我认为它不起作用,背景不会随着图像滚动
    • 我也尝试了上面的解决方案,我可以在 GridView 中的行之间绘制分隔符,但是当我滚动 gridView 时,只有项目项会滚动而不是分隔符。如何使用分隔符滚动项目listView.?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多