admin
2021-07-28 c0269fcfa876b9c5cf309b2006462b4d09c5ef95
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.lcjian.library.widget;
 
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.ViewConfiguration;
import android.widget.GridView;
 
import com.lcjian.lcjianlibrary.R;
import com.lcjian.library.drawable.SpotlightDrawable;
import com.lcjian.library.drawable.TransitionDrawable;
 
public class ShelfView extends GridView {
    private Bitmap mShelfBackground;
    private int mShelfWidth;
    private int mShelfHeight;
 
    public ShelfView(Context context) {
        super(context);
        init(context);
    }
 
    public ShelfView(Context context, AttributeSet attrs) {
        super(context, attrs);
        load(context, attrs, 0);
        init(context);
    }
 
    public ShelfView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        load(context, attrs, defStyle);
        init(context);
    }
 
    private void load(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShelvesView, defStyle, 0);
 
        final Resources resources = getResources();
        final int background = a.getResourceId(R.styleable.ShelvesView_shelfBackground, 0);
        final Bitmap shelfBackground = BitmapFactory.decodeResource(resources, background);
        if (shelfBackground != null) {
            mShelfWidth = shelfBackground.getWidth();
            mShelfHeight = shelfBackground.getHeight();
            mShelfBackground = shelfBackground;
        }
        a.recycle();
    }
 
    private void init(Context context) {
        StateListDrawable drawable = new StateListDrawable();
 
        SpotlightDrawable start = new SpotlightDrawable(context, this);
        start.disableOffset();
        SpotlightDrawable end = new SpotlightDrawable(context, this, R.drawable.spotlight_blue);
        end.disableOffset();
        
        TransitionDrawable transition = new TransitionDrawable(start, end);
        drawable.addState(new int[] { android.R.attr.state_pressed }, transition);
 
        final SpotlightDrawable normal = new SpotlightDrawable(context, this);
        drawable.addState(new int[] { }, normal);
 
        normal.setParent(drawable);
        transition.setParent(drawable);
 
        setSelector(drawable);
        setDrawSelectorOnTop(false);
    }
 
    @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 = mShelfBackground;
 
        for (int x = 0; x < width; x += shelfWidth) {
            for (int y = top; y < height; y += shelfHeight) {
                canvas.drawBitmap(background, x, y, null);
            }
        }
 
        super.dispatchDraw(canvas);
    }
 
    
 
    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
 
        final Drawable current = getSelector().getCurrent();
        if (current instanceof TransitionDrawable) {
            if (pressed) {
                ((TransitionDrawable) current).startTransition(
                        ViewConfiguration.getLongPressTimeout());
            } else {
                ((TransitionDrawable) current).resetTransition();
            }
        }
    }
}