001.
package
com.myapp;
002.
003.
import
android.app.ActionBar.LayoutParams;
004.
import
android.app.Activity;
005.
import
android.os.Bundle;
006.
import
android.view.GestureDetector;
007.
import
android.view.GestureDetector.OnGestureListener;
008.
import
android.view.MotionEvent;
009.
import
android.view.View;
010.
import
android.view.animation.AlphaAnimation;
011.
import
android.view.animation.Animation;
012.
import
android.widget.ImageSwitcher;
013.
import
android.widget.ImageView;
014.
import
android.widget.TextView;
015.
import
android.widget.ViewSwitcher.ViewFactory;
016.
import
android.widget.Toast;
017.
018.
public
class
MainActivity
extends
Activity
implements
ViewFactory, OnGestureListener {
019.
020.
GestureDetector gd;
021.
TextView tv1;
022.
023.
private
Integer[] mImageIds = {
024.
R.drawable.pic_a,
025.
R.drawable.pic_b,
026.
R.drawable.pic_c,
027.
R.drawable.pic_d,
028.
R.drawable.pic_e,
029.
R.drawable.pic_f,
030.
R.drawable.pic_g,
031.
R.drawable.pic_h,
032.
R.drawable.pic_i
033.
};
034.
035.
private
static
final
int
SWIPE_MIN_DISTANCE =
120
;
036.
private
static
final
int
SWIPE_MAX_OFF_PATH =
250
;
037.
private
static
final
int
SWIPE_THRESHOLD_VELOCITY =
100
;
038.
int
position =
0
;
039.
int
i =
0
;
040.
ImageSwitcher iSwitcher;
041.
042.
@Override
043.
public
void
onCreate(Bundle savedInstanceState) {
044.
super
.onCreate(savedInstanceState);
045.
setContentView(R.layout.activity_main);
046.
047.
048.
049.
Animation inAnim =
new
AlphaAnimation(
0
,
1
);
050.
inAnim.setDuration(
1000
);
051.
Animation outAnim =
new
AlphaAnimation(
1
,
0
);
052.
outAnim.setDuration(
1000
);
053.
054.
055.
iSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
056.
iSwitcher.setFactory(
this
);
057.
iSwitcher.setInAnimation(inAnim);
058.
iSwitcher.setOutAnimation(outAnim);
059.
060.
iSwitcher.setImageResource(mImageIds[
0
]);
061.
062.
063.
gd =
new
GestureDetector(
this
,
this
);
064.
}
065.
066.
@Override
067.
public
boolean
onTouchEvent(MotionEvent event) {
068.
if
(gd.onTouchEvent(event))
069.
return
true
;
070.
else
071.
return
false
;
072.
}
073.
074.
public
boolean
onFling(MotionEvent e1, MotionEvent e2,
float
velocityX,
075.
float
velocityY) {
076.
077.
try
{
078.
if
(Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
079.
return
false
;
080.
081.
if
(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
082.
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
083.
setPositionNext();
084.
iSwitcher.setImageResource(mImageIds[position]);
085.
Toast.makeText(MainActivity.
this
, getResources().getResourceName(mImageIds[position]), Toast.LENGTH_SHORT).show();
086.
087.
}
else
if
(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
088.
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
089.
090.
setPositionPrev();
091.
iSwitcher.setImageResource(mImageIds[position]);
092.
Toast.makeText(MainActivity.
this
, getResources().getResourceName(mImageIds[position]), Toast.LENGTH_SHORT).show();
093.
}
094.
}
catch
(Exception e) {
095.
096.
return
true
;
097.
}
098.
099.
return
true
;
100.
}
101.
102.
public
void
setPositionNext()
103.
{
104.
position++;
105.
if
(position > mImageIds.length -
1
)
106.
{
107.
position =
0
;
108.
}
109.
}
110.
111.
public
void
setPositionPrev()
112.
{
113.
position--;
114.
if
(position <
0
)
115.
{
116.
position = mImageIds.length -
1
;
117.
}
118.
}
119.
120.
public
View makeView() {
121.
122.
ImageView iView =
new
ImageView(
this
);
123.
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
124.
iView.setLayoutParams(
new
125.
ImageSwitcher.LayoutParams(
126.
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
127.
iView.setBackgroundColor(
0xFF000000
);
128.
return
iView;
129.
}
130.
131.
public
boolean
onDown(MotionEvent arg0) {
132.
133.
return
false
;
134.
}
135.
136.
public
void
onLongPress(MotionEvent arg0) {
137.
138.
139.
}
140.
141.
public
boolean
onScroll(MotionEvent arg0, MotionEvent arg1,
float
arg2,
142.
float
arg3) {
143.
144.
return
false
;
145.
}
146.
147.
public
void
onShowPress(MotionEvent arg0) {
148.
149.
150.
}
151.
152.
public
boolean
onSingleTapUp(MotionEvent arg0) {
153.
154.
return
false
;
155.
}
156.
157.
}