Code example for Matrix

Methods: getValuespostTranslatesetsetScale

0
    /** 
     * 限制最大最小缩放比例,自动居中 
     */ 
    private void CheckView() { 
        float p[] = new float[9];
        matrix.getValues(p);
        if (mode == ZOOM) {
            if (p[0] < minScaleR) {
                matrix.setScale(minScaleR, minScaleR);
            } 
            if (p[0] > MAX_SCALE) {
                matrix.set(savedMatrix);
            } 
        } 
        center(); 
    } 
 
    /** 
     * 最小缩放比例,最大为100% 
     */ 
    private void minZoom() { 
//        minScaleR = Math.min( 
//                (float) dm.widthPixels / (float) bitmap.getWidth(), 
//                (float) dm.heightPixels / (float) bitmap.getHeight()); 
        if(bitmap.getWidth() >= dm.widthPixels)
        	minScaleR = ((float) dm.widthPixels) / bitmap.getWidth();
    	else 
    		minScaleR = 1.0f;
         
        if (minScaleR < 1.0) {
            matrix.postScale(minScaleR, minScaleR);
        } 
    } 
 
    private void center() { 
        center(true, true); 
    } 
 
    /** 
     * 横向、纵向居中 
     */ 
    protected void center(boolean horizontal, boolean vertical) {
        Matrix m = new Matrix();
        m.set(matrix);
        RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        m.mapRect(rect);
 
        float height = rect.height();
        float width = rect.width();
 
        float deltaX = 0, deltaY = 0;
 
        if (vertical) {
            // 图片小于屏幕大小,则居中显示。大于屏幕,上方留空则往上移,下方留空则往下移 
            int screenHeight = dm.heightPixels;
            if (height < screenHeight) {
                deltaY = (screenHeight - height) / 2 - rect.top;
            } else if (rect.top > 0) {
                deltaY = -rect.top;
            } else if (rect.bottom < screenHeight) {
                deltaY = imgView.getHeight() - rect.bottom;
            } 
        } 
 
        if (horizontal) {
            int screenWidth = dm.widthPixels;
            if (width < screenWidth) {
                deltaX = (screenWidth - width) / 2 - rect.left;
            } else if (rect.left > 0) {
                deltaX = -rect.left;
            } else if (rect.right < screenWidth) {
                deltaX = screenWidth - rect.right;
            } 
        } 
        matrix.postTranslate(deltaX, deltaY);
    } 
	 
	// 计算移动距离 
	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
Experience pair programming with AI  Get Codota for Java