繁体   English   中英

在自定义ImageView中顶部对齐图像

[英]Align image on top in Custom ImageView

我正在使用ViewPagerViewPager将图像加载到我的应用程序。 这是负责图像加载的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; ,图像将填满整个高度。 我应该进行哪些修改才能在垂直顶部对齐?

采用

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

postTranslate-移动图像。 您无需沿y轴移动即可在顶部对齐。

对于底部对齐:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM