图片放大缩小并且文字与图片相对位置不变

package com.android.activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable{
	private SurfaceHolder holder=null; //控制对象
	Point point ;
	Bitmap bitmap ;
	Matrix matrix;
	Matrix savedMatrix;
	public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		holder=getHolder();
		holder.addCallback(this);		
	}
	public MySurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.i);
		bitmapWidth = bitmap.getWidth();
		bitmapHeight = bitmap.getHeight();
		matrix = new Matrix();
		 matrix.setTranslate(0f, 0f);  
		savedMatrix = new Matrix();
		savedMatrix.setTranslate(0f, 0f);
		holder=getHolder();
		holder.addCallback(this);
	}
	float oldDistance,newDistance,oldAngle;	
	
	private final int DRAG = 1;
	private final int ZOOM = 2;
	private final int NONE = 3;
	private int flag;
	private final int flagCount = 5;
	private int action = NONE;
	Point startPoint = new Point();
	Point currentPoint = new Point();
	Point midPoint = new Point();
	Point pointMid;
	Paint paint = new Paint();
	private Point bitmapLeftTop,bitmapRightBottom;
	private float scale;
	private float fontScale = 1;
	private float fontTransX = 0;
	private float fontTransY = 0;
	float leftSide,topSide,fontX,fontY;
	private float bitmapWidth,bitmapHeight;
	Point textPoint;
	 private int screenWidth,screenHeight;
	

	//判断手势
	public boolean onTouchEvent(MotionEvent event) 
	{
		switch(event.getAction() & MotionEvent.ACTION_MASK)  
        {  
	        case MotionEvent.ACTION_DOWN:           // 第一个手指touch  
	        {
	        	startPoint.x = event.getX();
	        	startPoint.y = event.getY();
	        	flag =flagCount - 3;
	        	action = DRAG;
	        	break;
	        }
	        case MotionEvent.ACTION_POINTER_DOWN:   // 第二个手指touch  
	        {	       
	        	
	        	oldDistance = getDistance(event);   // 计算第二个手指touch时,两指之间的距离  
	        	if(oldDistance>10f){
	        		action = ZOOM;
		        	savedMatrix.set(matrix);
		        	midPoint = pointMid(event);
	        	}	        	
	        	break;
	        }
	        case MotionEvent.ACTION_UP:  
	        {
	        	action = NONE;	        
	        	break;
	        }
	        case MotionEvent.ACTION_POINTER_UP:  
	        {
	        	System.out.println("ACTION_POINTER_UP");
	        	flag = 0;
	        	action = DRAG;	        	
	        	break;
	        }
	        case MotionEvent.ACTION_MOVE:  	           
	        {	   
	        	System.out.println("move");
	        	if(action == DRAG){
	        		onDrag(event);
	        	}
	        	flag++;
	        	if(action == ZOOM){
	        		onZoom(event);
	        	}	    
	        }	
        }
		return true;
	}
	//拖动函数
	public void onDrag(MotionEvent event){
		
		matrix.set(savedMatrix);
		currentPoint.setXY(event.getX(), event.getY());
		
		if(flag > flagCount){
			matrix.postTranslate((float)(currentPoint.x-startPoint.x), (float)(currentPoint.y-startPoint.y));
			//累计的移动量
			fontTransX += (currentPoint.x-startPoint.x);
			fontTransY+=(currentPoint.y-startPoint.y);			
			//当前textPoint的值
			textPoint.setXY(textPoint.x+currentPoint.x-startPoint.x, textPoint.y+currentPoint.y-startPoint.y);
				
		}
		Canvas c=holder.lockCanvas();
		c.drawColor(Color.WHITE);	
		c.drawBitmap(bitmap, matrix, paint);
	
		paint.setTextSize(10*fontScale);
		c.drawText("hello", textPoint.x, textPoint.y, paint);
		
	    holder.unlockCanvasAndPost(c);
	    startPoint.setPoint(currentPoint);
		savedMatrix.set(matrix);
			
	}
	
	//缩放函数
	public void onZoom(MotionEvent event){
	
		 newDistance = getDistance(event);   // 计算第二个手指touch时,两指之间的距离  
		 if(newDistance>10f){
            matrix.set(savedMatrix);  
            scale = (oldDistance+((newDistance-oldDistance)/2)) / oldDistance;  
            matrix.postScale(scale, scale, midPoint.x, midPoint.y); 
            fontScale *=scale;
            //缩放的中心点和textPoint的距离,在一次缩放过程中保持不变
            float distance = getXYDistance(midPoint, textPoint);
            float sina = (textPoint.y-midPoint.y)/distance;
            float cosa = (textPoint.x-midPoint.x)/distance;
            distance*=scale;
            textPoint.x = distance*cosa+midPoint.x;
            textPoint.y = distance*sina+midPoint.y;
            oldDistance = newDistance;
        	Canvas c=holder.lockCanvas();
        	paint.setTextSize(10*fontScale);
    		c.drawColor(Color.WHITE);
    		c.drawBitmap(bitmap, matrix, paint);
            c.drawText("hello", textPoint.x, textPoint.y, paint);
    		holder.unlockCanvasAndPost(c);
    		savedMatrix.set(matrix);
            
            
            
            
       	/* float[] matrixValues = new float[9];  
		 
 		
         matrix.getValues(matrixValues);  
    
            oldDistance = newDistance;
         	Canvas c=holder.lockCanvas();
    		c.drawColor(Color.WHITE);
    		
    		fontScale *=scale;
    		
    		//左边界和上边界
    		leftSide = midPoint.x-fontScale*(midPoint.x - matrixValues[2]);
    		topSide = midPoint.y-fontScale*(midPoint.y - matrixValues[5]);
    		
    		bitmapHeight*=fontScale;
    		bitmapWidth*=fontScale;
    	
    		System.out.println("fontTransX"+fontTransX);
    	
    		paint.setTextSize(20*fontScale);
    	
    		//c.drawText("hello", 100, 100, paint);
    		c.drawBitmap(bitmap, matrix, paint);
    		
    	
             
         	float fontX = leftSide+bitmapWidth/3f;
    		float fontY = topSide+bitmapHeight/3f;
            
             System.out.println("fontX"+fontX+":::"+fontY);
    		c.drawText("hello", fontX, fontY, paint);
    		holder.unlockCanvasAndPost(c);
    		savedMatrix.set(matrix);*/
		 }			 		
		}
	public void onNone(MotionEvent event){
		System.out.println("NONE");
	}	
	
	 private float getDistance(MotionEvent event)  
     {  
         float x = event.getX(0) - event.getX(1);  
         float y = event.getY(0) - event.getY(1);  
         return FloatMath.sqrt(x * x + y * y);  
     }  
	 private Point pointMid(MotionEvent event)  
     {  
         pointMid = new Point();  
         pointMid.x = event.getX(0) + event.getX(1);  
         pointMid.y = event.getY(0) + event.getY(1);  
         pointMid.setXY(pointMid.x /2f, pointMid.y /2f);  
         return pointMid;  
     }  
	/* private float getDegree(MotionEvent event)  
     {  
         return (float)(Math.atan((event.getY(1) - event.getY(0)) / (event.getX(1) - event.getX(0))) * 180f);  
     }  	*/
	// 创建新的图片
		//Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);
	
	public float getXYDistance(Point p1,Point p2){
		float x = (p1.x-p2.x)*(p1.x-p2.x);
		
		float y = (p1.y-p2.y)*(p1.y-p2.y);
		return (float) Math.sqrt(x+y);
	}
	//接口函数实现
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		new Thread(this).start();		
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {	
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {		
	}


	//初始化线程
	@Override
	public void run() {		
		//获得屏幕参数
		
	/*	WindowManager windowManager = new Activity().getWindowManager();
	    Display display = windowManager.getDefaultDisplay();
	    screenWidth = display.getWidth();
	    screenHeight = display.getHeight();*/
	    bitmapLeftTop = new Point(150f,150f);
		textPoint = new Point(100+bitmapWidth/3f,100+bitmapHeight/3f);
		matrix.setTranslate(bitmapLeftTop.x, bitmapLeftTop.y);
		savedMatrix.set(matrix);
		Canvas c=holder.lockCanvas();
		c.drawColor(Color.WHITE);
		c.drawBitmap(bitmap, matrix, new Paint());
		
		
	/*	
		 float[] matrixValues = new float[9];  
		 
 		
         matrix.getValues(matrixValues);  
         for(int i=0;i<9;i++){
        	 System.out.println(matrixValues[i]);
         }
		*/
		
		c.drawText("hello", textPoint.x, textPoint.y, new Paint());
	    holder.unlockCanvasAndPost(c);	
	}
	//点对象
	class Point {
		private float x;
		private float y;
		public Point() {
			super();
		}
		
		public void setPoint(Point point){
			this.x = point.x;
			this.y = point.y;
		}

		public Point(float x, float y) {
			super();
			this.x = x;
			this.y = y;
		}

		public void setXY(float xx,float yy){
			x=xx;
			y=yy;
		}
		public float getX() {
			return x;
		}

		public void setX(float x) {
			this.x = x;
		}

		public float getY() {
			return y;
		}

		public void setY(float y) {
			this.y = y;
		}
	}
	
	}

你可能感兴趣的:(图片放大缩小并且文字与图片相对位置不变)