PhotoView的简介:
这是一个图片查看库,实现图片浏览功能,支持pinch(捏合)手势或者点击放大缩小。支持在ViewPager中翻页浏览图片。
MainActivity中:
BaseApplication中:
主清单配置文件中:
这是一个图片查看库,实现图片浏览功能,支持pinch(捏合)手势或者点击放大缩小。支持在ViewPager中翻页浏览图片。
PhotoView 是一款扩展自Android ImageView ,支持通过单点/多点触摸来进行图片缩放的智能控件。功能实用和强大。
PhotoView的功能:
图片浏览查看
双指缩放
单点触摸缩放
图片缩放模式设置
基本用法:
导入jar包,布局XML里设置PhotoView
将ImageView传入PhotoViewAttacher
代码演示:
使用 PhotoView进行网络图片和本地图片的加载,缩放和点击事件处理
布局文件中:
- <span style="background-color: rgb(255, 255, 255);"><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></span>
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();
- }
- });
- }
- }
- /**
- * 加载网络图片时,需要对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>
效果:
photoview-library-1.2.2.jar
universal-image-loader-1.9.2_sources.jar
效果展示:
jar包及源码下载: