在使用 Matrix.postTranslate(dx, dy)平移一下中心点

解决方案 »

  1.   

    兄弟我遇到这个问题,纠结半天终于搞定,现在把我实现的给打会儿参考参考
    class ImageState { 
    private float left; 
    private float top; 
    private float right; 
    private float bottom;

    public void getScaleImagePoint(ImageView imageView){
    Matrix matrix = imageView.getImageMatrix(); 
    Rect rect = imageView.getDrawable().getBounds(); 
    float[] values = new float[9]; 
    matrix.getValues(values); 
    setLeft(values[2]); 
    setTop(values[5]);
    setRight(getLeft() + rect.width() * values[0]); 
    setBottom(getTop() + rect.height() * values[0]);
    } public float getLeft() {
    return left;
    } public void setLeft(float left) {
    this.left = left;
    } public float getTop() {
    return top;
    } public void setTop(float top) {
    this.top = top;
    } public float getRight() {
    return right;
    } public void setRight(float right) {
    this.right = right;
    } public float getBottom() {
    return bottom;
    } public void setBottom(float bottom) {
    this.bottom = bottom;
    }
     }
    // 主要实现方式ImageState state = new ImageState();
    float percent = 30.0f;
    float centerX = iamgeView.getWidth()>>1;
    float centerY = iamgeView.getHeight()>>1;
    float scaleValue = percent / 250.0f + 1.0f; // 1.0f - 1.4f

    Matrix matrix = iamgeView.getImageMatrix();
    matrix.preTranslate(-centerX, -centerY);
    matrix.setScale(scaleValue, scaleValue);
    state.getScaleImagePoint(iamgeView);
    matrix.postTranslate(centerX-state.getRight()/2, centerY-state.getBottom()/2);
    iamgeView.setImageMatrix(matrix);
    postInvalidate();