Android第三方控件PhotoView使用方法詳解
PhotoView的簡(jiǎn)介:
這是一個(gè)圖片查看庫(kù),實(shí)現(xiàn)圖片瀏覽功能,支持pinch(捏合)手勢(shì)或者點(diǎn)擊放大縮小。支持在ViewPager中翻頁(yè)瀏覽圖片。
PhotoView 是一款擴(kuò)展自Android ImageView ,支持通過(guò)單點(diǎn)/多點(diǎn)觸摸來(lái)進(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ò)圖片時(shí),需要對(duì)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)都是可選的,只使用那些你真的想定制),這個(gè)可以設(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())
// 將保存的時(shí)候的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包及源碼:下載
這個(gè)庫(kù)里面上面庫(kù)里面有bug,參考這個(gè)庫(kù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android仿打開(kāi)微信紅包動(dòng)畫效果實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿打開(kāi)微信紅包動(dòng)畫效果實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Android實(shí)現(xiàn)Ant Design 自定義表單組件
Ant Design 組件提供了Input,InputNumber,Radio,Select,uplod等表單組件,下面通過(guò)本文給大家詳細(xì)介紹Android實(shí)現(xiàn)Ant Design 自定義表單組件,需要的的朋友參考下吧2017-06-06
Android仿新浪微博啟動(dòng)界面或登陸界面(1)
這篇文章主要為大家詳細(xì)介紹了Android仿新浪微博啟動(dòng)界面或登陸界面的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題
最近幾天在學(xué)習(xí)使用phonegap進(jìn)行android應(yīng)用的開(kāi)發(fā),網(wǎng)上的資料比較亂,個(gè)人參考了很多資料,也試驗(yàn)了很多次,一直在摸索,總算小有心得,這此過(guò)程中也遇到了一些問(wèn)題,這里給大家分享下解決Phonegap使用拍照功能時(shí)的內(nèi)存問(wèn)題的方法,這里簡(jiǎn)單的整理一下2015-05-05
anroid開(kāi)發(fā)教程之spinner下拉列表的使用示例
這篇文章主要介紹了anroid的spinner下拉列表的使用示例,需要的朋友可以參考下2014-04-04
Android 代碼寫控件代替XML簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android 代碼寫控件代替XML簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
解析Android中View轉(zhuǎn)換為Bitmap及getDrawingCache=null的解決方法
在android中經(jīng)常會(huì)遇到View轉(zhuǎn)換為Bitmap的情形,本篇文章主要介紹了Android中View轉(zhuǎn)換為Bitmap及getDrawingCache=null的解決方法,有需要的可以了解一下。2016-11-11
Android仿微信語(yǔ)音聊天界面設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Android仿微信語(yǔ)音聊天界面設(shè)計(jì)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)新手引導(dǎo)半透明蒙層效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03

