一、前言
老规矩,别的不说,这demo是找了很相关知识集合而成的,可以说对我这种小白来说是绞尽脑汁!程序员讲的是无图无真相!
现在大家一睹为快!
二、比较关键的还是scroller这个类的
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
package com.icq.slideview.view; import android.content.context; import android.util.attributeset; import android.util.log; import android.util.typedvalue; import android.view.motionevent; import android.view.view; import android.widget.linearlayout; import android.widget.relativelayout; import android.widget.scroller; import android.widget.textview; import com.icq.slideview.r; public class slideview extends linearlayout { private static final string tag = slideview; private context mcontext; private linearlayout mviewcontent; private relativelayout mholder; private scroller mscroller; private onslidelistener monslidelistener; private int mholderwidth = 120 ; private int mlastx = 0 ; private int mlasty = 0 ; private static final int tan = 2 ; public interface onslidelistener { public static final int slide_status_off = 0 ; public static final int slide_status_start_scroll = 1 ; public static final int slide_status_on = 2 ; /** * @param view current slideview * @param status slide_status_on or slide_status_off */ public void onslide(view view, int status); } public slideview(context context) { super (context); initview(); } public slideview(context context, attributeset attrs) { super (context, attrs); initview(); } private void initview() { mcontext = getcontext(); mscroller = new scroller(mcontext); setorientation(linearlayout.horizontal); view.inflate(mcontext, r.layout.slide_view_merge, this ); mviewcontent = (linearlayout) findviewbyid(r.id.view_content); mholderwidth = math.round(typedvalue.applydimension( typedvalue.complex_unit_dip, mholderwidth, getresources() .getdisplaymetrics())); } public void setbuttontext(charsequence text) { ((textview)findviewbyid(r.id.delete)).settext(text); } public void setcontentview(view view) { mviewcontent.addview(view); } public void setonslidelistener(onslidelistener onslidelistener) { monslidelistener = onslidelistener; } public void shrink() { if (getscrollx() != 0 ) { this .smoothscrollto( 0 , 0 ); } } public void onrequiretouchevent(motionevent event) { int x = ( int ) event.getx(); int y = ( int ) event.gety(); int scrollx = getscrollx(); log.d(tag, x= + x + y= + y); switch (event.getaction()) { case motionevent.action_down: { int deltax = x - mlastx; system.out.println(按下偏移+deltax); if (!mscroller.isfinished()) { mscroller.abortanimation(); } if (monslidelistener != null ) { monslidelistener.onslide( this , onslidelistener.slide_status_start_scroll); } break ; } case motionevent.action_move: { int deltax = x - mlastx; int deltay = y - mlasty; system.out.println(偏移+deltax); if (math.abs(deltax) < math.abs(deltay) * tan) { break ; } int newscrollx = scrollx - deltax; if (deltax != 0 ) { if (newscrollx < 0 ) { newscrollx = 0 ; } else if (newscrollx > mholderwidth) { newscrollx = mholderwidth; } this .scrollto(newscrollx, 0 ); } break ; } case motionevent.action_up: { int newscrollx = 0 ; if (scrollx - mholderwidth * 0.75 > 0 ) { newscrollx = mholderwidth; } this .smoothscrollto(newscrollx, 0 ); if (monslidelistener != null ) { monslidelistener.onslide( this , newscrollx == 0 ? onslidelistener.slide_status_off : onslidelistener.slide_status_on); } break ; } default : break ; } mlastx = x; mlasty = y; } private void smoothscrollto( int destx, int desty) { // 缓慢滚动到指定位置 int scrollx = getscrollx(); int delta = destx - scrollx; system.out.println(偏移scrollx+scrollx); system.out.println(偏移差delta+delta); int a= math.abs(delta) * 3 ; system.out.println(什么意思+a); mscroller.startscroll(scrollx, 0 , delta, 0 , math.abs(delta) * 3 ); invalidate(); } @override public void computescroll() { if (mscroller.computescrolloffset()) { system.out.println(当前x位置:+mscroller.getcurrx()); system.out.println(当前y位置:+mscroller.getcurry()); scrollto(mscroller.getcurrx(), mscroller.getcurry()); postinvalidate(); } } } |