MultiImageView+PhotoView+ViewPager图片预览

效果截图

MultiImageView+PhotoView+ViewPager图片预览_第1张图片

MultiImageView+PhotoView+ViewPager图片预览_第2张图片

PhotoView

PhotoView是一个github上的一个开源项目。

  • 将下面加入到根build.gradle文件中
allprojects {
	repositories {
        maven { url "https://jitpack.io" }
    }
}
  • 然后,加入库到你的模块build.gradle
dependencies {
    implementation 'com.github.chrisbanes:PhotoView:2.0.0'
}

MultiImageView

MultiImageView是多图加载的一个控件,可根据app:maxSize来控制图片加载个数,最多9个,多出来的在最后一个图片上显示总共图片数量。

  • 将下面加入到根build.gradle文件中
allprojects {
    repositories {
      maven { url 'https://jitpack.io' }
    }
}
  • 然后,加入库到你的模块build.gradle
dependencies {
    implementation 'com.github.ydslib:MultiImageView:1.0.1'
}

使用

  • ViewPager的adapter
package com.yds.mainmodule.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.yds.jianshulib.widget.photoview.PhotoView;

import java.util.List;

/**
 * Created by yds
 * on 2020/4/8.
 */
public class ImageViewPagerAdapter<T> extends PagerAdapter {
    private List<T> mImageList;
    private Context mContext;

    public ImageViewPagerAdapter(List<T> mImageList, Context mContext) {
        this.mImageList = mImageList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        T url = mImageList.get(position);
        PhotoView photoView = new PhotoView(mContext);
        Glide.with(mContext)
                .load(url)
                .into(photoView);
        container.addView(photoView);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) mContext).finish();
            }
        });
        return photoView;
    }

    @Override
    public int getCount() {
        return mImageList != null ? mImageList.size() : 0;
    }

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

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

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }
}

  • PhotoViewActivity
package com.yds.mainmodule.mobile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.yds.jianshulib.base.mvp.BaseActivity;
import com.yds.jianshulib.base.mvp.IBasePresenter;
import com.yds.jianshulib.utils.statusbar.StatusBarUtil;
import com.yds.mainmodule.R;
import com.yds.mainmodule.adapter.ImageViewPagerAdapter;

import java.util.List;

/**
 * Created by yds
 * on 2020/4/8.
 */
public class PhotoViewActivity extends BaseActivity {
    private ViewPager mImageViewPager;
    private TextView mCurImageNum;

    private int mCurrentPosition;
    private List<String> mImageList;

    @Override
    protected void initLayout(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_photo_view);
    }

    @Override
    protected void initViews() {
        mImageViewPager = findViewById(R.id.view_pager);
        mCurImageNum = findViewById(R.id.image_num);

        Intent intent = getIntent();
        mCurrentPosition = intent.getIntExtra("currentPosition", 0);
        mImageList = (List<String>) intent.getSerializableExtra("imageUrlList");
    }

    @Override
    protected void initEvents() {
        ImageViewPagerAdapter adapter = new ImageViewPagerAdapter<>(mImageList, this);
        mImageViewPager.setAdapter(adapter);

        mImageViewPager.setCurrentItem(mCurrentPosition, false);
        mCurImageNum.setText(mCurrentPosition + 1 + "/" + mImageList.size());
        mImageViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mCurrentPosition = position;
                mCurImageNum.setText(mCurrentPosition + 1 + "/" + mImageList.size());
            }
        });
    }

    @Override
    protected IBasePresenter setPresenter() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
    }

}

RecyclerView的adapter

      private MultiImageView mImageView;
      ......
      viewHolder.mImageView.setOnItemImageClickListener(new MultiImageView.OnItemImageClickListener() {
                @Override
                public void onItemImageClick(Context context, ImageView imageView, int index, List list) {
                    Intent intent = new Intent(context, PhotoViewActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("imageUrlList", (Serializable) list);
                    intent.putExtras(bundle);
                    intent.putExtra("currentPosition", index);
                    context.startActivity(intent);
                }
            });
  • activity_photo_view.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#b0000000"
    android:orientation="vertical">
    <TextView
        android:id="@+id/image_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textSize="16sp"
        android:textColor="#ffffff"
        tools:text="1/9"/>
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    androidx.viewpager.widget.ViewPager>
androidx.constraintlayout.widget.ConstraintLayout>

代码地址:https://github.com/ydslib/Jianshu

你可能感兴趣的:(Android,Android实例)