woong's

GridView row background & 책장 효과 & 배경이미지 움직이게 하기 본문

Develop/Android

GridView row background & 책장 효과 & 배경이미지 움직이게 하기

dlsdnd345 2016. 2. 14. 16:07
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

GridView row background & 책장 효과 & 배경이미지 움직이게 하기


심심풀이로 안드로이드 도서 관리 앱만들고 있습니다 .

아이폰 어플이 퀄리티가 좋은것 같네요 ㅜ.

안드로이드 개발자로써 분발해야 될것 같습니다 ^^


아이폰 도서관리 어플 퀄리티 높은 것들이 많은데 안드로이드는 없는것 같아

만들어보려구 시작했습니다 . 완성되면 이용해주세요 ~

어느정도 퀄리티가 나올지는 모르겠는데 ... 도전해봐야죠 ^^


도서 관리 앱 만들면서 GridView 를 사용했는데 Background 가 같이 움직이지 

않았습니다 . 하지만 아이폰에서는 움직이죠 ;;; 그래서 

찾다 보니 소스를 제공해주신분의 소스를 사용하여 배경 이미지를 같이 움직일수

있도록 하였습니다 .

 



 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.iw.activity;
 
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.widget.GridView;
 
public class BookCaseView extends GridView {
 
    private Bitmap background;
 
    private int mShelfWidth;
    private int mShelfHeight;
 
    public BookCaseView(Context context, AttributeSet attributes) {
        super(context, attributes);
 
        this.setFocusableInTouchMode(true);
        this.setClickable(false);
 
        final Bitmap shelfBackground = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.test);
        setBackground(shelfBackground);
        this.setFocusable(true);
    }
 
    public void setBackground(Bitmap background) {
        this.background = background;
 
        mShelfWidth = background.getWidth();
        mShelfHeight = background.getHeight();
    }
 
    protected void onClick(int bookIndex) {
        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);
    }
 
}

 

 

코드를 제공해 주신분에게 감사를 표합니다.


final Bitmap shelfBackground = BitmapFactory.decodeResource(context.getResources(),

 R.drawable.test);

위코드에서 이미지를 변경하는 부분이 되겠습니다.

해당 row 수를 결정하기 위해서 이미지 크기를 변경해야 합니다


BookCaseView gridView gridView = (BookCaseView) findViewById(R.id.grid_view);


이렇게 해서 GridView 를 상속한 CustomView 하나를 생성하고 액티비티에서 GridView를

대체 해주시면 움직이는 배경을 만들수 있습니다.


Comments