Android image view matrix scale + translate

后端 未结 4 1973
执笔经年
执笔经年 2020-11-28 04:52

I am trying to manually get an image inside an imageview centered and fitting the screen. I need to do it with a matrix (I will later dynamically change the matrix transform

4条回答
  •  自闭症患者
    2020-11-28 05:30

    Here is how I solved my problem using matrices (requested by joakime in the other answer):

    private void setImageTransformation(float tx, float ty, float scale) {
        savedMatrix.reset();
        savedMatrix.postTranslate(-imageWidth / 2f, -imageHeight / 2f);
        savedMatrix.postScale(scale, scale);
        savedMatrix.postTranslate(tx, ty);
        imageView.setImageMatrix(savedMatrix);
    }
    
    public void resetImageMatrix() {
        if (!isImageLoaded()) return;
    
        imageWidth = imageView.getDrawable().getIntrinsicWidth();
        imageHeight = imageView.getDrawable().getIntrinsicHeight();
    
        float scaleX = (float) displayWidth / (float) imageWidth;
        float scaleY = (float) displayHeight / (float) imageHeight;
        minScale = Math.min(scaleX, scaleY);
        maxScale = 2.5f * minScale;
    
        initialTranslation.set(
                  Math.max(0, 
                    minScale * imageWidth / 2f 
                    + 0.5f * (displayWidth - (minScale * imageWidth))), 
                  Math.max(0, 
                    minScale * imageHeight / 2f
                    + 0.5f * (displayHeight - (minScale * imageHeight))));
    
        currentScale = minScale;
        currentTranslation.set(initialTranslation);
        initialImageRect.set(0, 0, imageWidth, imageHeight);
    
        setImageTransformation(initialTranslation.x, initialTranslation.y, 
                    minScale);
    }
    

    I am cheating here a bit because the pinch is not really centered between the user fingers, which is acceptable in my case.

提交回复
热议问题