在Custom ImageView中将图像对齐在顶部

时间:2016-09-04 01:33:11

标签: android imageview

我正在使用TouchImageView中的ViewPager将图片加载到我的应用中。这是负责加载图像的TouchImageView.java

float redundantXSpace = viewWidth - (scaleX * drawableWidth);
        float redundantYSpace = viewHeight - (scaleY * drawableHeight);
        matchViewWidth = viewWidth - redundantXSpace;
        matchViewHeight = viewHeight - redundantYSpace;
        if (!isZoomed() && !imageRenderedAtLeastOnce) {
            //
            // Stretch and center image to fit view
            //
            matrix.setScale(scaleX, scaleY);
            matrix.postTranslate(redundantXSpace / 2, redundantYSpace / 2);
            normalizedScale = 1;

        } else {
            //
            // These values should never be 0 or we will set viewWidth and viewHeight
            // to NaN in translateMatrixAfterRotate. To avoid this, call savePreviousImageValues
            // to set them equal to the current values.
            //
            if (prevMatchViewWidth == 0 || prevMatchViewHeight == 0) {
                savePreviousImageValues();
            }

            prevMatrix.getValues(m);

代码在垂直中心加载我的图像。我想要实现的是将图像加载到垂直顶部。如果我将matchViewHeight = viewHeight - redundantYSpace;更改为matchViewHeight = viewHeight;,则图片将填满整个高度。我应该做什么修改才能在垂直顶部对齐?

1 个答案:

答案 0 :(得分:1)

使用

matrix.setScale(scaleX, scaleY);
matrix.postTranslate(redundantXSpace / 2, 0);

postTranslate - 移动图片。您无需沿y轴移动以在顶部对齐。

在底部对齐:

matrix.setScale(scaleX, scaleY);
matrix.postTranslate(redundantXSpace / 2, redundantYSpace );