【Android -- 开源库】PhotoView 的基本使用

简介

一款 ImageView 展示框架,支持缩放,响应手势,位于图片排行榜的第五位,PhotoView 与上面不同的是图片的展示功能,可以实现类似微信头像的放大功能,还有就是很多 App 的图片显示响应手势按压式如何是现实的,这里 PhotoView 将都可以轻松实现。

GitHub 地址:https://github.com/Baseflow/PhotoView

效果图

请添加图片描述

代码实现

1. 添加依赖

  • 在 settings.gradle 中添加如下依赖:
pluginManagement {
    
    
    repositories {
    
    
        ...
        maven {
    
     url "https://www.jitpack.io" }
    }
}
dependencyResolutionManagement {
    
    
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
    
    
        ...
        maven {
    
     url "https://www.jitpack.io" }
    }
}
  • 在 app/build.gradle 下添加如下依赖
implementation 'com.github.chrisbanes:PhotoView:2.3.0'

2. 布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".PhotoViewActivity">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/photoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

3. 逻辑代码

public class PhotoViewActivity extends AppCompatActivity {
    
    
    private PhotoView mPhotoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_view);

        mPhotoView = findViewById(R.id.photoView);

        mPhotoView.setImageResource(R.mipmap.ic_girl);

    }
}

猜你喜欢

转载自blog.csdn.net/duoduo_11011/article/details/128984409