Android ImageView——图片切换

效果:一个ImageView,手势滑动切换图片,带左右切换效果
方法:使用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);
	}


注:非全部代码,提供参考




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值