简体   繁体   English

自定义放大/缩小按钮不能同时工作

[英]The custom zoom in/out buttons will not work at the same time

I am making buttons to zoom in/out on an image view. 我正在制作按钮来放大/缩小图像视图。 The zoom in button works correctly. 放大按钮可正常工作。 However, once I add the zoom out button, the zoom in button ceases to work and it zooms in on the image. 但是,一旦我添加缩小按钮,放大按钮就会停止工作并放大图像。

I have tried switching the variable names to make sure they're initialized correctly. 我尝试切换变量名称以确保它们已正确初始化。 Once I delete the zoom out button and its function, the zoom in button works correctly. 删除缩小按钮及其功能后,放大按钮可正常工作。 It is only the zoom out button function that makes the zoom in button break. 只有缩小按钮功能才能使缩放按钮中断。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_camera2_video,container,false);
}

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    mTextureView = view.findViewById(R.id.texture);
    mButtonVideo = view.findViewById(R.id.video);

    mZoomIn = view.findViewById(R.id.zoomIn);
    mZoomOut = view.findViewById(R.id.zoomOut);
    img = view.findViewById(R.id.imageView1);

    mButtonVideo.setOnClickListener(this);
    mZoomIn.setOnClickListener(this);
    mZoomOut.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.video: {
            if (mIsRecordingVideo) {
                stopRecordingVideo();
            } else {
                startRecordingVideo();
            }
            break;
        }

        // Zoom In
        case R.id.zoomIn: {
            float x = img.getScaleX();
            float y = img.getScaleY();

            img.setScaleX(x + 1);
            img.setScaleY(y + 1);
        }

        // Zoom Out
        case R.id.zoomOut: {
            float x = img.getScaleX();
            float y = img.getScaleY();

            img.setScaleX(x - 1);
            img.setScaleY(y - 1);
        }
    }
}


<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.android.camera2video.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <ImageButton
        android:id="@+id/zoomIn"
        android:layout_width="49dp"
        android:layout_height="46dp"
        android:layout_above="@+id/zoomOut"
        android:layout_alignStart="@+id/texture"
        android:layout_alignEnd="@+id/zoomOut"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="0dp"
        android:contentDescription="@string/zoom_in"
        android:src="@drawable/ic_zoom_in_black_24dp" />

    <ImageButton
        android:id="@+id/zoomOut"
        android:layout_width="49dp"
        android:layout_height="46dp"
        android:layout_above="@+id/change_screen_brightness_seekbar"
        android:layout_alignStart="@+id/texture"
        android:layout_alignEnd="@+id/video"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="0dp"
        android:contentDescription="@string/zoom_out"
        android:src="@drawable/ic_zoom_out_black_24dp" />

    <ImageView
        android:contentDescription="@string/reticle"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:id="@+id/imageView1"
        android:src="@drawable/reticle"
        android:layout_height="wrap_content">

    </ImageView>
</RelativeLayout>

Both the buttons appear on the screen. 这两个按钮都出现在屏幕上。 When I have just the zoom in button, the image view will zoom in as expected. 当我只有放大按钮时,图像视图将按预期放大。 But once I add the function to make the zoom out button work, it looks like the image will zoom out/disappear, then reappear and begin zooming in. The zoom in button stops working too. 但是,一旦我添加了使缩小按钮工作的功能,看起来图像将缩小/消失,然后重新出现并开始放大。放大按钮也会停止工作。

You are experiencing "fall-through" in your switch because you don't have a break before case R.id.zoomOut: . 您在交换机中遇到“跌落”,因为在case R.id.zoomOut:之前没有break case R.id.zoomOut: This means that the program is moving on to the next case , which in your case is the "zoom out" functionality. 这意味着程序正在进行下一个case ,在您的情况下是“缩小”功能。 You aren't seeing any effect because that cancels out the "zoom in" functionality. 您没有看到任何效果,因为它取消了“放大”功能。

It is a good practice to add the break after the last case to avoid just this sort of error. 在最后case之后添加break是一种很好的做法,以避免这种错误。

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

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