如何在ZoomageView上设置onClick监听器

问题描述 投票:0回答:1

我正在 kotlin 中为 android 制作图库应用程序。我正在使用 ZoomageView 全屏显示图像。我还想在 imag 单击的单击上显示菜单和底部以进行共享、添加/删除收藏夹等,但有问题。如果我在 Holder 上设置 setOnClickListener。

Holder.itemView
,它允许我放大和缩小,但不会显示菜单。如果我在
sliderImageView
(ZoomageView) 上设置 setOnClickListener,它将显示菜单,但我无法再放大或缩小图像。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.jsibbold.zoomage.ZoomageView
        android:id="@+id/sliderImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:zoomage_animateOnReset="true"
        app:zoomage_autoCenter="true"
        app:zoomage_autoResetMode="UNDER"
        app:zoomage_maxScale="8"
        app:zoomage_minScale="0.6"
        app:zoomage_restrictBounds="false"
        app:zoomage_translatable="true"
        app:zoomage_zoomable="true"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:visibility="gone"
        app:menu="@menu/bottom_navigation_menu" />


</LinearLayout>
 override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val photoPath: Uri = allImageList[position]
        val slideImageView: ImageView = holder.itemView.findViewById(R.id.sliderImageView)
        Glide.with(context).load(photoPath).into(slideImageView)

        var bottomNavigationView: BottomNavigationView =
            holder.itemView.findViewById(R.id.bottomNavigationView)

        // It allow me to zoom in and out but won't show me Bottom Menu
        holder.itemView.setOnClickListener {
            bottomNavigationView.visibility =
                if (bottomNavigationView.visibility == View.VISIBLE) View.GONE else View.VISIBLE
        }

        // It show Bottom menu but i can no longer zoom in or out 
        slideImageView.setOnClickListener {
            bottomNavigationView.visibility =
                if (bottomNavigationView.visibility == View.VISIBLE) View.GONE else View.VISIBLE
        }

    }

我想要放大和缩小图像,并在单击/单击图像时显示和隐藏菜单。

android kotlin imageview android-gallery
1个回答
0
投票

我查了

com.jsibbold.zoomage.ZoomageView
的源代码,我认为ZoomageView不能同时进行点击和缩放的原因是这一行

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // if the view is clickable, then it will skip handling zoom gestures
        if (!isClickable() && isEnabled() && (zoomable || translatable)) {
            // Handle the zoom gestures
            ......
        }

        return super.onTouchEvent(event);
    }

您可以看到视图是否为

Clickable
,那么它将跳过处理缩放手势。

所以,解决方案是:

  • 第1步,将视图设置为不可点击
  • 第2步,由于视图现在不可点击,因此我们将使用另一种方法来检测点击手势并调用原始点击监听器。例如以下代码使用
    setOnTouchListener()
    GestureDetector
    来实现。

这是解决方案代码,它似乎非常适合我的测试:

// First define a GestureTap class, just call performClick() when a tap gesture is detected
public class GestureTap extends GestureDetector.SimpleOnGestureListener {
    View view;
    public GestureTap(View view) {
        super();
        this.view = view;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        view.performClick();
        return true;
    }
}
// set OnClickListener as you want
slideImageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Log.e("wangpeiming", "imageView clicked");
        bottomNavigationView.visibility =
                if (bottomNavigationView.visibility == View.VISIBLE) View.GONE else View.VISIBLE
    }
});

// set the view to not clickable, so that it can handle zoom gestures afterward.
slideImageView.setClickable(false);

// use OnTouchListener and gestureDetector to detect tap gesture
final GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new GestureTap(slideImageView));
slideImageView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // let the gestureDetector to check and handle if there is a tap gesture
        gestureDetector.onTouchEvent(event);
        // must return false so that the view's onTouchEvent() can receive the event and handle zoom gestures.
        return false;
    }
});

希望有帮助~

© www.soinside.com 2019 - 2024. All rights reserved.