Code example for Matrix

Methods: getValues, postTranslate, set, setValues

0
		    break; 
		case MotionEvent.ACTION_POINTER_UP:
			break; 
		case MotionEvent.ACTION_MOVE:
			if (mMode == DRAG) {
				mMatrix.set(mPreMatrix);
				mMatrix.postTranslate(event.getX() - mStartPoint.x, event.getY() - mStartPoint.y);
 
			} else if (mMode == ZOOM) {
				float postGap = pointGapSpace(event);
				if (postGap > 5f) {
					mMatrix.set(mPreMatrix);
					float scale = postGap / mPreGap;
					mMatrix.postScale(scale, scale, mCenterPoint.x, mCenterPoint.y);
				} 
			} 
			break; 
		} 
		updateMatrix(mMatrix);
		setImageMatrix(mMatrix);
		return super.onTouchEvent(event);
	} 
	 
	private void disallowInterceptTouch(){ 
        float[] values = new float[9];
        mCurMatrix.getValues(values);
        int scaleImageW = (int) (mImageWidth * values[Matrix.MSCALE_X]);
        int scaleImageH = (int) (mImageHeight * values[Matrix.MSCALE_Y]);
        if(scaleImageW > mViewWidth || scaleImageH > mViewHeight || mMode == ZOOM){
            getParent().requestDisallowInterceptTouchEvent(true);
        }else{ 
            getParent().requestDisallowInterceptTouchEvent(false);
        } 
	} 
 
	private float pointGapSpace(MotionEvent event) {
		float x = event.getX() - event.getX(event.getPointerCount()-1);
		float y = event.getY() - event.getY(event.getPointerCount()-1);
		return FloatMath.sqrt(x * x + y * y);
	} 
 
	private void centerPoint(PointF point, MotionEvent event) {
		float x = event.getX() + event.getX(event.getPointerCount()-1);
		float y = event.getY() + event.getY(event.getPointerCount()-1);
		point.set(x / 2, y / 2);
	} 
 
	private void updateMatrix(Matrix matrix) {
        float scaleViewW = mViewWidth / VIEW_SCALE;
        float scaleViewH = mViewHeight / VIEW_SCALE;
		float[] values = new float[9];
        float[] savedValue = new float[9];
 
		matrix.getValues(values);
		mCurMatrix.getValues(savedValue);
		int scaleImageW = (int) (mImageWidth * values[Matrix.MSCALE_X]);
		int scaleImageH = (int) (mImageHeight * values[Matrix.MSCALE_Y]);
		 
		if (values[Matrix.MTRANS_X] < mViewWidth - scaleImageW)
			values[Matrix.MTRANS_X] = mViewWidth - scaleImageW;
		if (values[Matrix.MTRANS_Y] < mViewHeight - scaleImageH)
			values[Matrix.MTRANS_Y] = mViewHeight - scaleImageH;
		if (values[Matrix.MTRANS_X] > 0)
			values[Matrix.MTRANS_X] = 0;
		if (values[Matrix.MTRANS_Y] > 0)
			values[Matrix.MTRANS_Y] = 0;
 
		if (values[Matrix.MSCALE_X] > MAX_SCALE || values[Matrix.MSCALE_Y] > MAX_SCALE) {
			values[Matrix.MSCALE_X] = (Float.isNaN(savedValue[Matrix.MSCALE_X])) ? MAX_SCALE : savedValue[Matrix.MSCALE_X];
			values[Matrix.MSCALE_Y] = (Float.isNaN(savedValue[Matrix.MSCALE_Y])) ? MAX_SCALE : savedValue[Matrix.MSCALE_Y];
			values[Matrix.MTRANS_X] = savedValue[Matrix.MTRANS_X];
			values[Matrix.MTRANS_Y] = savedValue[Matrix.MTRANS_Y];
		}else{ 
    		if (mImageWidth > mViewWidth || mImageHeight > mViewHeight) {
    			if (scaleImageW < scaleViewW && scaleImageH < scaleViewH) {
    				fixSizeFullScreen(values, VIEW_SCALE);
    			} 
    		} else { 
    			if (values[Matrix.MSCALE_X] < SMALL_IMAGE_MIM_SCALE)
    				values[Matrix.MSCALE_X] = SMALL_IMAGE_MIM_SCALE;
    			if (values[Matrix.MSCALE_Y] < SMALL_IMAGE_MIM_SCALE)
    				values[Matrix.MSCALE_Y] = SMALL_IMAGE_MIM_SCALE;
    		} 
		} 
        transImageCenter(values);
		matrix.setValues(values);
		mCurMatrix.set(matrix);
	} 
	 
	public void setVideoFlag(boolean videoFlag) {
        this.mVideoFlag = videoFlag;