ImageView*腾挪

ImageView*移动

第一种:根据点击位置确定图片的左上角

 

@SuppressWarnings("deprecation")
public class MoveImage extends Activity {
	
	private ImageView mImageView01;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(
        		WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.move_image);
		mImageView01 = (ImageView) findViewById(R.id.imageView1);
		mImageView01.setImageResource(R.drawable.img1);
	}

	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			picMove(mImageView01, x, y);
			break;
		case MotionEvent.ACTION_MOVE:
			picMove(mImageView01, x, y);
			break;
		case MotionEvent.ACTION_UP:
			picMove(mImageView01, x, y);
			break;
		}
		return super.onTouchEvent(event);
	}
	
	
	private void picMove(ImageView image, float x, float y) {
		float positionX = x, positionY = y;
		
		image.setDrawingCacheEnabled(true);
		Bitmap bmp = Bitmap.createBitmap(image.getDrawingCache());
		image.setDrawingCacheEnabled(false);
		int intHeight = bmp.getHeight();
		int intWidth = bmp.getWidth();
		
		DisplayMetrics dm = new DisplayMetrics();
	    getWindowManager().getDefaultDisplay().getMetrics(dm); /* 取得屏幕解析像素 */
	    int intScreenX = dm.widthPixels;
	    int intScreenY = dm.heightPixels;
	    
	    if(x >= ( intScreenX - intWidth ) && y <= ( intScreenY - intHeight )){
	    	positionX = intScreenX - intWidth;
	    }
	    else if(x <= ( intScreenX - intWidth ) && y >= ( intScreenY - intHeight )){
	    	positionY = intScreenY - intHeight;
	    }
	    else if(x > ( intScreenX - intWidth ) && y > ( intScreenY - intHeight )){
	    	positionX = intScreenX - intWidth;
	    	positionY = intScreenY - intHeight;
	    }
	    
		image.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
				intHeight, (int) positionX, (int) positionY));
	}

}

 注意:layout的xml文件的imageView必须包含在AbsoluteLayout中


ImageView*腾挪

 

第二种:根据点击位置确定图片的中心位置

@SuppressWarnings("deprecation")
public class MoveImage2 extends Activity {
	
	private ImageView mImageView01;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(
        		WindowManager.LayoutParams.FLAG_FULLSCREEN,
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.move_image);
		mImageView01 = (ImageView) findViewById(R.id.imageView1);
		mImageView01.setImageResource(R.drawable.img1);
	}

	public boolean onTouchEvent(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			picMove(mImageView01, x, y);
			break;
		case MotionEvent.ACTION_MOVE:
			picMove(mImageView01, x, y);
			break;
		case MotionEvent.ACTION_UP:
			picMove(mImageView01, x, y);
			break;
		}
		return super.onTouchEvent(event);
	}
	
	
	private void picMove(ImageView image, float x, float y) {
		
		image.setDrawingCacheEnabled(true);
		Bitmap bmp = Bitmap.createBitmap(image.getDrawingCache());
		image.setDrawingCacheEnabled(false);
		int intHeight = bmp.getHeight();
		int intWidth = bmp.getWidth();
		
		float positionX = x - intWidth/2, positionY = y - intHeight/2;
		
		DisplayMetrics dm = new DisplayMetrics();
	    getWindowManager().getDefaultDisplay().getMetrics(dm); /* 取得屏幕解析像素 */
	    int intScreenX = dm.widthPixels;
	    int intScreenY = dm.heightPixels;
	    
	    if(x <= (intWidth / 2) && y <= (intHeight / 2)){
	    	positionX = 0;
	    	positionY = 0;
	    }
	    else if(x <= (intWidth / 2) && y >= (intScreenY - intHeight / 2)){
	    	positionX = 0;
	    	positionY = intScreenY - intHeight;
	    }
	    else if(x >= (intScreenX - intWidth / 2) &&  y <= (intHeight / 2)){
	    	positionX = intScreenX - intWidth;
	    	positionY = 0;
	    }
	    else if(x >= (intScreenX - intWidth / 2) &&  y >= (intScreenY - intHeight / 2)){
	    	positionX = intScreenX - intWidth;
	    	positionY = intScreenY - intHeight;
	    }
	    else if(x <= (intScreenX - intWidth / 2) && x >= (intWidth / 2) && y <= (intHeight / 2)){
	    	positionY = 0;
	    }
	    else if(x <= (intScreenX - intWidth / 2) && x >= (intWidth / 2) && y >= (intScreenY - intHeight / 2)){
	    	positionY = intScreenY - intHeight;
	    }
	    else if(x <= (intWidth / 2) && y <= (intScreenY - intHeight / 2) && y >= (intHeight / 2)){
	    	positionX = 0;
	    }
	    else if(x >= (intScreenX - intWidth / 2) && y <= (intScreenY - intHeight / 2) && y >= (intHeight / 2)){
	    	positionX = intScreenX - intWidth;
	    }
	    
		image.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
				intHeight, (int) positionX, (int) positionY));
	}

}
 


ImageView*腾挪