BÀI 94 - PHÓNG TO IMAGEVIEW TRONG ANDROID

  • Đầu tiên, thêm thư viện PhotoView này: source

implementation 'com.github.chrisbanes:PhotoView:2.0.0'

  • Trong mục settings.gradle thêm link maven như dưới

dependencyResolutionManagement {

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

repositories {

google()

mavenCentral()

//noinspection JcenterRepositoryObsolete

jcenter() // Warning: this repository is going to shut down soon

maven { url 'https://jitpack.io' }

}

}

  • Kỹ nữa thì trong buil.gradle (project) cũng thêm maven luôn

buildscript {

repositories {

google()

mavenCentral()

maven { url 'https://jitpack.io' }

}

}

  • Tạo một màn hình riêng biệt để hiển thị hình ảnh

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<com.github.chrisbanes.photoview.PhotoView

android:id="@+id/imageView_ZoomOutImageView"

android:adjustViewBounds="true"

android:scaleType="fitXY"

android:layout_width="match_parent"

android:layout_height="match_parent">

</com.github.chrisbanes.photoview.PhotoView>

</LinearLayout>

  • Code Java để đổ hình ảnh cần phóng bự đó ra

public class ShowFullScreenImageActivity extends AppCompatActivity {

ImageView imgPhoto;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_show_full_screen_image);

imgPhoto = findViewById(R.id.imageView_ZoomOutImageView);

Uri uri = getIntent().getData(); // Hình ảnh cần đổ ra màn hình

try {

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

if (bitmap != null) imgPhoto.setImageBitmap(bitmap);

} catch (IOException e) {

e.printStackTrace();

}

}

}