Android第三方控件PhotoView使用方法詳解
PhotoView的簡介:
這是一個圖片查看庫,實(shí)現(xiàn)圖片瀏覽功能,支持pinch(捏合)手勢或者點(diǎn)擊放大縮小。支持在ViewPager中翻頁瀏覽圖片。
PhotoView 是一款擴(kuò)展自Android ImageView ,支持通過單點(diǎn)/多點(diǎn)觸摸來進(jìn)行圖片縮放的智能控件。功能實(shí)用和強(qiáng)大。
PhotoView的功能:
圖片瀏覽查看
雙指縮放
單點(diǎn)觸摸縮放
圖片縮放模式設(shè)置
基本用法:
導(dǎo)入jar包,布局XML里設(shè)置PhotoView
將ImageView傳入PhotoViewAttacher
代碼演示:
使用 PhotoView進(jìn)行網(wǎng)絡(luò)圖片和本地圖片的加載,縮放和點(diǎn)擊事件處理
布局文件中:
<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();
}
}
/**
* 加載網(wǎng)絡(luò)圖片
*/
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, "圖片被點(diǎn)擊了", 10).show();
}
});
}
}
BaseApplication中:
/**
* 加載網(wǎng)絡(luò)圖片時,需要對ImageLoader進(jìn)行全局配置
*
*/
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");// 獲取到緩存的目錄地址
// 創(chuàng)建配置ImageLoader(所有的選項(xiàng)都是可選的,只使用那些你真的想定制),這個可以設(shè)定在APPLACATION里面,設(shè)置為全局的配置參數(shù)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context)
// 線程池內(nèi)加載的數(shù)量
.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>
需要導(dǎo)入的jar包:
photoview-library-1.2.2.jar
universal-image-loader-1.9.2_sources.jar
效果展示:

jar包及源碼:下載
這個庫里面上面庫里面有bug,參考這個庫
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android仿打開微信紅包動畫效果實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿打開微信紅包動畫效果實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-12-12
Android實(shí)現(xiàn)Ant Design 自定義表單組件
Ant Design 組件提供了Input,InputNumber,Radio,Select,uplod等表單組件,下面通過本文給大家詳細(xì)介紹Android實(shí)現(xiàn)Ant Design 自定義表單組件,需要的的朋友參考下吧2017-06-06
anroid開發(fā)教程之spinner下拉列表的使用示例
這篇文章主要介紹了anroid的spinner下拉列表的使用示例,需要的朋友可以參考下2014-04-04
Android實(shí)現(xiàn)簡易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
解析Android中View轉(zhuǎn)換為Bitmap及getDrawingCache=null的解決方法
在android中經(jīng)常會遇到View轉(zhuǎn)換為Bitmap的情形,本篇文章主要介紹了Android中View轉(zhuǎn)換為Bitmap及getDrawingCache=null的解決方法,有需要的可以了解一下。2016-11-11
Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03

