自定义放大/缩小按钮将无法同时使用

时间:2019-06-13 13:13:00

标签: java android

我正在制作按钮以放大/缩小图像视图。放大按钮正常工作。但是,一旦添加了“缩小”按钮,“放大”按钮将停止工作,并放大图像。

我尝试切换变量名称以确保正确初始化它们。删除缩小按钮及其功能后,放大按钮即可正常工作。只有缩小按钮功能才能使放大按钮中断。

@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>

两个按钮都出现在屏幕上。当我只有放大按钮时,图像视图将按预期放大。但是,一旦我添加了使缩小按钮起作用的功能,图像就会缩小/消失,然后重新出现并开始放大。放大按钮也停止工作。

1 个答案:

答案 0 :(得分:0)

由于您在break之前没有case R.id.zoomOut:,因此您在交换机中遇到“掉线”现象。这意味着程序正在移至下一个case,在您的情况下为“缩小”功能。您没有看到任何效果,因为这抵消了“放大”功能。

最好在最后一个break之后添加case,以避免出现此类错误。