首先在moddle的gradle导入依赖
compile ‘com.github.chrisbanes:PhotoView:2.1.3’
在项目的gradle里面的repositories下导入
maven { url ‘https://jitpack.io’ }
开始
图片的点击事件就不写了
先把图片链接放入一个String类型集合中Array
再传入一个你点击的第几张图片的数
Intent intent = new Intent(context, LookImageActivity.class);
intent.putStringArrayListExtra("image",imagety);
intent.putExtra("currentPosition",position);
//这是带动画的跳转需要在布局文件的控件加一个属性 在后文
context.startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(context,holder.marketpinglunimage, "shareNames").toBundle());
在你点击的控件上加上
android:transitionName=“shareNames”
还有在你跳转的页面加上这个
android:transitionName=“shareNames”
例如:
<com.lllkkk.coggg.view.MultiImageView
android:transitionName="shareNames"
android:id="@+id/marketpinglunimage"
android:layout_marginLeft="60dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.lllkkk.coggg.view.MultiImageView>
public class LookImageActivity extends Activity {
private int currentPosition;
private ArrayList<String> Urls;
private MyImageAdapter adapter;
private TextView tvImageCount;
private PhotoViewPager viewPagerPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
setContentView(R.layout.activity_look_image);
tvImageCount = findViewById(R.id.tv_image_count);
viewPagerPhoto = findViewById(R.id.view_pager_photo);
initData();
}
private void initData() {
Intent intent = getIntent();
currentPosition = intent.getIntExtra("currentPosition", 0);
Urls = intent.getStringArrayListExtra("image");
adapter = new MyImageAdapter(Urls, this);
viewPagerPhoto.setAdapter(adapter);
viewPagerPhoto.setCurrentItem(currentPosition, false);
tvImageCount.setText(currentPosition+1 + "/" + Urls.size());
viewPagerPhoto.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
currentPosition = position;
tvImageCount.setText(currentPosition + 1 + "/" + Urls.size());
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
Log.e("aaa","按了返回键");
}
}
public class MyImageAdapter extends PagerAdapter {
private List<String> imageUrls;
private Activity activity;
public MyImageAdapter(List<String> imageUrls, Activity activity) {
this.imageUrls = imageUrls;
this.activity = activity;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
String url = imageUrls.get(position);
PhotoView photoView = new PhotoView(activity);
Glide.with(activity)
.load(url)
.placeholder(R.mipmap.ic_launcher)//加载等待图
.diskCacheStrategy(DiskCacheStrategy.ALL)//缓存模式
.into(photoView);
container.addView(photoView);
photoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//调用返回键返回不要用finish()调用finish()会没有返回动画的
Log.e("aaa", "onClick: ");
activity.onBackPressed();
}
});
return photoView;
}
@Override
public int getCount() {
return imageUrls != null ? imageUrls.size() : 0;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
布局
<?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="match_parent"
android:orientation="vertical"
android:background="#000"
>
<com.lllkkk.coggg.view.PhotoViewPager
android:id="@+id/view_pager_photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transitionName="shareNames"/>
<TextView
android:id="@+id/tv_image_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1/3"
android:textColor="#fff"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>
public class PhotoViewPager extends ViewPager {
public PhotoViewPager(Context context) {
super(context);
}
public PhotoViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
try {
return super.onInterceptTouchEvent(ev);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return false;
}
}
}