效果:一个ImageView,手势滑动切换图片,带左右切换效果
方法:使用2个ImageView,前面的遮住后面的,切换时,2个ImageView分别执行不同的动画。
布局文件
动画定义
定义View
手势
方法:使用2个ImageView,前面的遮住后面的,切换时,2个ImageView分别执行不同的动画。
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="5dip">
<TextView android:id="@+id/img_title" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" android:textSize="20dip"
android:visibility="gone" />
<ImageView android:id="@+id/note_img_pre" android:layout_width="wrap_content"
android:layout_height="196dip" android:layout_below="@id/img_title" />
<ImageView android:id="@+id/note_img" android:layout_width="wrap_content"
android:layout_height="196dip" android:layout_below="@id/img_title" />
<TextView android:id="@+id/img_des" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="15dip"
android:layout_below="@id/note_img" android:visibility="gone"
android:scrollbars="vertical" />
</RelativeLayout>
动画定义
private Animation leftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="500"/>
</set>
private Animation leftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="500" />
</set>
private Animation rightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
view plain
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="500"/>
</set>
private Animation rightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p"android:duration="500" />
</set>
定义View
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.img_dialog, (ViewGroup) this.findViewById(R.id.layout_root));
imageView = (ImageView) v.findViewById(R.id.note_img);
imageView_pre = (ImageView) v.findViewById(R.id.note_img_pre);
imageView.setOnTouchListener(new ImageTouchListener());
imageGesture = new GestureDetector(v.getContext(),new ImageGestureListener());
手势
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private int i=0;
private class ImageTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View view, MotionEvent e) {
return imageGesture.onTouchEvent(e);
}
}
private class ImageGestureListener extends
GestureDetector.SimpleOnGestureListener {
@Override
public void onShowPress(MotionEvent ev) {
super.onShowPress(ev);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if((i+1) < imgs.size()){
i++;
nextImg();
}else {
Toast.makeText(NoteInfoActivity.this, "这已经是最后一张", Toast.LENGTH_SHORT).show();
}
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
if((i-1)>=0){
i--;
preImg();
}else {
Toast.makeText(NoteInfoActivity.this, "这已经是第一张", Toast.LENGTH_SHORT).show();
}
}
return true;
}
}
图片切换方法
private void nextImg() {//下一张
imageView_pre.setImageBitmap(bitmap); imgUrl = FileUtil.getImgLocalPath(imgs.get(i).getMidImg()); bitmap = (getLoacalBitmap(imgUrl)); imageView.setImageBitmap(bitmap); imageView_pre.startAnimation(leftOut); imageView.startAnimation(leftIn); } private void preImg() {//上一张 imageView_pre.setImageBitmap(bitmap); imgUrl = FileUtil.getImgLocalPath(imgs.get(i).getMidImg()); bitmap = getLoacalBitmap(imgUrl); imageView.setImageBitmap(bitmap); imageView_pre.startAnimation(rightOut); imageView.startAnimation(rightIn); }
注:非全部代码,提供参考