viewpager+photoview实现图片查看器
作者:ZhengJiaoCsdn 时间:2022-10-30 11:46:05
本文实例为大家分享了Android实现图片查看器的具体代码,供大家参考,具体内容如下
效果需要两个手指禁止缩放,所以没有光标,只能用手机投放电脑上录制动态图片;
demo中实用了一个第三方的photoview,非常简单实用;可实现图片双击放大,手势放大缩小,当手指离开屏幕时如果图片小于原图可自动恢复原图大小,可实现点击监听,长按图片监听;
整个demo非常简单,整体就是一个activity,页面布局只有一个viewpager和textview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#ffffff"
android:textSize="30sp" />
</RelativeLayout>
在activity中初始化图片的url,将集合传递到适配器FragmentPagerAdapter中即可中即可;
每个适配器中显示一个fragment,这里自己创建一个即可
/**
* Created by zheng on 2017/11/27.
*/
public class PhotoFragment extends Fragment {
private String url;
private PhotoView mPhotoView;
/**
* 获取这个fragment需要展示图片的url
* @param url
* @return
*/
public static PhotoFragment newInstance(String url) {
PhotoFragment fragment = new PhotoFragment();
Bundle args = new Bundle();
args.putString("url", url);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
url = getArguments().getString("url");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_img, container, false);
mPhotoView = view.findViewById(R.id.photoview);
//设置缩放类型,默认ScaleType.CENTER(可以不设置)
mPhotoView.setScaleType(ImageView.ScaleType.CENTER);
mPhotoView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
ToastUtils.showToast(getContext(),"长按事件");
return true;
}
});
mPhotoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
@Override
public void onPhotoTap(View view, float x, float y) {
ToastUtils.showToast(getContext(),"点击事件,真实项目中可关闭activity");
}
});
Glide.with(getContext())
.load(url)
.placeholder(R.mipmap.ic_launcher)//加载过程中图片未显示时显示的本地图片
.error(R.mipmap.ic_launcher)//加载异常时显示的图片
// .centerCrop()//图片图填充ImageView设置的大小
.fitCenter()//缩放图像测量出来等于或小于ImageView的边界范围,该图像将会完全显示
.into(mPhotoView);
return view;
}
}
fragment布局非常简单,只有一个图片展示的view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<uk.co.senab.photoview.PhotoView
android:id="@+id/photoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
想要实用PhotoView和Glide需要build.gradle中添加
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
dependencies {
compile 'com.github.chrisbanes.photoview:library:+'
compile 'com.github.bumptech.glide:glide:3.7.0'
}
点击打开链接免费下载源码
来源:http://blog.csdn.net/zheng_jiao/article/details/78652616
标签:viewpager,photoview,图片查看器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入了解c# 信号量和互斥体
2022-09-19 11:07:47
![](https://img.aspxhome.com/file/2023/4/113864_0s.png)
Java String类常用方法梳理总结
2021-08-22 17:57:03
Java JDK11基于嵌套的访问控制的实现
2021-07-11 10:02:05
IDEA提高开发效率的7个插件(推荐)
2021-10-16 15:28:35
![](https://img.aspxhome.com/file/2023/0/83410_0s.jpg)
springboot集成opencv实现人脸识别功能的详细步骤
2023-10-07 00:38:22
![](https://img.aspxhome.com/file/2023/0/74930_0s.png)
Android编程画图之抗锯齿解决方法
2022-12-17 12:03:54
![](https://img.aspxhome.com/file/2023/1/138711_0s.png)
Java对象的内存布局详细介绍
2021-07-28 05:11:38
![](https://img.aspxhome.com/file/2023/0/89470_0s.png)
Flutter底部导航栏的实现方式
2023-11-05 18:21:36
![](https://img.aspxhome.com/file/2023/3/126803_0s.gif)
Cocos2d-x入门教程(详细的实例和讲解)
2023-07-31 17:18:00
Mybatis多表关联查询的实现(DEMO)
2022-05-03 14:50:03
![](https://img.aspxhome.com/file/2023/3/67613_0s.png)
Winform 实现进度条弹窗和任务控制
2023-06-20 04:27:09
![](https://img.aspxhome.com/file/2023/4/66134_0s.png)
Unity实现轮盘方式的按钮滚动效果
2022-06-19 16:53:57
![](https://img.aspxhome.com/file/2023/5/113685_0s.gif)
Android 媒体库数据更新方法总结
2022-04-24 10:22:17
mybatis的if判断integer问题
2022-12-23 20:15:41
implicit关键字做自定义类型隐式转换的方法
2021-10-22 20:00:40
C#实现的阴历阳历互相转化类实例
2021-12-24 06:41:39
![](https://img.aspxhome.com/file/2023/0/85730_0s.png)
解决mybatis update并非所有字段需要更新问题
2022-12-09 10:20:55
![](https://img.aspxhome.com/file/2023/9/61599_0s.jpg)
分析java中全面的单例模式多种实现方式
2021-12-28 05:40:29
Lucene实现索引和查询的实例讲解
2022-09-06 16:35:30
![](https://img.aspxhome.com/file/2023/3/118903_0s.png)
Java命令设计模式详解
2022-07-14 04:38:31