文章大綱
一、什么是PhotoView
二、代碼實戰
三、項目源碼下載
一、什么是PhotoView
一款 ImageView 展示框架,支持縮放,響應手勢,位於圖片排行榜的第五位,PhotoView 與上面不同的是圖片的展示功能,可以實現類似微信頭像的放大功能,還有就是很多 App 的圖片顯示響應手勢按壓式如何是現實的,這里 PhotoView 將都可以輕松實現。
二、代碼實戰
1. 添加依賴
implementation 'com.bm.photoview:library:1.4.1'
2. 添加圖片資源
實際項目中,該圖片可能是通過網絡請求獲取的。

3. 編寫xml代碼
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.bm.library.PhotoView android:id="@+id/photoview" android:src="@drawable/test" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.constraint.ConstraintLayout>
3. 編寫Activity代碼
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PhotoView photoView = findViewById(R.id.photoview); photoView.enable();//設置是否允許縮放,默認是不允許的 } }
4. 運行結果如下

