之前写APP,要实现切换上一张、下一张图片、图片放大缩小的功能;
总结下遇到的坑。
一、ImageView展示图片位置错误
当图片大于imageView框时,总是展示图片的中部,而不是从左上角开始展示;本来是想让图片从左上角开始展示的。
解决方法:
使用android:scaleType="fitStart",就能让图片从左上角展示了。
代码如下:
<com.example.myapplication.photoview.PhotoView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitStart"
app:srcCompat="@drawable/blue" />
参考网址,scaleType详解:
https://www.cnblogs.com/51kata/p/4122142.html
二、图片放大缩小后位置错误
在网上找了一个图片放大缩小的标签代码,com.example.myapplication.photoview.PhotoView。
网址为:https://blog.youkuaiyun.com/wuqingsen1/article/details/84029503
然后使用时,发现图片双击放大、双击还原后,图片显示的位置错误,如下:
解决方法:
发现代码中设置图片url时,用的是ImageView的setImageURI(Uri.fromFile(this.files[this.beforePage()]));
这个方法正常展示图片没有问题,但是放大缩小后就出现了上方的问题,并且图片无法移动到正常位置。
后来偶然发现,使用ImageView的setImageBitmap(BitmapFactory.decodeFile(this.files[this.beforePage()].toString()));
用这个方法,就不会出现上方的问题了。
代码如下:
//ImageView img;
img.setImageBitmap(BitmapFactory.decodeFile(this.files[this.beforePage()].toString()));
//img.setImageURI(Uri.fromFile(this.files[this.beforePage()]));