可實現功能效果說明:html
可實現多張圖片點擊放大,手指控制,左右滑動,多張圖片點擊任意位置定位顯示任意位置圖片;無動畫,可本身加ide
效果圖:post
核心代碼:動畫
viewpager:this
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:url
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; } }
photoViewActivityspa
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; } } }
圖片列表頁面跳轉:code
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); }