背景滚动与gridview中的项目

时间:2011-07-18 14:40:11

标签: android android-layout android-gridview

如何使用gridview实现背景滚动?如果它听起来模糊,我的意思是使用gridview实现书架,其中货架图像附加到gridview中的项目。

3 个答案:

答案 0 :(得分: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;
    }   

}

答案 1 :(得分:1)

我所做的是在n gridview列中分割我的背景图像,在我的gridview getView方法中根据网格中的位置添加视图背景。 它运作得很好。

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

答案 2 :(得分:-1)

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

相关问题