PhotoView的点击事件遇到的问题

问题描述

项目中有个查看图片的功能,之前的同事用的PhotoView + ViewPager 实现的,现在我要补充的一个功能就是点击其中一张图片然后查看原图(带转场动画),再点击一次就回到原来的页面。

功能实现

点击跳转大图,包括转场动画和简单实现,我们再次就不赘述了,如果遇到问题了,可以看看我这篇文章
关于activity转场动画makeSceneTransitionAnimation没效果的问题的解决办法
接下来实现点击一次大图片就回到上一页面的这个功能时就遇到了问题,我在网上搜到使用这个监听方法:

mAttacher = new PhotoViewAttacher(photoView);
mAttacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }
            });

这么写是可以实现单击大图回到上个页面,但是放大的图片是在一个ViewPager中,图片在左右滑动的时候就会先把图片放大,然后在滑动到上一页下一页,我就郁闷了,为什么会这样,经过查看源码,发现PhotoViewAttacher还有个方法

 mAttacher.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    photoView.setOnPhotoTapListener(null);
                    onBackPressed();
                }
            });

还是不行,我就在想能不能再单击的时候设置PhotoView不能放大,但是试过之后还是不行,最后偶然试了一下直接设置PhotoView的setOnPhotoTapListener监听方法,运行之后达到了我要的效果。代码如下:

photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    onBackPressed();
                }
            });

总结

将我自己在工作中遇到的问题记录下来,问题可能有大有小,但是过一段时间再回过头看一看,对自己的以后的工作也有帮助,要是能帮助到和我遇到相同问题的同学,那我会很高兴的!

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要在 PhotoView 中实现涂鸦功能,你可以按照以下步骤进行操作: 1. 添加 PhotoView 依赖: 在你的项目的 `build.gradle` 文件中添加 PhotoView 依赖项: ```groovy dependencies { implementation 'com.github.chrisbanes:PhotoView:2.3.0' } ``` 确保使用的是最新版本的 PhotoView。 2. 在布局文件中添加 PhotoView: 在你的布局文件中添加 PhotoView 控件,并设置好相关属性,例如图片资源、缩放类型等: ```xml <com.github.chrisbanes.photoview.PhotoView android:id="@+id/photo_view" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/your_image" /> ``` 3. 添加涂鸦功能: 在代码中获取 PhotoView 对象,并设置触摸事件监听器,以实现涂鸦功能。你可以使用 Canvas 和 Paint 类来绘制路径。 ```java PhotoView photoView = findViewById(R.id.photo_view); photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() { @Override public void onPhotoTap(ImageView view, float x, float y) { // 获取触摸点坐标,并将其转换为图片上的坐标 Drawable drawable = photoView.getDrawable(); if (drawable != null) { int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int viewWidth = photoView.getWidth(); int viewHeight = photoView.getHeight(); float imageX = (x * drawableWidth) / viewWidth; float imageY = (y * drawableHeight) / viewHeight; // 在图片上绘制涂鸦 Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(5); canvas.drawPoint(imageX, imageY, paint); // 将修改后的图片显示在 PhotoViewphotoView.setImageBitmap(bitmap); } } }); ``` 在以上代码中,通过监听 `onPhotoTap` 方法获取触摸点的坐标,并将其转换为图片上的坐标。然后,使用 Canvas 和 Paint 绘制涂鸦,最后将修改后的图片显示在 PhotoView 上。 你可以根据需要修改涂鸦的样式、颜色和大小。 希望这些信息对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值