Android第三方控件PhotoView使用方法详解

时间:2019-04-11
本文章向大家介绍Android第三方控件PhotoView使用方法详解,主要包括Android第三方控件PhotoView使用方法详解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

PhotoView的简介:

这是一个图片查看库,实现图片浏览功能,支持pinch(捏合)手势或者点击放大缩小。支持在ViewPager中翻页浏览图片。

PhotoView 是一款扩展自Android ImageView ,支持通过单点/多点触摸来进行图片缩放的智能控件。功能实用和强大。

PhotoView的功能:

图片浏览查看
双指缩放
单点触摸缩放
图片缩放模式设置

基本用法:
导入jar包,布局XML里设置PhotoView
将ImageView传入PhotoViewAttacher

代码演示:

使用 PhotoView进行网络图片和本地图片的加载,缩放和点击事件处理
布局文件中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context=".MainActivity" 
 android:orientation="vertical" 
 > 
 
 <uk.co.senab.photoview.PhotoView 
 android:id="@+id/iv_photo1" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 
 /> 
 <uk.co.senab.photoview.PhotoView 
 android:id="@+id/iv_photo2" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 
 /> 
 
</LinearLayout> 

MainActivity中:

public class MainActivity extends Activity { 
 private PhotoView iv_photo1; 
 private PhotoView iv_photo2; 
 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 iv_photo1 = (PhotoView) findViewById(R.id.iv_photo1); 
 iv_photo2 = (PhotoView) findViewById(R.id.iv_photo2); 
// localImage(); 
 netImage(); 
 
 } 
 
/** 
 * 加载本地图片 
 * 
 */ 
 private void localImage() { 
// 加载本地图片,缩放处理 
 try { 
// 图片在asset目录中 
 InputStream is = getAssets().open("photo2.jpg"); 
 Bitmap bm = BitmapFactory.decodeStream(is); 
 iv_photo1.setImageBitmap(bm); 
 } catch (IOException e) { 
 e.printStackTrace(); 
 } 
 
 } 
 /** 
 * 加载网络图片 
 */ 
 private void netImage() { 
 ImageLoader loader = ImageLoader.getInstance(); 
 loader.displayImage("https://www.baidu.com/img/bdlogo.png", iv_photo2); 
 iv_photo2.setOnPhotoTapListener(new OnPhotoTapListener() { 
 
 @Override 
 public void onPhotoTap(View arg0, float arg1, float arg2) { 
 Toast.makeText(MainActivity.this, "图片被点击了", 10).show(); 
 } 
 }); 
 
 } 
 
 
 
} 

BaseApplication中:

/** 
 * 加载网络图片时,需要对ImageLoader进行全局配置 
 * 
 */ 
 
public class BaseApplication extends Application { 
 
 @Override 
 public void onCreate() { 
 super.onCreate(); 
 initImagloader(getApplicationContext()); 
 } 
 
 private void initImagloader(Context context) { 
 File cacheDir = StorageUtils.getOwnCacheDirectory(context, 
 "photoview/Cache");// 获取到缓存的目录地址 
 // 创建配置ImageLoader(所有的选项都是可选的,只使用那些你真的想定制),这个可以设定在APPLACATION里面,设置为全局的配置参数 
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( 
 context) 
 // 线程池内加载的数量 
 .threadPoolSize(3).threadPriority(Thread.NORM_PRIORITY - 2) 
 .memoryCache(new WeakMemoryCache()) 
 .denyCacheImageMultipleSizesInMemory() 
 .discCacheFileNameGenerator(new Md5FileNameGenerator()) 
 // 将保存的时候的URI名称用MD5 加密 
 .tasksProcessingOrder(QueueProcessingType.LIFO) 
 .discCache(new UnlimitedDiscCache(cacheDir))// 自定义缓存路径 
 // .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) 
 .writeDebugLogs() // Remove for release app 
 .build(); 
 // Initialize ImageLoader with configuration. 
 ImageLoader.getInstance().init(config);// 全局初始化此配置 
 } 
} 

主清单配置文件中:

<uses-permission android:name="android.permission.INTERNET"/> 
 <application 
 android:name="com.zhhandroid.BaseApplication" 
 android:allowBackup="true" 
 android:icon="@drawable/ic_launcher" 
 android:label="@string/app_name" 
 android:theme="@style/AppTheme" > 
 <activity 
 android:name=".MainActivity" 
 android:label="@string/app_name" > 
 <intent-filter> 
 <action android:name="android.intent.action.MAIN" /> 
 
 <category android:name="android.intent.category.LAUNCHER" /> 
 </intent-filter> 
 </activity> 
 </application> 

需要导入的jar包:

photoview-library-1.2.2.jar
universal-image-loader-1.9.2_sources.jar

效果展示:

jar包及源码:下载

这个库里面上面库里面有bug,参考这个库

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。