Code example for Matrix

Methods: getValues, postScale, postTranslate, set, setValues

0
					oldPosX = event.getX();
					oldPosY = event.getY();
				} else if (touch_mode == ZOOM) {
					float newDistance = spacing(event);
					if (newDistance > 10f) {
						matrix.set(savedMatrix);
						scale = newDistance / oldDistance;
						matrix.postScale(scale, scale, 0, 0);
						scaleMid.x = middle.x * scale;
						scaleMid.y = middle.y * scale;
						newMid = midPoint(event);
						matrix.postTranslate(newMid.x - scaleMid.x, newMid.y - scaleMid.y);
					} 
					 
				} 
				 
				break; 
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_POINTER_UP:
				touch_mode = NONE;
				if(vx != 0 || vy != 0) invalidate();
				break; 
			} 
			setImageMatrix(matrix);
		} 
		 
		return true; 
	} 
	 
	//双击 
	public void performDoubleClickEffect(float x, float y){
		runnable = new MyRunnable(x, y);
		handler.post(runnable);
	} 
	 
	public void stopDoubleClickEffectPerformance(){ 
		handler.removeCallbacks(runnable);
	} 
	 
	//初始化缩放 
	public void setOriginLayout(int width, int height){
		this.originWidth = width;
		this.originHeight = height;
		 
		initImage(); 
	} 
	 
	private void initImage(){ 
		if(((mode & PLAY_GIF) == PLAY_GIF) && gifDecoder != null && gifDecoder.parseOk()){
			for(int i = 0; i < gifDecoder.getFrameCount(); i++){
				animationDrawable.addFrame(new BitmapDrawable(getResources(), gifDecoder.getFrameImage(i)), gifDecoder.getDelay(i));
			} 
			setImageDrawable(animationDrawable);
			animationDrawable.start();
			animationDrawable.setOneShot(false);
		} 
		matrix = new Matrix();
		 
		if(originHeight > height && width / originWidth * originHeight > height){
			scale = (float) width / (float) originWidth;
			matrix.postScale(scale, scale, 0, 0);
			matrix.postTranslate((width - (originWidth * scale)) / 2, 0);
		}else{ 
			if(width / originWidth * originHeight < height){
				scale = (float) width / (float) originWidth;
			}else{ 
				scale = (float) height / (float) originHeight;
			} 
			matrix.postScale(scale, scale, 0, 0);
			matrix.postTranslate((width - (originWidth * scale)) / 2, (height - (originHeight * scale)) / 2);
		} 
		originScale = scale;
		 
		setImageMatrix(matrix);
	} 
	 
	//重写setImageMatrix以规定图片缩放和移动的范围 
	@Override 
	public void setImageMatrix(Matrix matrix){
		matrix.getValues(mMatrix);
		 
		if(mMatrix[0] < originScale || mMatrix[4] < originScale) mMatrix[0] = mMatrix[4] = originScale;
		 
		if(mMatrix[0] * originWidth >= width){
			if(mMatrix[2] > 0) mMatrix[2] = 0;
			if(-mMatrix[2] > (mMatrix[0] * originWidth) - width) mMatrix[2] = -((mMatrix[0] * originWidth) - width);
		}else{ 
			if(mMatrix[2] < 0) mMatrix[2] = 0;
			if(-mMatrix[2] < (mMatrix[0] * originWidth) - width) mMatrix[2] = -((mMatrix[0] * originWidth) - width);
		} 
		 
		if(mMatrix[4] * originHeight >= height){
			if(mMatrix[5] > notificationBarHeight) mMatrix[5] = notificationBarHeight;
			if(-mMatrix[5] > (mMatrix[0] * originHeight) - height) mMatrix[5] = -((mMatrix[0] * originHeight) - height);
		}else{ 
			if(mMatrix[5] < notificationBarHeight) mMatrix[5] = notificationBarHeight;
			if(-mMatrix[5] < (mMatrix[0] * originHeight) - height) mMatrix[5] = -((mMatrix[0] * originHeight) - height);
		} 
		 
		matrix.setValues(mMatrix);
		 
		super.setImageMatrix(matrix);
		 
	}