背景在GridView的项目滚动 [英] background scrolling with item in gridview

查看:89
本文介绍了背景在GridView的项目滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何实施的背景与gridview的滚动?如果这听起来含糊,我的意思是喜欢使用那里的货架形象附加到GridView的一个项目一个GridView实现一个书架。

How do I implement background scrolling with a gridview? If it sounds vague, I mean like implementing a bookshelf using a gridview where the shelf image is attached to an item in the gridview.

推荐答案

我花了永远摸不着头脑,所以每个人都试图做到这一点,这里是从我的电子书阅读器的code

It took me forever to figure this out, so for everybody trying to do this, here's the code from my e-book reader.

它基于货架由罗曼盖伊所以他应得的功劳原来code。

It's based on Shelves by Romain Guy so he deserves the credit for the original code.

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;
    }   

}

这篇关于背景在GridView的项目滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆