将矩阵图像定位在imageview的中心

时间:2013-01-21 19:14:35

标签: android matrix imageview zoom

我创建了一个图像库,一切正常。要放大并移动图像

ImgView.setScaleType (ImageView.ScaleType.MATRIX)

当我这样做时,我得到的图像很小,所以我调用了方法

scaleFactor = view.getWidth()/(float)view.getDrawable().getIntrinsicWidth();
 matrix.setScale(scaleFactor, scaleFactor);

所以不要错过图像尺寸,但正如您所看到的,图像高于一切,需要保持在屏幕中央。

我试过这个

matrix.postTranslate((screen_width-image_width)/2, (screen_height-image_height)/2);

但不起作用。

有什么想法吗?很抱歉没有插入图片,但我不能因为我的声誉。 非常感谢您提前

2 个答案:

答案 0 :(得分:15)

好的,我已经纠正了错误,只需要输入以下几行代码:

RectF drawableRect = new RectF(0, 0, image_width, image_height);
RectF viewRect = new RectF(0, 0, screen_width, screen_height);
matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);

希望有人帮忙,非常感谢您的回答。

答案 1 :(得分:4)

@Override
public void onWindowFocusChanged(boolean hasFocus) {

    super.onWindowFocusChanged(hasFocus);

    screenWidth = layout.getWidth();
    screenHeight = layout.getHeight();

    Log.e("", "Image Width : " + imageWidth + " > " + imageHeight);
    Log.e("", "screen Width : " + screenWidth + " > " + screenHeight);

    RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight);
    RectF viewRect = new RectF(0, 0, screenWidth, screenHeight);
    matrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
    img.setImageMatrix(matrix);
}
相关问题