为什么我的图像在缩放后会被剪裁?

Yev*_*niy 6 scrollview imageview android-layout android-imageview

我有一个奇怪的问题.我缩放图像,并且在缩放正常工作时,图像总是被剪裁.我尝试了不同的比例类型 - 事情发生了变化,但我从来没有让它成功.

为了清楚起见,这就是我需要解决的问题:1.我HorizontalScrollView周围ImageViewScrollView周围都有HorizontalView.2.我滚动(使用scrollTo两个滚动视图),并在某个事件时放大.3.我想要发生的是ImageView围绕我当前的滚动位置缩放.

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        android:overScrollMode="never">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:overScrollMode="never">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="3dp"
                android:scaleType="fitCenter" />
        </HorizontalScrollView>
    </ScrollView>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

这里是缩放代码(originalWidth/ originalHeight按比例计算1; targetView指向ImageView):

public synchronized void changeScale(float newScaleFactor) {
    this.scaleFactor = Math.max(min_zoom, Math.min(newScaleFactor, max_zoom));
    if (targetView != null && originalWidth > 0) {
        int newWidth = (int)(originalWidth * scaleFactor);
        int newHeight = (int)(originalHeight * scaleFactor);
        onScaleChanged(targetView, scaleFactor, newWidth, newHeight);
    }
}


public void onScaleChanged(View targetView, float scaleFactor, int newWidth, int newHeight) {
    ViewGroup.LayoutParams layoutParams = targetView.getLayoutParams();
    layoutParams.width = newWidth;
    layoutParams.height = newHeight;

    // This is needed to increase the pane size (rather than zoom within the initial layout)
    targetView.setLayoutParams(layoutParams);
    // Tell the system to recalculate the layout
    targetView.requestLayout();

    // This is needed to specify the center of scaling
    HorizontalScrollView horizontalScrollView = (HorizontalScrollView)targetView.getParent();
    ScrollView vertScrollView = (ScrollView)horizontalScrollView.getParent();
    // ~~~ the pivot points are probably wrong 
    targetView.setPivotX(horizontalScrollView.getScrollX() * scaleFactor);
    targetView.setPivotY(vertScrollView.getScrollY() * scaleFactor);

    // This is needed for actual zooming
    targetView.setScaleX(scaleFactor);
    targetView.setScaleY(scaleFactor);
};


public void zoomIn(float scaleDelta) {
    changeScale(scaleFactor + scaleDelta);
}

public void zoomOut(float scaleDelta) {
    changeScale(scaleFactor - scaleDelta);
}
Run Code Online (Sandbox Code Playgroud)

问题1:如何防止剪辑?我无法找到正确的组合scaleType和布局大小调整.

问题2:当我使用setScaleX/时setScaleY,应该在应用新的比例因子后计算我的数据,还是渲染器会自动处理?

小智 1

更新比例后,您需要 invalidate() 和 requestLayout() 视图。

targetView.invalidate();
targetView.requestLayout();
Run Code Online (Sandbox Code Playgroud)

我通常以不同的方式计算图像的比例。您可以尝试使用 MATRIX 缩放类型来缩放图像视图。您需要知道绑定 DPI 的大小。

// Get the scaled DPI
int boundBoxInDp = getResources().getDisplayMetrics().densityDpi * scaleFactor

// Determine how much to scale: the dimension requiring less scaling is
// closer to its side. This way the image always stays inside your
// bound AND either x/y axis touches the bound but neither will go over.
float xScale = ((float) boundBoxInDp) / newWidth;
float yScale = ((float) boundBoxInDp) / newHeight;
float scale = (xScale <= yScale) ? xScale : yScale;

// scale using our calculated scale
targetView.setScaleX(scale);
targetView.setScaleY(scale);
Run Code Online (Sandbox Code Playgroud)

至于你关于枢轴的第二个问题。这可能需要设置为可见滚动区域的中心。当您更改图像大小时,应增加可滚动区域,因为您正在使用FIT_CENTER;