The Android system comes with a Gallery image browsing application that allows you to smoothly display images when dragging your fingers. This provides excellent user interaction and experience.
This example uses Gallery and custom View to simulate the Image Browsing effect of a Gallery image set. As follows:
1. Basic Principles
Implement the OnGestureListener interface onFling () gesture event in the Activity, and draw a draw () image through a custom View
2. Activity
In Activity, register myGesture. onTouchEvent (event) through onTouchEvent)
[Java]
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_UP:
FlingView. onFling (0); // After the finger is lifted, reset the sliding distance from offsetX to 0.
Break;
}
Return myGesture. onTouchEvent (event );
}
The onScroll () method of the OnGestureListener interface is implemented. The sliding parameter is passed to the handleScroll () member method of the FlingView inherited from the View to obtain the x-axis distance of the sliding.
[Java]
@ Override
Public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY ){
FlingView. handleScroll (-1 * (int) distanceX );
Return true;
}
Then, the OnFling () method of the OnGestureListener interface is implemented to pass the sliding parameter to the onFling () member method of the FlingView inherited from the View to get the gesture speed.
[Java]
@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY ){
FlingView. onFling (int)-velocityX );
Return true;
}
3. FlingView
In FlingView, get the gesture speed from Activity
[Java]
Public void onFling (int paramFloat1 ){
If (offsetX> GalleryDemoActivity. deviceScreenWidth/5 ){
If (fBitmap! = Null ){
IsFling = true;
IsFlingRight = true;
}
} Else if (offsetX <-GalleryDemoActivity. deviceScreenWidth/5 ){
If (nBitmap! = Null ){
IsFling = true;
IsFlingLeft = true;
}
}
// Start the animation effect
StartAnimation (new MyAnimation ());
}
During the Sliding Process, Draw an image by implementing the Draw () method of the View. Note: You need to Draw the current image (obtain the focus) and the next image (to obtain the focus) at the same time) two images in total
[Java]
@ Override
Public void draw (Canvas canvas ){
Paint paint = new Paint ();
Rect rect = new Rect ();
Canvas. drawColor (Color. BLACK );
// Draw the current image
If (bitmap! = Null ){
Int left = offsetX;
Int top = offsetY;
Int right = offsetX + GalleryDemoActivity. deviceScreenWidth;
Int bottom = offsetY + GalleryDemoActivity. deviceScreenHeight;
Rect. set (left, top, right, bottom );
Canvas. drawBitmap (bitmap, null, rect, paint );
}
// Draw the next image
If (offsetX <0) {// slide to the left
If (nBitmap! = Null ){
Int left = GalleryDemoActivity. deviceScreenWidth + 15 + offsetX;
Int top = 0;
Int right = left + GalleryDemoActivity. deviceScreenWidth;
Int bottom = GalleryDemoActivity. deviceScreenHeight;
Rect. set (left, top, right, bottom );
Canvas. drawBitmap (nBitmap, null, rect, paint );
}
} Else if (offsetX> 0) {// slide to the right
If (fBitmap! = Null ){
Int left =-GalleryDemoActivity. deviceScreenWidth-15 + offsetX;
Int top = 0;
Int right = left + GalleryDemoActivity. deviceScreenWidth;
Int bottom = GalleryDemoActivity. deviceScreenHeight;
Rect. set (left, top, right, bottom );
Canvas. drawBitmap (fBitmap, null, rect, paint );
}
}
}
After the sliding image ends, you need to perform a sliding animation to re-set the status of the current image and the previous and next image to prepare for the next slide.
[Java]
@ Override
Protected void onAnimationEnd (){
If (isFlingRight) {// slide to the right, position minus 1
NBitmap = bitmap;
Bitmap = fBitmap;
FBitmap = null;
Postion = postion-1;
} Else if (isFlingLeft) {// slide to the left, add 1 to position
FBitmap = bitmap;
Bitmap = nBitmap;
NBitmap = null;
Postion = postion + 1;
}
IsFlingRight = false;
IsFlingLeft = false;
IsFling = false;
OffsetX = 0;
If (fBitmap = null & offsetX = 0) {// if the previous image is empty (slide to the right), reset the previous image (position-1)
If (postion> 0 ){
FBitmap = getBitmap (postion-1 );
}
} Else if (nBitmap = null & offsetX = 0) {// if the next image is empty (sliding to the left), reset the next image (position + 1)
If (postion <bitmaps. length-1 ){
NBitmap = getBitmap (postion + 1 );
}
}
ClearAnimation ();
}