可实现功能效果说明:
可实现多张图片点击放大,手指控制,左右滑动,多张图片点击任意位置定位显示任意位置图片;无动画,可自己加
效果图:
核心代码:
viewpager:
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;
}
}
}
adapter:
public class MyImageAdapter extends PagerAdapter {
public static final String TAG = MyImageAdapter.class.getSimpleName();
private List<String> imageUrls;
private AppCompatActivity activity; public MyImageAdapter(List<String> imageUrls, AppCompatActivity 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);
Picasso.with(activity)
.load(url)
.into(photoView);
container.addView(photoView);
photoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: ");
activity.finish();
}
});
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;
}
}
photoViewActivity
public class PhotoViewActivity extends AppCompatActivity implements View.OnClickListener { public static final String TAG = PhotoViewActivity.class.getSimpleName();
private PhotoViewPager mViewPager;
private int currentPosition;
private MyImageAdapter adapter;
private TextView mTvImageCount;
private TextView mTvSaveImage;
private List<String> Urls; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_view);
initView();
initData();
} private void initView() {
mViewPager = (PhotoViewPager) findViewById(R.id.view_pager_photo);
mTvImageCount = (TextView) findViewById(R.id.tv_image_count);
mTvSaveImage = (TextView) findViewById(R.id.tv_save_image_photo);
mTvSaveImage.setOnClickListener(this); } private void initData() { Intent intent = getIntent();
currentPosition = intent.getIntExtra("currentPosition", 0);
HomeQuestionListModel.DataBeanX DataBean = ((HomeQuestionListModel.DataBeanX) intent.getSerializableExtra("questionlistdataBean"));
Urls = DataBean.getAttach().getImage().getOri(); adapter = new MyImageAdapter(Urls, this);
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem(currentPosition, false);
mTvImageCount.setText(currentPosition+1 + "/" + Urls.size());
mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
currentPosition = position;
mTvImageCount.setText(currentPosition + 1 + "/" + Urls.size());
}
});
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_save_image_photo:
//save image
break;
}
}
}
图片列表页面跳转:
private void statPhotoViewActivity(int position) {
Intent intent = new Intent(this, PhotoViewActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("dataBean", mData);
intent.putExtras(bundle);
intent.putExtra("currentPosition", position);
startActivity(intent);
}