适合Android Imageview矩阵中心的屏幕

时间:2014-06-24 08:28:31

标签: android matrix imageview center

您好我正在使用一个适合屏幕的功能Android image view matrix scale + translate

float imageWidth = imageDetail.getDrawable().getIntrinsicWidth();
    float imageHeight = imageDetail.getDrawable().getIntrinsicHeight();
    RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
    RectF viewRect = new RectF(0, 0, imageDetail.getWidth(),
            imageDetail.getHeight());
    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    imageDetail.setImageMatrix(matrix);
    imageDetail.invalidate();

它有效但不是第一次。当我启动应用程序它不起作用。图像没有显示。当我点击它工作。我做错了什么?感谢您的任何建议

1 个答案:

答案 0 :(得分:2)

您好,您应该将代码放在onWindowFocusChanged方法中。

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    float imageWidth = image.getDrawable().getIntrinsicWidth();
    float imageHeight = image.getDrawable().getIntrinsicHeight();
    RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
    RectF viewRect = new RectF(0, 0, image.getWidth(),
            image.getHeight());
    Matrix matrix = new Matrix();
    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    image.setImageMatrix(matrix);
    image.invalidate();
}