一款好用的android图片缩放查看开源框架photoview的使用。
程序员文章站
2024-01-19 12:40:28
...
介绍:框架核心类PhotoView继承ImageView,它根据用户手势的不同,给图片做出相应的缩放,平移等动作。
使用步骤:
1.在project的build.gradle中添加如下:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
2.在app的build.gradle中添加依赖:
1).如果你想依赖的是1.x版本:
implementation ‘com.github.chrisbanes:PhotoView:1.x’(x请自己替换掉)
2).如果你想依赖的是2.x版本:
implementation ‘com.github.chrisbanes:PhotoView:2.x’(x请自己替换掉)
然后sync now.
3.xml布局如下:
针对1.x版本:
<uk.co.senab.photoview.PhotoView
android:id="@+id/photoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
针对2.x版本:
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/photoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
4.在activity的代码:
public class PhotoViewActivity extends AppCompatActivity {
private PhotoView mPhotoView;
private PhotoViewAttacher mAttacher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_view);
mPhotoView= (PhotoView) findViewById(R.id.photoview);
mAttacher=new PhotoViewAttacher(mPhotoView);
mPhotoView.setImageResource(R.mipmap.a);
mAttacher.update();
}
}