Using PhotoView to view pictures

The project needs to realize the function of picture viewing. Using ImageView can't scale, using the system's picture viewer can't meet their own customized needs, so I used Viewpager + PhotoView to realize it.

Introducing PhotoView

Github link for PhotoView , PhotoView is designed to help developers easily scale Android ImageView.

1. Configure warehouse address

Add the following code to the build.gradle file of the project

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

If there are multiple URLs, you can write

allprojects {
    repositories {
        jcenter()
        maven { url 'https://esri.bintray.com/arcgis' }
        maven { url "https://jitpack.io" }
    }
}

2. Import third party Library

Add in the build.gradle file of your project's app module

compile 'com.github.chrisbanes:PhotoView:2.1.3'

3. Control use

After introducing the tripartite library, you can use the PhotoView control

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

The realization of picture viewer

1. Define TaskBigImgActivity.java for viewing pictures

public class TaskBigImgActivity extends BaseActivity {

    @BindView(R.id.header_title)
    TextView headerTitle;
    @BindView(R.id.header_left_img)
    ImageView headerLeftImg;
    @BindView(R.id.big_img_vp)
    ViewPager bigImgVp;
    @BindView(R.id.header_right_tv)
    TextView headerRightTv;
    private int position;
    private ArrayList<String> paths;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_big_img);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        position = intent.getIntExtra("position", 0);
        paths = intent.getStringArrayListExtra("paths");
        String title = intent.getStringExtra("title");
        headerTitle.setText(title);
        headerLeftImg.setVisibility(View.VISIBLE);
        headerRightTv.setVisibility(View.VISIBLE);
        headerRightTv.setText(position + 1 + "/" + paths.size());
        initView();
    }

    private void initView() {
        bigImgVp.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return paths == null ? 0 : paths.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object o) {
                return view == o;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View adView = LayoutInflater.from(TaskBigImgActivity.this).inflate(R.layout.item_big_img, null);
                PhotoView icon = (PhotoView) adView.findViewById(R.id.flaw_img);
                icon.setBackgroundColor(getResources().getColor(R.color.colorBlack));
                Glide.with(TaskBigImgActivity.this)
                        .load(paths.get(position))
                        .into(icon);
                container.addView(adView);
                return adView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });

        bigImgVp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                headerRightTv.setText(position + 1 + "/" + paths.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }

        });

        bigImgVp.setCurrentItem(position, true);
    }

    @OnClick(R.id.header_left_img)
    public void onClick() {
        finish();
    }
}

Corresponding layout file activity task big img.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context="com.cnbs.cableinspection.user.activity.TaskBigImgActivity">

    <include layout="@layout/header" />

    <android.support.v4.view.ViewPager
        android:id="@+id/big_img_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Layout item > big > img.xml used in Adapter

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorWhite"
    android:gravity="center">

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

</RelativeLayout>

2. Call to view pictures

private ArrayList<String> recordPaths = new ArrayList<>(); //Picture collection of defect record
...
Intent imgIntent = new Intent(TaskRecordActivity.this, TaskBigImgActivity.class);
imgIntent.putStringArrayListExtra("paths",recordPaths);
imgIntent.putExtra("title","Defect record picture");
imgIntent.putExtra("position",msg.arg2);
startActivity(imgIntent);

position refers to the pictures viewed

Here you can view the picture freely, and realize the transformation of scale, rotation and so on.

Keywords: Android github xml Maven

Added by yanisdon on Thu, 30 Apr 2020 17:22:01 +0300