在ImageView中拖动图片

mainActivity如下:

package cn.c;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
/**
 * 需求描述:
 * 一个ImageView填充了整个屏幕,且其中显示的图片宽高均
 * 大于屏幕的宽高.现需要使用手指拖动图片,以便查看图片
 * 的任何部分.
 * 
 * 概况描述:
 * 1 在布局文件中ImageView的高宽是填充屏幕的
 * 2 截取的图片大小总是和屏幕的宽高相等!所以这也影响到了onScroll()
 *   中对于pointX和pointY的处理
 *   
 * 小结:
 * 1 注意GestureListenerImpl implements OnGestureListener
 *   实现的接口是GestureDetector.OnGestureListener
 *   而不是GestureOverlayView.OnGestureListener!!
 * 2 方法Bitmap.createBitmap(source, x, y, width, height)中
 *   pointX和pointY表示:从图片的哪个位置开始显示.
 *   不要错误地以为这是在屏幕上开始显示的开始坐标
 * 3 注意onTouchEvent()方法的处理!!!
 */
public class MainActivity extends Activity {
   private GestureListenerImpl mGestureListenerImpl;
   private GestureDetector mGestureDetector;
   private ImageView mImageView;
   private Bitmap mBitmap;
   private int screenWidth=0;
   private int screenHeight=0;
   private int bitmapWidth=0;
   private int bitmapHeight=0;
   private int pointX=0;
   private int pointY=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏状态栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        //得到屏幕的宽高
        Display display=getWindowManager().getDefaultDisplay();
        screenWidth=display.getWidth();
        screenHeight=display.getHeight();
        //得到Bitmap的宽高
        mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.photo);
        bitmapWidth=mBitmap.getWidth();
        bitmapHeight=mBitmap.getHeight();
        //设置ImageView
        mImageView=(ImageView) findViewById(R.id.imageView);
        //第一次时从原图的(0,0)截取图片
        Bitmap bitmap=Bitmap.createBitmap(mBitmap, pointX, pointY, screenWidth, screenHeight);
        mImageView.setImageBitmap(bitmap);
        //设置GestureDetector
        mGestureListenerImpl=new GestureListenerImpl();
        mGestureDetector=new GestureDetector(MainActivity.this,mGestureListenerImpl);
    }
    //当Activity的onTouchEvent()被触发时
    //触发GestureDetector的onTouchEvent()
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	    if (mGestureDetector.onTouchEvent(event)) {
				return mGestureDetector.onTouchEvent(event);
			} else {
				return super.onTouchEvent(event);
			}
    		
    	}
    
   private class GestureListenerImpl implements OnGestureListener{
		public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {
			// 计算拖动后X轴基准点(pointX)的位置
			// 注意:在多次拖动中,pointX是累加变化的
			if (pointX + distanceX >= 0) {
				if ((pointX+distanceX+screenWidth) > bitmapWidth) {
					//如果在某次移动后,移动距离为distanceX
					//此时pointX+distanceX再加上screenWidth大于了bitmapWidth
					//若此时从pointX + distanceX开始截宽度为screenWidth的图
					//那么很显然越界,超过了原bitmap的宽,所以报错.
					//所以只能从bitmapWidth-screenWidth处开始截宽为screenWidth的图
					//这样恰好不会越界:
					//即bitmapWidth-screenWidth+screenWidth=bitmapWidth
					pointX = bitmapWidth - screenWidth;
				} else {
					pointX = (int) (pointX + distanceX);
				}
			} else {
				//原图的原点
				pointX = 0;
			}
			// 计算拖动后Y轴基准点(pointY)的位置
			// 注意:在多次拖动中,pointY是累加变化的
			if (pointY + distanceY >= 0) {
				if ((pointY+distanceY+screenHeight) > bitmapHeight) {
					pointY = bitmapHeight - screenHeight;
				} else {
					pointY = (int) (pointY + distanceY);
				}
			} else {
				//原图的原点
				pointY = 0;
			}
			//重新显示重原图中截取的Bitmap
			//注意截取的图片大小总是和屏幕的宽高相等!
			if (distanceX != 0 && distanceY != 0) {
				Bitmap bitmap = Bitmap.createBitmap(mBitmap, pointX, pointY,screenWidth, screenHeight);
				mImageView.setImageBitmap(bitmap);
			}
			return false;
		}
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}

		public void onShowPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}

		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
			return false;
		}

		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			
		}

		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			// TODO Auto-generated method stub
			return false;
		}
	   
   }

}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/photo"
    />

</RelativeLayout>



 

你可能感兴趣的:(在ImageView中拖动图片)