实现图片的从左到右或者从右到左的出场入场动画,看代码:
private ImageSwitcher mShowPicArea = null;
......
mShowPicArea.setOnTouchListener(new View.OnTouchListener() {
private float downX;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
break;
case MotionEvent.ACTION_UP:
int totalCount = getPictureCount();
if(event.getX()+60 < downX && selectPosition < totalCount-1) {
// to next image, the distance of gesture should be longer than 100
int selection = selectPosition+1;
updateSelectedImage(selection, TO_RIGHT);
}
else if(event.getX()-60 > downX && selectPosition > 0){
// to previous image
int selection = selectPosition - 1;
updateSelectedImage(selection, TO_LEFT);
}
break;
}
return true;
}
private void updateSelectedImage(int selection, boolean toRight) {
if(toRight) {
mShowPicArea.setInAnimation(AnimationUtils.loadAnimation(PictureSlideActivity.this,
android.R.anim.slide_in_left));
mShowPicArea.setOutAnimation(AnimationUtils.loadAnimation(PictureSlideActivity.this,
android.R.anim.slide_out_right));
} else {
mShowPicArea.setInAnimation(AnimationUtils.loadAnimation(PictureSlideActivity.this,
R.anim.slide_in_right));
mShowPicArea.setOutAnimation(AnimationUtils.loadAnimation(PictureSlideActivity.this,
R.anim.slide_out_left));
}
showImage(selection);
selectPosition = selection;
mPictureSlide.setSelection(selectPosition);
updateGallerySelection(selectPosition);
}
});
实现一个OnTouchListener,实现onTouch方法,捕获滑动的操作,当滑动超过60dip后触发图片切换动作