简体   繁体   English

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

[英]Align image on top in Custom ImageView

I am using ViewPager from TouchImageView to load images to my app. 我正在使用ViewPagerViewPager将图像加载到我的应用程序。 This is the TouchImageView.java which responsible for the loading of image. 这是负责图像加载的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);

The code load my images on vertical center. 代码将我的图像加载到垂直中心。 What I want to achieve is to load the images on vertical top. 我要实现的是将图像加载到垂直顶部。 If I change the matchViewHeight = viewHeight - redundantYSpace; 如果我更改matchViewHeight = viewHeight - redundantYSpace; to matchViewHeight = viewHeight; matchViewHeight = viewHeight; , the image will fill the entire height. ,图像将填满整个高度。 What modification should I made to make to align on vertical top? 我应该进行哪些修改才能在垂直顶部对齐?

use 采用

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

postTranslate - moved the image. postTranslate-移动图像。 You do not need to move along the y axis for align on top. 您无需沿y轴移动即可在顶部对齐。

For align on bottom: 对于底部对齐:

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

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

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